【发布时间】:2019-09-27 08:26:14
【问题描述】:
我正在尝试使用 c# 使我的面板控件可移动,但我的主窗体在拖动时会随着面板控件一起移动。有更好的代码吗?
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll")]
public static extern bool ReleaseCaptre();
public static void _MOVE_PANEL(IntPtr Handle, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCaptre();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
private void sub_inv_pcount_edit_MouseMove(object sender, MouseEventArgs e) //panel move
{
_MOVE_PANEL(Handle, e);
}
查看示例:see screenshot
【问题讨论】:
-
这就是 WM_NCLBUTTONDOWN 所做的,它假装你点击了一个主窗口的标题栏。哪个移动了整个shebang。谷歌“c# move panel with mouse”可以找到更好的代码,MSDN 论坛上的热门文章以及 Stackoverflow 看起来不错。