4

Thanks you for your previous answers for my question. you can see following link.

How to minimize and maximize in C#.Net?

Now i'm facing another problem. When i changed my form's borderstyle to none,i can't move form like the real form. Its stable and can't move anywhere.

In Windows form's normal borderstyle can move anywhere. But i want to move like that in borderstyle's none property. How can i do that? Please let me know if you can. Thanks you for your time. :)

Community
  • 1
  • 1
Seven
  • 411
  • 2
  • 5
  • 8

3 Answers3

10
public class AppFormBase : Form
{   
    public Point downPoint = Point.Empty;

    protected override void OnLoad(EventArgs e)
    {
        if (FormBorderStyle == System.Windows.Forms.FormBorderStyle.None)
        {
            MouseDown += new MouseEventHandler(AppFormBase_MouseDown);
            MouseMove += new MouseEventHandler(AppFormBase_MouseMove);
            MouseUp   += new MouseEventHandler(AppFormBase_MouseUp);
        }

        base.OnLoad(e);
    }

    private void AppFormBase_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
            downPoint = new Point(e.X, e.Y);
    }
    private void AppFormBase_MouseMove(object sender, MouseEventArgs e)
    {
        if (downPoint != Point.Empty)
            Location = new Point(Left + e.X - downPoint.X, Top + e.Y, - downPoint.Y);
    }
    private void AppFormBase_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
            downPoint = Point.Empty;
    }
}
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Aaron Anodide
  • 16,906
  • 15
  • 62
  • 121
  • Nice answer. @Seven - if the "movable" form contains controls that you want to be able to grab with mouse as well, then you also need to handle those controls MouseMove/MouseDown/MouseUp events with the handlers Gabriel wrote. – Steve Wong Sep 02 '11 at 17:36
4

Take a look at this tutorial: http://www.codeproject.com/KB/cs/csharpmovewindow.aspx.

Here's the gist of it:

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, System.Windows.Forms.MouseEventArgs e)
{     
    if (e.Button == MouseButtons.Left)
    {
        ReleaseCapture();
        SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
    }
}
James Hill
  • 60,353
  • 20
  • 145
  • 161
1
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
    Capture = false;
    Message msg = Message.Create(Handle, WM_NCLBUTTONDOWN, (IntPtr)HT_CAPTION, IntPtr.Zero);
base.WndProc(ref msg);
}