我之前在这个论坛上寻找帮助解决这个问题。基本上我是从类中动态创建一个表单,我希望在屏幕上没有标题栏可拖动。我遇到的代码是:
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool ReleaseCapture();
private void window_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
然而,当我尝试编译时,我收到以下错误:
错误1名称'处理'在当前不存在 上下文C:\ Users \ xxxxxx \ Documents \ Visual Studio 2013 \ Projects \ practiceProgressBar2 \ practiceProgressBar2 \ Notifications.cs 109 29 practiceProgressBar2
有没有人知道我做错了什么,因为我整天都在处理这个问题
答案 0 :(得分:1)
变化:
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
要:
SendMessage(((Form)sender).Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
如果您有其他控件,例如Labels,指向同一个处理程序,那么您可以使用它来代替拖动标签时拖动它:
SendMessage(((Control)sender).FindForm().Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
答案 1 :(得分:0)
请尝试改写表单的WndProc
方法。
public const int HTCAPTION = 0x2;
public const int WM_NCHITTEST = 0x84;
public const int HTCLIENT = 1;
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_NCHITTEST)
{
if (m.Result.ToInt32() == HTCLIENT)
m.Result = (IntPtr)HTCAPTION;
}
}