2013-06-03 28 views
0

我有一个小方法,让我拖动没有边框/标题栏的一种形式:我可以将表单的MouseDown事件传递给自定义类吗?

public const int WM_NCLBUTTONDOWN = 0xA1; 
    public const int HT_CAPTION = 0x2; 
    [DllImportAttribute("user32.dll")] 
    public static extern int SendMessage(IntPtr hWnd, 
        int Msg, int wParam, int lParam); 
    [DllImportAttribute("user32.dll")] 
    public static extern bool ReleaseCapture(); 





private void Form1_MouseDown(object sender, MouseEventArgs e) 
      { 
       if (e.Button == MouseButtons.Left) 
       { 
        ReleaseCapture(); 
        SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0); 
       } 
      } 

,并在设计师

this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.RemoteControl_MouseDown); 

目前,我必须将它添加到在应用程序中的每个表单,这是烦人的。因此我试图将它添加到自定义类,这是我使用的样式的形式:

public class ThemeManager 
{ 

    // Required for drag/drop 
    public const int WM_NCLBUTTONDOWN = 0xA1; 
    public const int HT_CAPTION = 0x2; 
    [DllImportAttribute("user32.dll")] 
    public static extern int SendMessage(IntPtr hWnd, 
        int Msg, int wParam, int lParam); 
    [DllImportAttribute("user32.dll")] 
    public static extern bool ReleaseCapture(); 

    public void setTheme(Form sender) 
    { 
     sender.MouseDown += new System.Windows.Forms.MouseEventHandler(MyForm_MouseDown); 
    } 

    private void MyForm_MouseDown(object sender, MouseEventArgs e) 
    { 
     Form f = sender as Form; 
     if (e.Button == MouseButtons.Left) 
     { 
      ReleaseCapture(); 
      SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0); 
     } 
    } 
} 

麻烦的是,当我这样说,我得到以下错误:

The name 'Handle' does not exist in the current context 

我怎样才能使它工作?

+1

也许f.Handle? – Mark

回答

4

难道不应该是:

SendMessage(f.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0); 
+0

确实应该!直接发布后直接实现。干杯兄弟。 – Ben

+0

(将在9分钟内标记为正确;-)) – Ben

相关问题