Here is some simple code to move a borderless form which I converted from my VB.NET code bank submission which can be found here.
You can place this code on the events of any control. Say if you would like to use an image as a title bar, then just place the code on the appropriate events of the image.Code:public class Form1 { //Declare the variables bool drag; int mousex; int mousey; private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { drag = true; //Sets the variable drag to true. mousex = System.Windows.Forms.Cursor.Position.X - this.Left; //Sets variable mousex mousey = System.Windows.Forms.Cursor.Position.Y - this.Top; //Sets variable mousey } private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { //If drag is set to true then move the form accordingly. if (drag) { this.Top = System.Windows.Forms.Cursor.Position.Y - mousey; this.Left = System.Windows.Forms.Cursor.Position.X - mousex; } } private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { drag = false; //Sets drag to false, so the form does not move according to the code in MouseMove } }
The code is pretty much self explanatory but if you have any questions just feel free to ask.
I wouldn't advise using this code if your form has a large number of controls on it as this will generate 'flickering' and possibly what might look like 'lag'.
Here is another means of doing it, by overriding WndProc:
Also a more direct approach using WinAPIs:Code:const int WM_NCHITTEST = 0x0084; const int HTCAPTION = 2; protected override void WndProc(ref Message m) { if (m.Msg == WM_NCHITTEST) { Point pt = this.PointToClient(new Point(m.LParam.ToInt32())); if (ClientRectangle.Contains(pt)) { m.Result = new IntPtr(HTCAPTION); return; } } base.WndProc (ref m); }
Code://Import API Functions public const int WM_NCLBUTTONDOWN = 0xA1; public const int HTCAPTION = 0x2; [DllImportAttribute("user32.dll")] public static extern bool ReleaseCapture(); [DllImportAttribute("user32.dll")] public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam); public void Form1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { ReleaseCapture(); SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0); }




Reply With Quote