visual ventana net mover formulario bordes borde arrastrar c# mousemove formborderstyle

c# - net - mover ventana sin bordes vb



¿Cómo mover un formulario de Windows cuando su propiedad FormBorderStyle está establecida en Ninguna? (5)

Aquí es la mejor manera que he encontrado. Es una "forma de .NET", sin usar WndProc. Solo tiene que manejar los eventos MouseDown, MouseMove y MouseUp de las superficies que desea que se puedan arrastrar.

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; }

Utilizando C #.
Estoy tratando de mover un Form sin su barra de título.
Encontré un artículo al respecto en: http://www.codeproject.com/KB/cs/csharpmovewindow.aspx

Funciona siempre y cuando no establezca FormBorderStyle como None .

¿Hay alguna manera de hacer que funcione con esta propiedad establecida como None ?


Hace un tiempo tuve la misma pregunta y mientras buscaba la respuesta, encontré el siguiente código (no recuerdo el sitio web) y esto es lo que hago:

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; }


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


Primero tendremos que usar los servicios de interoperabilidad usando el espacio de nombres como

using System.Runtime.InteropServices;

Lo siguiente sería definir los mensajes que se encargarán de mover el formulario. Tendremos estas como variables miembro de la clase

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();

y finalmente escribiremos el código para enviar el mensaje cada vez que el usuario presione el botón del mouse. El formulario se reposicionará según el movimiento del mouse si el usuario mantiene presionado el botón del mouse.

private void Form1_MouseDown(object sender, MouseEventArgs e) { ReleaseCapture(); SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0); }

Remítase a este enlace de forma arrastra

Créditos a rahul-rajat-singh


Sé que esta pregunta tiene más de un año, pero estaba buscando para tratar de recordar cómo lo he hecho en el pasado. Entonces, para la referencia de cualquier otra persona, la forma más rápida y menos compleja que la del enlace anterior es anular la función 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); }

Esto permitirá mover cualquier formulario haciendo clic y arrastrando dentro del área del cliente.