Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move a Windows Form when its FormBorderStyle property is set to None? [duplicate]

Using C#.
I am trying to move a Form without its title bar.
I found an article about it on: http://www.codeproject.com/KB/cs/csharpmovewindow.aspx

It works as long as I do not set FormBorderStyle as None.

Is there a way to make it work with this property set as None?

like image 455
Moon Avatar asked Aug 06 '09 22:08

Moon


People also ask

Which property is used to change the position of a Windows Form?

The position of the form is determined by the Location property. The form is positioned at the Windows default location and is resized to the default size determined by Windows. The form is positioned at the Windows default location and isn't resized.

What is the use of Startposition property of Windows Forms?

This property enables you to set the starting position of the form when it is displayed at run time. The form's position can be specified manually by setting the Location property or use the default location specified by Windows.


Video Answer


2 Answers

I know this question is over a year old, but I was searching trying to remember how I've done it in the past. So for anyone else's reference, the quickest and less complex way then the above link is to override the WndProc function.

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

This will allow any form to be moved by clicking and dragging within the client area.

like image 112
LizB Avatar answered Sep 20 '22 12:09

LizB


Here it is the best way I have found. It is a ".NET way", wihout using WndProc. You just have to handle the MouseDown, MouseMove and MouseUp events of the surfaces you want to be draggable.

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;
}
like image 43
cprcrack Avatar answered Sep 18 '22 12:09

cprcrack