10

I have this System.Windows.Forms.Panel that I want to enable so that if the user click and drags the mouse drags the window around to.

Can I do this? Do i have to implement multiple events?

Jason94
  • 13,320
  • 37
  • 106
  • 184

6 Answers6

51

The Solution that works best for me is using unmanaged code, which gives you smooth window movements unlike the answer posted by HatSoft.

3 small steps to drag your window on Panel movement

using System.Runtime.InteropServices;

add these six lines inside your class

public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;

[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();

and your MouseMove event on Panel should look like this

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

posted it a little late :) , who knows we might need it again in future.

Bravo
  • 3,341
  • 4
  • 28
  • 43
  • e.g. `const int WM_NCLBUTTONDOWN = 0x00A1;` – Rotem Oct 21 '13 at 11:13
  • oh yes, i missed that, Thanks for pointing that out. :) updated my answer – Bravo Oct 21 '13 at 11:27
  • 3
    This should be chosen as the accepted answer. Works like a charm. The current accepted answer has glitches but gives a nudge towards the approach to be taken while solving this problem. – Abhirath Mahipal Sep 28 '16 at 05:01
  • The accepted answer causes issues when using multiple screens. This should be the accepted answer. Easy to implement and is very smooth – Mark Jun 28 '18 at 15:44
  • This is really perfect solution. other solution that is accepted is flickering. this is smooth. Also you may give this functionality to the labels and other controls on panel. in my case there was no space on panel to drag. so I give the labels same function from Events tab. – Alp Altunel Apr 12 '20 at 21:03
6

You can achieve it by using the MouseMove Event of the panel

Example should be something like this (Sorry have not tested it)

private void panel1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        this.Location = new Point(Cursor.Position.X + e.X , Cursor.Position.Y + e.Y);
    }
}
HatSoft
  • 11,077
  • 3
  • 28
  • 43
1

You might want to take a look at this component that I pasted here:

http://pastebin.com/5ufJmuay

It is a component that you will be able to drop on a form, and then drag the form by dragging inside it.

Daniel Mošmondor
  • 19,718
  • 12
  • 58
  • 99
0

Currently Set For A Panel. VS C# Just Messing About Seems to work for me as I wanted Sets application left-top corner to mouse position while left-click is pressed.

 public form1()
    {
        InitializeComponent();
        this.panel2.MouseMove += new MouseEventHandler(panel2_MouseMove);
    }
    
    public const int WM_NCLBUTTONDOWN = 0xA1;
    public const int HT_CAPTION = 0x2;

    [DllImportAttribute("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
    
    private void panel2_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            
            Point loc1 = MousePosition;
            this.Location = loc1;
        }
    }
0

Hey Hope this works for you

  1. First you have to put a panel on your form and dock it to top
  2. Then add these lines of codes

using System.Runtime.InteropServices;

public partial class Main_FM : Form
{

  [DllImport("user32.dll", EntryPoint = "ReleaseCapture")]
    private extern static void ReleaseCapture();

  [DllImport("user32.dll", EntryPoint = "SendMessage")]
    private extern static void SendMessge(System.IntPtr hwnd, int wmsg, int wparam, int lparam);

}
  1. The create the MouseDown event on the panel and add these codes:
 private void Top_PNL_MouseDown(object sender, MouseEventArgs e)
  {
      ReleaseCapture();
      SendMessge(this.Handle, 0x112, 0xf012, 0);
  }
Atrin Noori
  • 311
  • 3
  • 12
-1

Bravo's code works perfectly fine but I couldn't get this working until i explicitly made MouseMove event enable in panel's->properties->event section of my panel i wanted to move.

Halim
  • 2,090
  • 2
  • 16
  • 9