Move window form without Titlebar in C#

I am going to describe two method of moving Window Form without title bar.

I Method – Using Windows API
Add following code in your .cs file

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

public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
public static extern bool ReleaseCapture();

Now write following code on MouseMove event of that control on which window form should move –

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

II Method : Using C# Code
Add following variables in your .cs file.

private bool mouseIsDown = false;
private Point firstPoint;

Now handle MouseMove, MouseUp, MouseDown event of that window control on which window form should move.
Code is as follow –

private void panel1_MouseMove(object sender, MouseEventArgs e)
            if (mouseIsDown)
                // Get the difference between the two points
                int xDiff = firstPoint.X - e.Location.X;
                int yDiff = firstPoint.Y - e.Location.Y;

                // Set the new point
                int x = this.Location.X - xDiff;
                int y = this.Location.Y - yDiff;
                this.Location = new Point(x, y);

        private void panel1_MouseDown_1(object sender, MouseEventArgs e)
            firstPoint = e.Location;
            mouseIsDown = true;

        private void panel1_MouseUp(object sender, MouseEventArgs e)
            mouseIsDown = false;

Note: Drawback of second method is, when cursor will move below taskbar then window form will also go below taskbar.

Posted in Uncategorized. 5 Comments »

5 Responses to “Move window form without Titlebar in C#”

  1. sachin Says:

    your code not working plz give another solution

    • RAM Says:

      Hello Sachin
      Both method are working fine. If you will use it in correct way as describe in post.
      use first method and do same on mouse move event as describe. Let me know if you face any problem in this.

  2. Steve Says:

    You have a couple of errors in your code.
    1) You need to register the Event Handlers or nothing will happen.
    this.panel1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseMove);
    this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseDown);
    this.panel1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseUp);

    2) this.Location is the “form” it should be panel1.Location

    • RAM Says:

      Hi Steve
      I did not write event handlers in code as i assumed it should understand bu users.
      And regarding your point 2, it should be form, not panel. Because our target is to move form not panel.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: