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

使用C#。
我试图移动一个没有标题栏的Form
我发现了一篇关于它的文章: http : //www.codeproject.com/KB/cs/csharpmovewindow.aspx

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

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

我知道这个问题已经有一年多了,但我一直在努力想起我过去是怎么做到的。 因此,对于其他任何人的参考,上述链接的最快和最简单的方法是覆盖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); } 

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

这是我找到的最佳方式。 这是一种“.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; } 

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

  Point mousedownpoint = Point.Empty; private void Form_MouseDown(object sender, MouseEventArgs e) { mousedownpoint = new Point(eX, eY); } private void Form_MouseMove(object sender, MouseEventArgs e) { if (mousedownpoint.IsEmpty) return; Form f = sender as Form; f.Location = new Point(f.Location.X + (eX - mousedownpoint.X), f.Location.Y + (eY - mousedownpoint.Y)); } private void Form_MouseUp(object sender, MouseEventArgs e) { mousedownpoint = Point.Empty; } 

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

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

请参阅此链接可拖动表单

rahul-rajat-singh的致谢

Point mousedownpoint = Point.Empty;

  private void Form_MouseDown(object sender, MouseEventArgs e) { mousedownpoint = new Point(eX, eY); } private void Form_MouseMove(object sender, MouseEventArgs e) { if (mousedownpoint.IsEmpty) return; Form f = sender as Form; f.Location = new Point(f.Location.X + (eX - mousedownpoint.X), f.Location.Y + (eY - 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); }