0

I'm new to C# and I've attempted many of the solutions here on StackExchange and I've had no luck in trying to set up a control or the entire form for that matter to left click and drag the entire window. I'm working with a frameless window in visual studio 12. The closest I've come to moving the window is moving a single control with the pastebin component(last response) from this- How do I make mousedrag inside Panel move form window? I could only get the panel itself to move with that component.

I've tried most of the approaches but I seem get lost where I am to customize it to my own needs. I've tried WndProc override but it didn't do anything when I attempted to move the form window.

I have two panels I want to be able to drag the window with DragPanel and DragPanel2.

Here is my most recent failed approach trying to use the whole form.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
        InsideMover _dragger = new InsideMover();
        _dragger.ControlToMove = this.DragPanel;
    }

    private void close_Click(object sender, EventArgs e)
    {
        Close();
    }
}

public class InsideMover : Component
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private Container components = null;

    public InsideMover(IContainer container)
    {
        ///
        /// Required for Windows.Forms Class Composition Designer support
        ///
        container.Add(this);
        InitializeComponent();

        //
        // TODO: Add any constructor code after InitializeComponent call
        //
    }

    public InsideMover()
    {
        ///
        /// Required for Windows.Forms Class Composition Designer support
        ///
        InitializeComponent();

        //
        // TODO: Add any constructor code after InitializeComponent call
        //
    }

    public Control ControlToMove
    {
        set
        {
            if (_parent != null)
            {
                //  odkvaci prijasnje evente
                _parent.MouseDown -= new MouseEventHandler(_parent_MouseDown);
                _parent.MouseMove -= new MouseEventHandler(_parent_MouseMove);
                _parent.MouseUp -= new MouseEventHandler(_parent_MouseUp);
                _parent.DoubleClick -= new EventHandler(_parent_DoubleClick);
            }
            _parent = value;
            if (value != null)
            {
                //  zakači se na evente od containera koji ti trebaju
                _parent.MouseDown += new MouseEventHandler(_parent_MouseDown);
                _parent.MouseMove += new MouseEventHandler(_parent_MouseMove);
                _parent.MouseUp += new MouseEventHandler(_parent_MouseUp);
                _parent.DoubleClick += new EventHandler(_parent_DoubleClick);
            }
        }
        get
        {
            return _parent;
        }
    }

    Control _parent;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (components != null)
            {
                components.Dispose();
            }
        }
        base.Dispose(disposing);
    }


    #region Component Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        components = new System.ComponentModel.Container();
    }
    #endregion

    int _lastMouseX;
    int _lastMouseY;
    bool _moving;

    public void StartMouseDown(MouseEventArgs e)
    {
        _parent_MouseDown(null, e);
    }

    private void _parent_MouseDown(object sender, MouseEventArgs e)
    {
        _lastMouseX = e.X;
        _lastMouseY = e.Y;
        _moving = true;
    }

    private void _parent_MouseMove(object sender, MouseEventArgs e)
    {
        if (_moving)
        {
            Point newLocation = _parent.Location;
            newLocation.X += e.X - _lastMouseX;
            newLocation.Y += e.Y - _lastMouseY;
            _parent.Location = newLocation;
        }
    }

    private void _parent_MouseUp(object sender, MouseEventArgs e)
    {
        _moving = false;
    }

    private void _parent_DoubleClick(object sender, EventArgs e)
    {
        if (_parent is Form)
        {
            Form f = (Form)_parent;
            if (f.WindowState == FormWindowState.Normal)
            {
                f.WindowState = FormWindowState.Maximized;
            }
            else
            {
                f.WindowState = FormWindowState.Normal;
            }
        }
    }
}
}

How can I set the panels to left click drag the window?

I've tried all of the methods at the post above and the WndProc method here: Drag borderless windows form by mouse

Community
  • 1
  • 1
stemsmit
  • 31
  • 6

1 Answers1

0

If you follow the answer in the link you've posted, it is using the mousemove event, whereas judging by the code you've posted, you're using the mousedown event. The mouse down event is only called once when you press a mouse button, it is not called again if you move the mouse while you keep pressing the button. Whereas the mousemove event is called whenever your pointer moves. So your best bet would be to change your mousedown event with the mousemove event i.e.

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
   if (e.Button == MouseButtons.Left)
   {
      ReleaseCapture();
      SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
   }
}

if that doesn't work, you can do something like this in the mousemove event, first create a Point 'prevpoint' and an offset point in the form.

Point prevpoint=new Point(0,0);
Point offset=new Point(0,0);
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
   if (e.Button == MouseButtons.Left)
   {
      offset.X=e.X-prevpoint.X;
      offset.Y=e.Y-prevpoint.Y;
      prevpoint.X=e.X;
      prevpoint.Y=e.Y;
      this.Location = new Point(this.Location.X + offset.X, this.Location.Y + offset.Y);
   }
}

I have not tested the above code but that will hopefully give you the basic idea.

stranded
  • 312
  • 2
  • 9
  • you've mentioned that you could get the panel to move but not the form, can you post in that code and a picture of your form to clarify what you're trying to achieve? – stranded Mar 11 '14 at 07:05
  • Yes, I added the code just now. Only difference is that this moves the panel and not the window. – stemsmit Mar 11 '14 at 07:14
  • I receive an error with `this.Location.X+=offset.X;` and `this.Location.Y+=offset.Y;` stating that this isn't a variable. – stemsmit Mar 11 '14 at 07:20
  • try changing _dragger.ControlToMove = this.DragPanel; to _dragger.ControlToMove = this; – stranded Mar 11 '14 at 07:20
  • put the mousemove event on the panel instead of the form, i've edited my code too, that should work now. – stranded Mar 11 '14 at 07:26
  • Your code didn't work :/ the dragger works but there isn't a mousemove event on the form using that component. – stemsmit Mar 11 '14 at 07:35