Как переместить Windows Form, когда его свойство FormBorderStyle не установлено ни на Один? [дубликат]

18
задан J0e3gan 17 December 2014 в 16:54
поделиться

2 ответа

Я знаю, что этому вопросу больше года, но я искал, пытаясь вспомнить, как я это делал в прошлом. Итак, для справки, самый быстрый и менее сложный способ, чем указанная выше ссылка, - это переопределить функцию 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);
}

Это позволит перемещать любую форму щелчком и перетаскиванием в клиентской области.

40
ответ дан 30 November 2019 в 05:42
поделиться

Вот лучший способ, который я нашел. Это "способ .NET", без использования WndProc. Вам просто нужно обработать события 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;
}
38
ответ дан 30 November 2019 в 05:42
поделиться
Другие вопросы по тегам:

Похожие вопросы: