0

ボーダー/タイトルバーのないフォームをドラッグできる小さなメソッドがあります:

 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 

どうすればそれを機能させることができますか?

4

1 に答える 1