Search code examples
c#movemenustripborderless

Move window without border


How do I move a window that does not have a border. There is no empty space on the application, all that is available is a webbrowser and a menustrip. I would like the users to be able to move the window by dragging the menu strip. How do I code this? I have tried a few code blocks I have found online, but none of them worked.


Solution

  • This Code Project article should help you accomplish this. I've used this myself with no problems. This is the jist of it:

    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();
    
    private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {     
        if (e.Button == MouseButtons.Left)
        {
            ReleaseCapture();
            SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
        }
    }
    

    This will basically "trick" the window manager into thinking that it is grabbing the title bar of the winform.

    To apply it to your project, just use the MouseDown event from the MenuStrip.