beta.blog

Programming

C# – Move a window without titlebar/frame

by on Jun.06, 2010, under Programming

By setting a form’s FrameBorder property to None, the default titlebar and border of a frame gets removed.
But how can you move a window without a titlebar then?

Well, there are many solutions for this question, but it took me quite long to find one, which would also work for my current Windows 7 installation, and here’s how to do it :

1.) Insert the following code into your program:

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

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

private void titelLeiste_MouseDown(object sender, MouseEventArgs e)
{
   ReleaseCapture();
   SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}

2.) Set the MouseDown event of your form to titelLeiste_MouseDown

Now compile the project and you should be able to move your frame, despite the fact it has no titlebar.

3 Comments more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!