使用 C#。
我正在尝试移动Form
没有标题栏的 a。
我找到了一篇关于它的文章:http: //www.codeproject.com/KB/cs/csharpmovewindow.aspx
只要我不设置FormBorderStyle
为None
.
有没有办法让它与这个属性集一起工作None
?
使用 C#。
我正在尝试移动Form
没有标题栏的 a。
我找到了一篇关于它的文章: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(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;
}
首先,我们必须通过使用命名空间来使用互操作服务
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(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);
}