Frage zur Windows.Forms
-
In C++ gibts die Moeglichkeit, dass man sagt : Wenn klick auf Fenster(egal wo) dann verschiebe Fenster
PostMessage(hwnd, WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM( 5, 5));
Gibts sowas in C# ?
Ich hab folgendes versucht hat aber nicht geklappt
using System.Runtime.InteropServices; // ... [DllImport("user32.dll")] public static extern bool PostMessage(IntPtr hWnd, int Msg, int wParam, uint lParam); public Form1() { InitializeComponent(); MoveWindow(); } private void MoveWindow() { IntPtr thisWindow = this.Handle; PostMessage(thisWindow, 0x00A1, 2, (uint)MakeLong(5, 5)); } public static int MakeLong(int nLow, int nHigh) { return (int)(nLow + nHigh * 65536); }
klappt aber gar nicht. Hat das wer schonmal hingekriegt ?
-
Dein Weg mit der Post-Sonstwas-Methode geht nicht
Es gibt zwei verschiedene Wege um das Fenster zu verschieben:
- Die erste Möglichkeit besteht darin MouseMove, MouseUp, MouseDown zu verwenden:
private Point m_start; private void Form1_MouseDown(object sender, MouseEventArgs e) { if (e.Button != MouseButtons.Left) return; Capture = true; m_start = new Point(e.X, e.Y); } private void Form1_MouseMove(object sender, MouseEventArgs e) { if (!Capture) return; Location = new Point(Location.X + e.X - m_start.X, Location.Y + e.Y - m_start.Y); } private void Form1_MouseUp(object sender, MouseEventArgs e) { if (e.Button != MouseButtons.Left) return; Capture = false; }
- die andrere Möglickeit wäre WndProc zu überschreiben:
protected override void WndProc(ref Message msg) { base.WndProc(ref msg); const int WM_NCHITTEST = 0x84; const int HTCLIENT = 0x01; if (msg.Msg == WM_NCHITTEST && HTCLIENT == msg.Result.ToInt32()) msg.Result = new IntPtr(2); }
Grundsätzlich ist, meiner Meinung nach, die erste ein wenig besser ...
Mit freundlichen Grüßen
Rhombicosidodecahedron