Come spostare un Windows Form quando la sua proprietà FormBorderStyle è impostato su Nessuno?
-
12-09-2019 - |
Domanda
con C #.
Sto cercando di spostare un Form
senza la barra del titolo.
Ho trovato un articolo su di esso sopra: http://www.codeproject.com/KB/cs /csharpmovewindow.aspx
Funziona finché non metto FormBorderStyle
come None
.
C'è un modo per farlo funzionare con questa proprietà impostata come None
?
Soluzione
So che questa domanda è più di un anno, ma ero alla ricerca cercando di ricordare come ho fatto in passato. Quindi, per chiunque altro di riferimento, il modo più rapido e meno complesso quindi il link qui sopra è quello di ignorare la funzione 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);
}
In questo modo qualsiasi forma di spostare facendo clic e trascinando all'interno dell'area client.
Altri suggerimenti
Qui è il modo migliore che ho trovato. Si tratta di una "via NET", wihout usando WndProc. Non vi resta che gestire gli eventi MouseDown, MouseMove e MouseUp delle superfici che si desidera essere trascinabile.
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;
}
Ho avuto la stessa domanda qualche tempo fa e durante la ricerca per la risposta che ho trovato il codice qui sotto (non ricordo il sito web) e Ecco quello che faccio:
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;
}
In primo luogo dovremo utilizzare i servizi di interoperabilità utilizzando lo spazio come
using System.Runtime.InteropServices;
Il passo successivo sarebbe quello di definire i messaggi che si prenderà cura di spostare il modulo. Avremo questi come variabili membro di classe
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();
e alla fine avremo il scrivere il codice per inviare il messaggio ogni volta che l'utente preme il pulsante del mouse. Il modulo sarà riposizionato secondo il movimento del mouse se l'utente mantenere premuto il pulsante del mouse.
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
Fare riferimento questo link Dragable formare
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);
}