2009-10-22 61 views
5

我使用以下代码通过单击并拖动窗体本身来拖动无边界窗体。它可以工作,但它不适用于单击并拖动窗体上的控件。我需要在单击某些控件时才能拖动它,而不是其他按钮 - 按标签拖动,但不按按钮和文本框。我该怎么做?C#:如何从窗体拖动一个和它的控件?

protected override void WndProc(ref Message m) 
{ 
    base.WndProc(ref m); 

    const int WM_NCHITTEST = 0x84; 
    const int HTCLIENT = 0x1; 
    const int HTCAPTION = 0x2; 

    if (m.Msg == WM_NCHITTEST && (int)m.Result == HTCLIENT) 
     m.Result = (IntPtr)HTCAPTION; 
} 

回答

3

其实,我找到了解决方案here

public const int WM_NCLBUTTONDOWN = 0xA1; 
public const int HTCAPTION = 0x2; 

[DllImport("User32.dll")] 
public static extern bool ReleaseCapture(); 
[DllImport("User32.dll")] 
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam); 

// Paste the below code in the your label control MouseDown event 
if (e.Button == MouseButtons.Left) 
{ 
    ReleaseCapture(); 
    SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0); 
} 

它的工作原理。

而且,在上述我的代码,如果大小调整是需要的,if语句应改为

 if (m.Msg == WM_NCHITTEST) 
      if ((int)m.Result == HTCLIENT) 
       m.Result = (IntPtr)HTCAPTION; 
1

使用Spy ++分析哪些控件正在接收什么Windows消息,然后您将知道需要捕获的内容。

没有深入研究你的代码,我在想象主窗口上的子控件正在接收消息而不是表单,并且你想对其中的一些进行响应。

相关问题