如何在FormBorderStyle属性设置为None时移动Windows窗体?

时间:2009-08-06 22:11:47

标签: c# mousemove formborderstyle

使用C#。
我试图在没有标题栏的情况下移动Form 我在http://www.codeproject.com/KB/cs/csharpmovewindow.aspx

上找到了一篇关于它的文章

只要我没有将FormBorderStyle设置为None,就可以正常工作。

有没有办法让它可以使用此属性设置为None

5 个答案:

答案 0 :(得分:40)

我知道这个问题已经过了一年多了,但我正在努力记住我过去是如何做到这一点的。因此,对于其他任何人的参考,上述链接的最快和最简单的方法是覆盖WndProc函数。

/*
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);
}

这将允许通过在客户区域内单击并拖动来移动任何表单。

答案 1 :(得分:38)

这是我找到的最佳方式。这是一种“.NET方式”,不使用WndProc。您只需要处理要拖拽的曲面的MouseDown,MouseMove和MouseUp事件。

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;
}

答案 2 :(得分:2)

前一段时间我有同样的问题,在搜索答案时,我发现下面的代码(不记得网站),这就是我的工作:

    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;
    }

答案 3 :(得分:0)

首先,我们必须使用命名空间作为

来使用互操作服务
using System.Runtime.InteropServices;

接下来的事情是定义将负责移动表单的消息。我们将这些作为类成员变量

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, MouseEventArgs e)
{
    ReleaseCapture();
    SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0); 
}

请参阅此链接Dragable form

rahul-rajat-singh

的信用

答案 4 :(得分:0)

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);
    }