none
Move form without titlebar

Answers

  • Hi,

    Two ways. First:

    //API functions to move the form
    public const int WM_NCLBUTTONDOWN = 0xA1;
    public const int HTCAPTION = 0x2;
    [DllImportAttribute("user32.dll")]
    public static extern bool ReleaseCapture();
    [DllImportAttribute("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

    And in your mouse down event of the form:

    public void Form1_MouseDown(object sender, MouseEventArgs e)
    {
    //If the left mouse is pressed, release form for movement
    if (e.Button == MouseButtons.Left)
    {
    ReleaseCapture();
    SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
    }

     

    Second:

    protected override void WndProc(ref Message m)
    {
                base.WndProc(ref m);

                int WM_NCHITTEST = 0x84;
                if (m.Msg == WM_NCHITTEST)
                {
                    int HTCLIENT = 1;
                    int HTCAPTION = 2;
                    if (m.Result.ToInt32() == HTCLIENT)
                        m.Result = (IntPtr)HTCAPTION;
                }
    }

    I use the second. Its much simplier and doesn't use any API's. Though the first method is the one that I was using during my VB6 days...

     

    cheers,

    Paul June A. Domag

    Friday, September 22, 2006 8:40 PM
    Moderator

All replies

  • Declare 2 Member variables in Form:

    private Point lastPoint;

    private bool mouseDown;

    Add Event handlers to 3 events of the form i.e MouseDown, MouseMove and MouseUP

    In MouseDown Handler put this code:

    this.mouseDown = true;

    this.lastPoint = new Point(e.X, e.Y);

    In MouseMove Handler put this code:

    if(this.mouseDown)

         this.Location = new Point(this.Left - (this.lastPoint.X - e.X), this.Top - (this.lastPoint.Y - e.Y));

    In Last Put this In MouseUP:

    this.mouseDown = false;

    That's it! Cheers ;)

    Friday, September 22, 2006 7:54 PM
  • Hi,

    Two ways. First:

    //API functions to move the form
    public const int WM_NCLBUTTONDOWN = 0xA1;
    public const int HTCAPTION = 0x2;
    [DllImportAttribute("user32.dll")]
    public static extern bool ReleaseCapture();
    [DllImportAttribute("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

    And in your mouse down event of the form:

    public void Form1_MouseDown(object sender, MouseEventArgs e)
    {
    //If the left mouse is pressed, release form for movement
    if (e.Button == MouseButtons.Left)
    {
    ReleaseCapture();
    SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
    }

     

    Second:

    protected override void WndProc(ref Message m)
    {
                base.WndProc(ref m);

                int WM_NCHITTEST = 0x84;
                if (m.Msg == WM_NCHITTEST)
                {
                    int HTCLIENT = 1;
                    int HTCAPTION = 2;
                    if (m.Result.ToInt32() == HTCLIENT)
                        m.Result = (IntPtr)HTCAPTION;
                }
    }

    I use the second. Its much simplier and doesn't use any API's. Though the first method is the one that I was using during my VB6 days...

     

    cheers,

    Paul June A. Domag

    Friday, September 22, 2006 8:40 PM
    Moderator
  • Thanks, this will help me!
    Saturday, September 23, 2006 7:21 PM
  •  

    RizwanSharp that was such a great help. You rule!!!
    Friday, September 14, 2007 12:17 PM