C# 当 FormBorderStyle 属性设置为 None 时,如何移动 Windows 窗体?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1241812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to move a Windows Form when its FormBorderStyle property is set to None?
提问by Moon
Using C#.
I am trying to move a Form
without its title bar.
I found an article about it on: http://www.codeproject.com/KB/cs/csharpmovewindow.aspx
使用 C#。
我正在尝试移动Form
没有标题栏的 a 。
我在以下位置找到了一篇关于它的文章:http: //www.codeproject.com/KB/cs/csharpmovewindow.aspx
It works as long as I do not set FormBorderStyle
as None
.
只要我不设置FormBorderStyle
为None
.
Is there a way to make it work with this property set as None
?
有没有办法让它与这个属性设置为None
?
采纳答案by LizB
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.
我知道这个问题已经有一年多了,但我一直在寻找,试图记住我过去是如何做到的。因此,对于其他任何人的参考,最快和最简单的方法是上面的链接覆盖 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);
}
This will allow any form to be moved by clicking and dragging within the client area.
这将允许通过在客户区中单击和拖动来移动任何表单。
回答by cprcrack
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.
这是我找到的最好的方法。这是一种“.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;
}
回答by thejustv
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
回答by Ryu
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;
}
回答by aya ali rayan
Point mousedownpoint = Point.Empty;
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);
}