そのFormBorderStyleプロパティをNoneに設定されている場合、Windowsフォームを移動する方法?

StackOverflow https://stackoverflow.com/questions/1241812

  •  12-09-2019
  •  | 
  •  

質問

C#を使用。
私は、そのタイトルバーなしFormを移動しようとしています。
http://www.codeproject.com/KB/cs:私は上でそれについての記事を見つけました/csharpmovewindow.aspxする

これは、限り、私はFormBorderStyleとしてNoneを設定しないように動作します。

それはNoneとして設定し、このプロパティを使用して動作させるための方法はありますか?

役に立ちましたか?

解決

私はこの質問が終わっ歳ですけど、私は過去にそれをやったか思い出そうと探していました。だから、誰の参考のために、迅速かつそれほど複雑な方法は、その後、上記のリンクはのWndProc関数をオーバーライドすることです。

/*
Constants in Windows API
0x84 = WM_NCHITTEST - Mouse Capture Test
0x1 = HTCLIENT - Application Client Area
0x2 = HTCAPTION - Application Title Bar

This function intercepts all the commands sent to the application. 
It checks to see of the message is a mouse click in the application. 
It passes the action to the base action by default. It reassigns 
the action to the title bar if it occured in the client area
to allow the drag and move behavior.
*/

protected override void WndProc(ref Message m)
{
    switch(m.Msg)
    {
        case 0x84:
            base.WndProc(ref m);
            if ((int)m.Result == 0x1)
                m.Result = (IntPtr)0x2;
            return;
    }

    base.WndProc(ref m);
}

これはどのような形態でクリックし、クライアント領域内のドラッグして移動することができるようになります。

他のヒント

ここでは、私が見つけた最良の方法です。これは、WndProcメソッドを使用してwihout、「NETの道」です。あなたはちょうどあなたがドラッグ可能にする表面のMouseDownイベント、のMouseMoveとのMouseUpイベントを処理する必要があります。

private bool dragging = false;
private Point dragCursorPoint;
private Point dragFormPoint;

private void FormMain_MouseDown(object sender, MouseEventArgs e)
{
    dragging = true;
    dragCursorPoint = Cursor.Position;
    dragFormPoint = this.Location;
}

private void FormMain_MouseMove(object sender, MouseEventArgs e)
{
    if (dragging)
    {
        Point dif = Point.Subtract(Cursor.Position, new Size(dragCursorPoint));
        this.Location = Point.Add(dragFormPoint, new Size(dif));
    }
}

private void FormMain_MouseUp(object sender, MouseEventArgs e)
{
    dragging = false;
}

私はしばらく前に同じ質問があったと答えを探しながら、私は、以下の(ウェブサイトを覚えていない)。ここでのコードは、私が何をすべきかです見つけます:

    Point mousedownpoint = Point.Empty;

    private void Form_MouseDown(object sender, MouseEventArgs e)
    {
        mousedownpoint = new Point(e.X, e.Y);
    }

    private void Form_MouseMove(object sender, MouseEventArgs e)
    {

        if (mousedownpoint.IsEmpty)
            return;
        Form f = sender as Form;
        f.Location = new Point(f.Location.X + (e.X - mousedownpoint.X), f.Location.Y + (e.Y - mousedownpoint.Y));

    }

    private void Form_MouseUp(object sender, MouseEventArgs e)
    {
        mousedownpoint = Point.Empty;
    }

まず、

として名前空間を使用して相互運用サービスを使用する必要があります
using System.Runtime.InteropServices;

次のことは、フォームを移動するの世話をするメッセージを定義することです。私たちは、クラスのメンバ変数としてこれらを持つことになります。

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)
{
    ReleaseCapture();
    SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0); 
}

このリンクを参照してください。ドラッグ可能フォーム

にクレジットラーフル・rajat-シン

ポイントmousedownpoint = Point.Empty;

    private void Form_MouseDown(object sender, MouseEventArgs e)
    {
        mousedownpoint = new Point(e.X, e.Y);
    }

    private void Form_MouseMove(object sender, MouseEventArgs e)
    {

        if (mousedownpoint.IsEmpty)
            return;
        Form f = sender as Form;
        f.Location = new Point(f.Location.X + (e.X - mousedownpoint.X), f.Location.Y + (e.Y - mousedownpoint.Y));

    }

    private void Form_MouseUp(object sender, MouseEventArgs e)
    {
        mousedownpoint = Point.Empty;
    }

    private void panel1_MouseDown(object sender, MouseEventArgs e)
    {
        Form_MouseDown(this, e);
    }

    private void panel1_MouseUp(object sender, MouseEventArgs e)
    {
        Form_MouseUp(this, e);
    }

    private void panel1_MouseMove(object sender, MouseEventArgs e)
    {
        Form_MouseMove(this, e);
    }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top