2010-06-23 3 views
1

Simple, je veux déplacer une fenêtre en appuyant sur ALT + MOUSE, comme linux os (ALT + glisser).Déplacez une fenêtre sur keypress + mouse (comme linux ALT + souris vers le bas)

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

J'ai un services de Windows que la touche de crochet a pressé (le bouton d'ALT en particulier). Lorsque la touche ALT est pressée et qu'un la souris vers le bas l'événement est vérifiée, je veux déplacer la fenêtre en cliquant n'importe où, pas seulement sur la barre de titre!

Actuellement je déplace mes fenêtres forment ainsi:

using System.Runtime.InteropServices; 

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)] 
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam); 
[DllImportAttribute("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 des fenêtres poignée des fenêtres spécifiques en cliquant sur et après appel SendMessage() là-dessus?

C'est possible?

+1

s'il vous plaît don Répétez les tags likg "C#" dans le titre. C'est ce que les tags sont pour. –

+0

@john: ok, la prochaine fois. – elp

Répondre

1

Vous pouvez le faire en piégeant le message WM_NCHITTEST que Windows envoie pour voir quelle zone de la fenêtre a été cliquée. Vous pouvez le tromper en retournant HTCAPTION et il effectuera consciencieusement les actions de la souris que vous obtiendriez normalement en cliquant sur la légende d'une fenêtre. Y compris en déplaçant la fenêtre. Coller 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; 
     } 
    } 
+0

euh ... ça va, mais ça ne bouge que ma forme. J'ai besoin de déplacer les autres fenêtres ... – elp

+0

Mettez ce code dans une classe de base. Héritez vos autres formulaires de cette classe de base. –

+0

non, désolé ... mais je veux déplacer les fenêtres du système comme calc, bloc-notes, navigateur, toutes les fenêtres ... comme ceci: http://www.howtogeek.com/howto/windows-vista/get-the-linux -altwindow-drag-fonctionnalité-dans-windows / – elp

0

J'ai travaillé ceci moi-même, est venu avec quelque chose d'intéressant de mes propres calculs, a parfaitement fonctionné pour moi, pour une fenêtre (une fenêtre de premier plan actif). Kinda long, mais vraiment facile à comprendre si vous suivez les commentaires, j'espère que cela aide :) La façon dont cela fonctionne, c'est que vous appuyez sur une certaine combinaison de touches enregistrées, comme Ctrl + Alt + M et la souris va coller Au centre d'une fenêtre active, vous déplacez la souris, les fenêtres la suivent, appuyez à nouveau sur la même commande, pour libérer, pas besoin de clics de souris ou quoi que ce soit.

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 que je faisais mes DllImports dans

Questions connexes