- MoveWindow function (winuser.h)
- Syntax
- Parameters
- Return value
- Remarks
- Mouse Movement
- Capturing Mouse Movement Outside the Window
- Example: Drawing Circles
- Left Button Down
- Mouse Move
- Left Button Up
- How to move desktop Windows without a mouse?
- WM_MOUSEMOVE message
- Parameters
- Return value
- Remarks
- Move a Window with the Keyboard in Windows 7/8/10
- Method 1 – Incremental Move
- Method 2 – Windows Snap
- Method 3 – Multi-Monitor Setups
MoveWindow function (winuser.h)
Changes the position and dimensions of the specified window. For a top-level window, the position and dimensions are relative to the upper-left corner of the screen. For a child window, they are relative to the upper-left corner of the parent window’s client area.
Syntax
Parameters
A handle to the window.
The new position of the left side of the window.
The new position of the top of the window.
The new width of the window.
The new height of the window.
Indicates whether the window is to be repainted. If this parameter is TRUE, the window receives a message. If the parameter is FALSE, no repainting of any kind occurs. This applies to the client area, the nonclient area (including the title bar and scroll bars), and any part of the parent window uncovered as a result of moving a child window.
Return value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
Remarks
If the bRepaint parameter is TRUE, the system sends the WM_PAINT message to the window procedure immediately after moving the window (that is, the MoveWindow function calls the UpdateWindow function). If bRepaint is FALSE, the application must explicitly invalidate or redraw any parts of the window and parent window that need redrawing.
Mouse Movement
When the mouse moves, Windows posts a WM_MOUSEMOVE message. By default, WM_MOUSEMOVE goes to the window that contains the cursor. You can override this behavior by capturing the mouse, which is described in the next section.
The WM_MOUSEMOVE message contains the same parameters as the messages for mouse clicks. The lowest 16 bits of lParam contain the x-coordinate, and the next 16 bits contain the y-coordinate. Use the GET_X_LPARAM and GET_Y_LPARAM macros to unpack the coordinates from lParam. The wParam parameter contains a bitwise OR of flags, indicating the state of the other mouse buttons plus the SHIFT and CTRL keys. The following code gets the mouse coordinates from lParam.
Remember that these coordinates are in pixels, not device-independent pixels (DIPs). Later in this topic, we will look at code that converts between the two units.
A window can also receive a WM_MOUSEMOVE message if the position of the cursor changes relative to the window. For example, if the cursor is positioned over a window, and the user hides the window, the window receives WM_MOUSEMOVE messages even if the mouse did not move. One consequence of this behavior is that the mouse coordinates might not change between WM_MOUSEMOVE messages.
Capturing Mouse Movement Outside the Window
By default, a window stops receiving WM_MOUSEMOVE messages if the mouse moves past the edge of the client area. But for some operations, you might need to track the mouse position beyond this point. For example, a drawing program might enable the user to drag the selection rectangle beyond the edge of the window, as shown in the following diagram.
To receive mouse-move messages past the edge of the window, call the SetCapture function. After this function is called, the window will continue to receive WM_MOUSEMOVE messages for as long as the user holds at least one mouse button down, even if the mouse moves outside the window. The capture window must be the foreground window, and only one window can be the capture window at a time. To release mouse capture, call the ReleaseCapture function.
- When the user presses the left mouse button, call SetCapture to start capturing the mouse.
- Respond to mouse-move messages.
- When the user releases the left mouse button, call ReleaseCapture.
Example: Drawing Circles
Let’s extend the Circle program from Module 3 by enabling the user to draw a circle with the mouse. Start with the Direct2D Circle Sample program. We will modify the code in this sample to add simple drawing. First, add a new member variable to the MainWindow class.
This variable stores the mouse-down position while the user is dragging the mouse. In the MainWindow constructor, initialize the ellipse and ptMouse variables.
Remove the body of the MainWindow::CalculateLayout method; it’s not required for this example.
Next, declare message handlers for the left-button down, left-button up, and mouse-move messages.
Mouse coordinates are given in physical pixels, but Direct2D expects device-independent pixels (DIPs). To handle high-DPI settings correctly, you must translate the pixel coordinates into DIPs. For more discussion about DPI, see DPI and Device-Independent Pixels. The following code shows a helper class that converts pixels into DIPs.
Call DPIScale::Initialize in your WM_CREATE handler, after you create the Direct2D factory object.
To get the mouse coordinates in DIPs from the mouse messages, do the following:
- Use the GET_X_LPARAM and GET_Y_LPARAM macros to get the pixel coordinates. These macros are defined in WindowsX.h, so remember to include that header in your project.
- Call DPIScale::PixelsToDipsX and DPIScale::PixelsToDipsY to convert pixels to DIPs.
Now add the message handlers to your window procedure.
Finally, implement the message handlers themselves.
Left Button Down
For the left-button down message, do the following:
- Call SetCapture to begin capturing the mouse.
- Store the position of the mouse click in the ptMouse variable. This position defines the upper left corner of the bounding box for the ellipse.
- Reset the ellipse structure.
- Call InvalidateRect. This function forces the window to be repainted.
Mouse Move
For the mouse-move message, check whether the left mouse button is down. If it is, recalculate the ellipse and repaint the window. In Direct2D, an ellipse is defined by the center point and x- and y-radii. We want to draw an ellipse that fits the bounding box defined by the mouse-down point (ptMouse) and the current cursor position (x, y), so a bit of arithmetic is needed to find the width, height, and position of the ellipse.
The following code recalculates the ellipse and then calls InvalidateRect to repaint the window.
Left Button Up
For the left-button-up message, simply call ReleaseCapture to release the mouse capture.
How to move desktop Windows without a mouse?
the parameters on my desktop have changed. my window for Outlook open outside the parameter.
You can use an old school keystroke technique to regain control of a window that is outside your current desktop.
- Close all programs so that you can start with a clean desktop.
- Open Outlook from the same way you always do (desktop shortcut, pinned from taskbar or start menu, etc)
- Since it is Outlook, just leave it alone for a minute so it completes its startup Send/Receive actions.
- Press ALT+ together, then release and press m. You are now in Move mode.
- Press one of the four directional arrow keys (up, down, right, or left) repeatedly to move the Outlook application window back to the visible area of your desktop.
- When you can see the Outlook application window again (or at least the top left section of the title bar), press to exit Move mode.
If you want to practice this method, open any program onto a blank clean desktop (e.g. Notepad) and start at step 4.
- «Have you tried turning it off and then on again?» — Roy from ‘The IT Crowd’ (Emmy award-winning British comedy)
21 people found this reply helpful
Was this reply helpful?
Sorry this didn’t help.
Great! Thanks for your feedback.
How satisfied are you with this reply?
Thanks for your feedback, it helps us improve the site.
WM_MOUSEMOVE message
Posted to a window when the cursor moves. If the mouse is not captured, the message is posted to the window that contains the cursor. Otherwise, the message is posted to the window that has captured the mouse.
A window receives this message through its WindowProc function.
Parameters
Indicates whether various virtual keys are down. This parameter can be one or more of the following values.
Value | Meaning |
---|---|
MK_CONTROL 0x0008 | The CTRL key is down. |
MK_LBUTTON 0x0001 | The left mouse button is down. |
MK_MBUTTON 0x0010 | The middle mouse button is down. |
MK_RBUTTON 0x0002 | The right mouse button is down. |
MK_SHIFT 0x0004 | The SHIFT key is down. |
MK_XBUTTON1 0x0020 | The first X button is down. |
MK_XBUTTON2 0x0040 | The second X button is down. |
The low-order word specifies the x-coordinate of the cursor. The coordinate is relative to the upper-left corner of the client area.
The high-order word specifies the y-coordinate of the cursor. The coordinate is relative to the upper-left corner of the client area.
Return value
If an application processes this message, it should return zero.
Remarks
Use the following code to obtain the horizontal and vertical position:
As noted above, the x-coordinate is in the low-order short of the return value; the y-coordinate is in the high-order short (both represent signed values because they can take negative values on systems with multiple monitors). If the return value is assigned to a variable, you can use the MAKEPOINTS macro to obtain a POINTS structure from the return value. You can also use the GET_X_LPARAM or GET_Y_LPARAM macro to extract the x- or y-coordinate.
Do not use the LOWORD or HIWORD macros to extract the x- and y- coordinates of the cursor position because these macros return incorrect results on systems with multiple monitors. Systems with multiple monitors can have negative x- and y- coordinates, and LOWORD and HIWORD treat the coordinates as unsigned quantities.
Move a Window with the Keyboard in Windows 7/8/10
Comes in handy once in a blue moon
So for those of you who do not like to use a mouse, it would be pretty cool if you could move a window using just the keyboard in Windows 7 right? Well, there are a couple of ways to do this and even though you may not need to do it very often, it does come in handy on the rare occasion!
In this article, I’ll tell you how to move a window in small increments to the exact position you want, how to snap a window to the left or right and how to move a window between monitors on a multi-monitor setup, all by just using the keyboard!
Method 1 – Incremental Move
For exact positioning, you first need to click on the window. Now this is obviously going to only work for windows that are not fully maximized. If it’s fully maximized, there is really nowhere to move the window.
Step 1: Either click on the window or if you want to use the keyboard, press ALT+TAB and make the window you want to move active.
Step 2: Now go ahead and press ALT+SPACEBAR and you’ll see a small menu appear in the window.
Step 3: Now press M, which will basically choose the Move option in the menu.
Step 4: Now use the arrow keys on your keyboard to move the window to the new position that you desire.
Step 5: Press the Enter key to get out of the move mode. Also, if you want to have the window go back to the original position before you started the move, press the Esc key instead.
Method 2 – Windows Snap
Windows has a nifty feature that lets you snap windows to the left-hand or right-hand side of the screen. If you drag a window to the right or left, it will automatically resize and snap to the side.
To do this using the keyboard, press the Windows Key + the right or left arrow. Make sure to hold down the Windows key while pressing the left and right arrow keys. It’s actually pretty neat and much faster than dragging the window around the screen.
Method 3 – Multi-Monitor Setups
Lastly, for moving between multiple monitors, all you have to do is add SHIFT to the above key combo, so it would simply be Windows Key + Shift + right or left arrow.
For the power users who really want to control their windows, moving may not be enough. So here’s a few other shortcuts that could come in handy if you are left with nothing but a keyboard and Windows:
End – Will display the bottom of the active window (in case you need to scroll using the keyboard)
Home – Will display the top of the active window
F11 – Will either maximize or minimize the active window
Ctrl + Tab – If you have a window with tabs, then this will move you forward through the tabs
Ctrl + Shift + Tab – Will move you back through the tabs
Windows Key + Shift + Up Arrow – This will stretch a window to the top and bottom of the screen.
Founder of Help Desk Geek and managing editor. He began blogging in 2007 and quit his job in 2010 to blog full-time. He has over 15 years of industry experience in IT and holds several technical certifications. Read Aseem’s Full Bio