0

I have a small method that allows me to drag a form that has no border / title bar:

 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);
                }
            }

and in the designer:

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

At the moment I have to add it to each form in the application, which is annoying. As such I have tried adding it to a custom class, which I use to style the forms:

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);
        }
    }
}

Trouble is, when I do it this way, I get the following error:

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

How can I make it work?

4

1 回答 1

4

那不应该是:

SendMessage(f.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
于 2013-06-03T11:27:25.363 回答