我有一個小方法,讓我拖動沒有邊框/標題欄的一種形式:我可以將表單的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
我怎樣才能使它工作?
也許f.Handle? – Mark