An application can move a window by using the WinSetWindowPos function and specifying the SWP_MOVE constant. The function changes the position of the window to the specified position. The position is always given in coordinates relative to the parent window.
The code fragment in the following figure moves the window to the position (10,10):
HWND hwnd; WinSetWindowPos( hwnd, /* Window handle */ NULL, /* Not used for moving and sizing */ 10, 10, /* New position */ 0, 0, /* Not used for moving */ SWP_MOVE); /* Move window */
An application can set the size of a window by using the WinSetWindowPos function and specifying the SWP_SIZE constant. WinSetWindowPos changes the width and height of the window to the specified width and height.
An application can combine moving and sizing in a single function call, as shown in the following figure.
HWND hwnd; WinSetWindowPos( hwnd, /* Window handle */ NULL, /* Not used for moving and sizing */ 10, 10, /* New position */ 200, 200, /* Width and height */ SWP_MOVE | SWP_SIZE); /* Move and size window. */
An application can retrieve the current size and position of a window by using the WinQueryWindowPos function. This function copies the current information to an SWP structure.
The code fragment in the following figure uses the current size and position to change the height of the window, leaving the width and position unchanged.
HWND hwnd; SWP swp; WinQueryWindowPos(hwnd, &swp); WinSetWindowPos( hwnd, /* Window handle */ NULL, /* Not used for moving and sizing */ 0, 0, /* Not used for sizing */ swp.cx, /* Current width */ swp.cy + 200, /* New height */ SWP_SIZE); /* Change the size. */
An application also can move and change the size of several windows at once by using the WinSetMultWindowPos function. This function takes an array of SWP structures. Each structure specifies the window to be moved or changed.
An application can move and size a window even if it is not visible, although the user is not able to see the effects of the moving and sizing until the window is visible.