C#&.NET Winform桌面程序中Label如何实现同时绑定鼠标拖动窗体和点击事件呢?
1.64K 次浏览
窗体上有个Label,既想实现:
- 按住Label拖拽移动整个窗体
- 又想实现Label单击后执行某个方法
拖拽是通过Label的MouseDown
事件进行的
但是在MouseDown
里执行formDrag()
方法后MouseUp
,MouseClick
,Click
事件都没反应了,代码如下:
private void label1_MouseDown(object sender, MouseEventArgs e)
{
Console.WriteLine("MouseDown");
formDrag(this.Handle);//窗体拖拽
}
private void label1_Click(object sender, EventArgs e)
{ //没反应
Console.WriteLine("Click");
anotherWork();//单击执行的某个方法
}
private void label1_MouseClick(object sender, MouseEventArgs e)
{ //没反应
Console.WriteLine("MouseClick");
}
private void label1_MouseUp(object sender, MouseEventArgs e)
{ //没反应
Console.WriteLine("MouseUp");
}
//////拖拽//////
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_MOVE = 0xF010;
public const int HTCAPTION = 0x0002;
//拖动
public static void formDrag(IntPtr intp)
{
//拖动窗体
ReleaseCapture();
SendMessage(intp, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}
本问题来自:inform练习遇到的一个问题