Пример #1
0
        public void MoveWindow_AfterMouse()
        {
            // 1- get a handle to the foreground window.
              // 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!
              // Loop ends when the user clicks the left mouse button.

              // 1-
              IntPtr hWnd = WinAPIs.GetForegroundWindow();
              //IntPtr hWnd = WinAPIs.FindWindow(null, "Form1");

              // 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);
              }
        }
Пример #2
0
 public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);