移动窗口没有边框

如何移动没有边框的窗口。 应用程序上没有空白区域,所有可用的内容都是webbrowser和menustrip。 我希望用户能够通过拖动菜单条来移动窗口。 我该如何编码呢? 我尝试了一些我在网上找到的代码块,但都没有。

此代码项目文章应该可以帮助您实现此目的。 我自己也没用过这个问题。 这是它的主旨:

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

这将基本上“欺骗”窗口管理器认为它正在抓取winform的标题栏。

要将它应用于您的项目,只需使用MenuStrip中的MouseDown事件即可。

这是.Net方式

  private bool dragging = false; private Point dragCursorPoint; private Point dragFormPoint; private void Form1_MouseDown(object sender, MouseEventArgs e) { dragging = true; dragCursorPoint = Cursor.Position; dragFormPoint = this.Location; } private void Form1_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 Form1_MouseUp(object sender, MouseEventArgs e) { dragging = false; } 

而已。

你可以假装你的menustrip,例如使用带有标签的面板。 然后您可以手动处理:当用户单击标签时,将打开一个弹出菜单,当用户拖动标签时,窗口将移动。 但我建议不要使用这些解决方法,因为它不是标准的GUI行为,您可能会让用户感到困惑。

我没有尝试过,但如果您可以在菜单栏上处理“OnMouseDown”和“onMouseUp”事件:

  • 鼠标按下 – 根据鼠标移动移动窗口
  • 在鼠标向上或鼠标移开时停止跟踪鼠标移动

如果您使用的是Panel,则必须在中添加

YourForm.Designer.cs

 this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseDown); 

这个在

YourForm.cs

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

Mbithi Kioko走在正确的轨道上,但我会这样做。

  bool dragging = false; int xOffset = 0; int yOffset = 0; private void Form1_MouseDown(object sender, MouseEventArgs e) { dragging = true; xOffset = Cursor.Position.X - this.Location.X; yOffset = Cursor.Position.Y - this.Location.Y; } private void Form1_MouseMove(object sender, MouseEventArgs e) { if (dragging) { this.Location = new Point(Cursor.Position.X - xOffset, Cursor.Position.Y - yOffset); this.Update(); } } private void Form1_MouseUp(object sender, MouseEventArgs e) { dragging = false; } 

我不得不使用System.Runtime.InteropServices.DllImportAttribute – 只是想我会发表评论并让大家都知道。

只需将起点放入2D数组中,如下所示:

 public partial class mainForm : Form { //Global variables for Moving a Borderless Form private bool dragging = false; private Point startPoint = new Point(0, 0); public mainForm() { InitializeComponent(); } private void mainForm_MouseDown(object sender, MouseEventArgs e) { dragging = true; startPoint = new Point(eX, eY); } private void mainForm_MouseUp(object sender, MouseEventArgs e) { dragging = false; } private void mainForm_MouseMove(object sender, MouseEventArgs e) { if (dragging) { Point p = PointToScreen(e.Location); Location = new Point(pX - this.startPoint.X, pY - this.startPoint.Y); } } }