Moo*_*oon 18 c# mousemove formborderstyle
使用C#.
我试图移动一个Form没有它的标题栏.
我发现了一篇关于它的文章:http://www.codeproject.com/KB/cs/csharpmovewindow.aspx
只要我没有设置FormBorderStyle,它就可以工作None.
有没有办法使它适用于此属性设置为None?
Liz*_*izB 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);
}
Run Code Online (Sandbox Code Playgroud)
这将允许通过在客户区域内单击并拖动来移动任何表单.
cpr*_*ack 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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
37090 次 |
| 最近记录: |