Déplacer une fenêtre sur le clavier + souris (comme Linux ALT + souris vers le bas)

Simple, je veux déplacer une fenêtre en appuyant sur ALT + MOUSE, comme linux OS (ALT + glisser).

Il est possible de passer une API win32 (déplacer une API) aux fenêtres intéressées en cliquant dessus?

J’ai un Windows Services qui accrochent la clé (bouton ALT en particulier). Lorsque la touche Alt est enfoncée et qu’un événement de souris enfoncé est vérifié, je souhaite déplacer la fenêtre en cliquant n’importe où, pas seulement sur la barre de titre!

Actuellement, je déplace mes fenêtres de formulaire de la manière suivante:

using System.Runtime.InteropServices; [DllImport( "user32.dll", CharSet = CharSet.Auto, SetLastError = false )] static extern IntPtr SendMessage( IntPtr hWnd, uint Msg, int wParam, int lParam ); [DllImportAtsortingbute( "user32.dll", CharSet = CharSet.Auto, SetLastError = false )] public static extern bool ReleaseCapture(); private void Form1_MouseDown( object sender, MouseEventArgs e ) { ReleaseCapture(); SendMessage( this.Handle, 0xa1, 0x2, 0 ); } 

Comment puis-je obtenir le gestionnaire de fenêtres spécifiques en cliquant sur et après avoir appelé SendMessage () dessus?

C’est possible?

Vous pouvez le faire en récupérant le message WM_NCHITTEST envoyé par Windows pour voir quelle zone de la fenêtre a été cliquée. Vous pouvez le tromper en retournant HTCAPTION et celui-ci effectuera consciencieusement les actions de la souris que vous auriez normalement en cliquant sur le libellé d’une fenêtre. Y compris le déplacement de la fenêtre. Collez ce code dans votre formulaire:

  protected override void WndProc(ref Message m) { base.WndProc(ref m); // Trap WM_NCHITTEST when the ALT key is down if (m.Msg == 0x84 && (Control.ModifierKeys == Keys.Alt)) { // Translate HTCLIENT to HTCAPTION if (m.Result == (IntPtr)1) m.Result = (IntPtr)2; } } 

J’ai travaillé cela moi-même, je suis arrivé à quelque chose d’intéressant de mes propres calculs, j’ai parfaitement fonctionné pour moi, pour n’importe quelle fenêtre (n’importe quelle fenêtre au premier plan active). Un peu long, mais très facile à comprendre si vous suivez les commentaires, espérez que cela aidera. La façon dont cela fonctionne est que vous appuyez sur un certain raccourci clavier enregistré, comme Ctrl + Alt + M, et que la souris rest au centre Dans une fenêtre active, vous déplacez la souris, les fenêtres la suivent, appuyez à nouveau sur la combinaison SAME, pour libérer, aucun clic de souris ni rien.

 public void MoveWindow_AfterMouse() { // 1- get a handle to the foreground window (or any window that you want to move). // 2- set the mouse pos to the window's center. // 3- let the window move with the mouse in a loop, such that: // win(x) = mouse(x) - win(width)/2 // win(y) = mouse(y) - win(height)/2 // This is because the origin (point of rendering) of the window, is at its top-left corner and NOT its center! // 1- IntPtr hWnd = WinAPIs.GetForegroundWindow(); // 2- Then: // first we need to get the x, y to the center of the window. // to do this, we have to know the width/height of the window. // to do this, we could use GetWindowRect which will give us the coords of the bottom right and upper left corners of the window, // with some math, we could deduce the width/height of the window. // after we do that, we simply set the x, y coords of the mouse to that center. RECT wndRect = new RECT(); WinAPIs.GetWindowRect(hWnd, out wndRect); int wndWidth = wndRect.right - wndRect.left; int wndHeight = wndRect.bottom - wndRect.top; // cuz the more you go down, the more y value increases. Point wndCenter = new Point(wndWidth / 2, wndHeight / 2); // this is the center of the window relative to itself. WinAPIs.ClientToScreen(hWnd, out wndCenter); // this will make its center relative to the screen coords. WinAPIs.SetCursorPos(wndCenter.X, wndCenter.Y); // 3- Moving :))) while (true) { Point cursorPos = new Point(); WinAPIs.GetCursorPos(out cursorPos); int xOffset = cursorPos.X - wndWidth / 2; int yOffset = cursorPos.Y - wndHeight / 2; WinAPIs.MoveWindow(hWnd, xOffset, yOffset, wndWidth, wndHeight, true); Thread.Sleep(25); } } 

Et maintenant:

 int moveCommandToggle = 0; protected override void WndProc(ref Message m) { if (m.Msg == 0x0312) { int keyID = m.WParam.ToInt32(); if(keyID == some_key_combo_you_registered_for_the_moving) { if (moveCommandToggle++ % 2 == 0) { mover = new Thread(() => MoveWindow_AfterMouse()); mover.Start(); } else mover.Abort(); } } } 

Si vous vous interrogez sur RECT:

  public struct RECT { public int left; // xCoor of upper left corner. public int top; // yCoor of upper left corner. public int right; // xCoor of lower right corner. public int bottom; // yCoor of lower right corner. }; 

WinAPIs était juste une classe statique dans laquelle j’ai fait mes DllImports.