How to move a Windows Form when its FormBorderStyle property is set to None?

女生的网名这么多〃 提交于 2019-11-27 18:50:39

I know this question is over a year old, but I was searching trying to remember how I've done it in the past. So for anyone else's reference, the quickest and less complex way then the above link is to override the WndProc function.

/*
Constants in Windows API
0x84 = WM_NCHITTEST - Mouse Capture Test
0x1 = HTCLIENT - Application Client Area
0x2 = HTCAPTION - Application Title Bar

This function intercepts all the commands sent to the application. 
It checks to see of the message is a mouse click in the application. 
It passes the action to the base action by default. It reassigns 
the action to the title bar if it occured in the client area
to allow the drag and move behavior.
*/

protected override void WndProc(ref Message m)
{
    switch(m.Msg)
    {
        case 0x84:
            base.WndProc(ref m);
            if ((int)m.Result == 0x1)
                m.Result = (IntPtr)0x2;
            return;
    }

    base.WndProc(ref m);
}

This will allow any form to be moved by clicking and dragging within the client area.

Here it is the best way I have found. It is a ".NET way", wihout using WndProc. You just have to handle the MouseDown, MouseMove and MouseUp events of the surfaces you want to be draggable.

private bool dragging = false;
private Point dragCursorPoint;
private Point dragFormPoint;

private void FormMain_MouseDown(object sender, MouseEventArgs e)
{
    dragging = true;
    dragCursorPoint = Cursor.Position;
    dragFormPoint = this.Location;
}

private void FormMain_MouseMove(object sender, MouseEventArgs e)
{
    if (dragging)
    {
        Point dif = Point.Subtract(Cursor.Position, new Size(dragCursorPoint));
        this.Location = Point.Add(dragFormPoint, new Size(dif));
    }
}

private void FormMain_MouseUp(object sender, MouseEventArgs e)
{
    dragging = false;
}

I had the same question a while ago and while searching for the answer I found the code below(don't remember the website) and Here is what I do:

    Point mousedownpoint = Point.Empty;

    private void Form_MouseDown(object sender, MouseEventArgs e)
    {
        mousedownpoint = new Point(e.X, e.Y);
    }

    private void Form_MouseMove(object sender, MouseEventArgs e)
    {

        if (mousedownpoint.IsEmpty)
            return;
        Form f = sender as Form;
        f.Location = new Point(f.Location.X + (e.X - mousedownpoint.X), f.Location.Y + (e.Y - mousedownpoint.Y));

    }

    private void Form_MouseUp(object sender, MouseEventArgs e)
    {
        mousedownpoint = Point.Empty;
    }

First we will have to use the interop services by using the namespace as

using System.Runtime.InteropServices;

The next thing would be to define the messages that will take care of moving the form. We will have these as class member variables

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 finally we will write the code to send the message whenever the user presses the mouse button. The form will be repositioned as per the mouse movement if the user keep the mouse button pressed.

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

Refer this link Dragable form

Credits to rahul-rajat-singh

Point mousedownpoint = Point.Empty;

    private void Form_MouseDown(object sender, MouseEventArgs e)
    {
        mousedownpoint = new Point(e.X, e.Y);
    }

    private void Form_MouseMove(object sender, MouseEventArgs e)
    {

        if (mousedownpoint.IsEmpty)
            return;
        Form f = sender as Form;
        f.Location = new Point(f.Location.X + (e.X - mousedownpoint.X), f.Location.Y + (e.Y - mousedownpoint.Y));

    }

    private void Form_MouseUp(object sender, MouseEventArgs e)
    {
        mousedownpoint = Point.Empty;
    }

    private void panel1_MouseDown(object sender, MouseEventArgs e)
    {
        Form_MouseDown(this, e);
    }

    private void panel1_MouseUp(object sender, MouseEventArgs e)
    {
        Form_MouseUp(this, e);
    }

    private void panel1_MouseMove(object sender, MouseEventArgs e)
    {
        Form_MouseMove(this, e);
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!