C# 我可以将窗体的MouseDown事件传递给自定义类吗?
我有一个小方法,允许我拖动没有边框/标题栏的表单:C# 我可以将窗体的MouseDown事件传递给自定义类吗?,c#,forms,C#,Forms,我有一个小方法,允许我拖动没有边框/标题栏的表单: 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 lPa
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
我怎样才能让它工作呢?这不应该是:
SendMessage(f.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
这不应该是:
SendMessage(f.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
确实应该!发帖后立即实现。干杯,伙计,真的应该!发帖后立即实现。干杯,伙计。