SetWindowPos function Explanation

Source: Internet
Author: User

Winapi:setwindowpos-changing the position and state of the window

SetWindowPos (
Hwnd:hwnd; {Window Handle}
Hwndinsertafter:hwnd; {Window's Z-order}
X, Y:integer; Location
CX, Cy:integer; Size
Uflags:uint {Options}
): BOOL;

Hwndinsertafter parameter Optional values:
hwnd_top = 0; {in front}
Hwnd_bottom = 1; {at the back}
Hwnd_topmost = HWND (-1); {in front, in front of any top window}
Hwnd_notopmost = HWND (-2); {in front, after the other top window}

Uflags parameter Optional values:
Swp_nosize = 1; {Ignore CX, CY, keep size}
Swp_nomove = 2; {Ignore X, Y, do not change position}
Swp_nozorder = 4; {Ignore Hwndinsertafter, hold Z-order}
Swp_noredraw = 8; {Do not redraw}
Swp_noactivate = $; {Do not activate}
swp_framechanged = $ $; {Forces a wm_nccalcsize message to be sent, typically only when the size is changed}
Swp_showwindow = $ $; {Display window}
Swp_hidewindow = $; {Hide Window}

--------------------------------------------------------------------------------------

SetWindowPos ()
function function: This function changes the size, position and Z-order of a subwindow, pop-up window top-level window. Child windows, pop-up windows, and top-level windows are sorted according to the order they appear on the screen, top-level windows are set to the highest levels, and are set to the first window of the z-order.
Function prototype: BOOL SetWindowPos (hwn hwnd,hwnd hwndlnsertafter,int x,int y,int cx,int cy,unit. Flags);
Parameters:
HWnd: Window handle.
Hwndlnsertafter: The window handle in the Z-order in front of the placed window. The parameter must be a window handle, or one of the following values:
Hwnd_bottom: Places the window at the bottom of the z-order. If the parameter HWND identifies a top-level window, the window loses its top-level position and is placed at the bottom of the other window.
Hwnd_dottopmost: Places the window above all non-top-level windows (that is, after all top-level windows). This flag does not work if the window is already a non-top-level window.
Hwnd_top: Places the window at the top of the z-order.
Hwnd_topmost: Places the window above all non-top-level windows. The top-level position is maintained even if the window is not activated.

To see how this parameter is used, see the Description section.
X: Specifies the left edge of the window's new location in customer coordinates.
Y: Specifies the top boundary of the window's new position in customer coordinates.
CX: Specifies the new width of the window in pixels.
CY: Specifies the new height of the window in pixels.

Uflags: The window size and positioning of the logo. This parameter can be a combination of the following values:
Swp_asncwindowpos: If the calling process does not own a window, the system makes a request to the thread that owns the window. This prevents the calling thread from deadlock when other threads are processing the demand.
Swp_defererase: Prevents wm_syncpaint messages from being generated.
Swp_drawframe: Draw a border around the window (defined in the window class description).
Swp_framechanged: Sends a WM_NCCALCSIZE message to the window, even if the window size does not change. If this flag is not specified, the wm_nccalcsize is sent only if the window size is changed.
Swp_hidewindow; hide the window.
Swp_noactivate: The window is not activated. If the flag is not set, the window is activated and set to the top of other topmost or non-superlative groups (set according to the parameter hwndlnsertafter).
Swp_nocopybits: Clears all content from the customer area. If this flag is not set, valid content for the customer area is saved and copied back to the customer area after the window size is updated and relocated.
Swp_nomove: maintains the current position (ignoring the x and Y parameters).
Swp_noownerzorder: Does not change the position of the owner window in the z-order.
Swp_noredraw: Do not repaint the changed content. If this flag is set, no redraw action occurs. Applies to both customer and non-client areas (including title bars and scroll bars) and any parts of the parent window that are exposed as the window moves back. If this flag is set, the application must explicitly invalidate the window and redraw any part of the window and the part that the parent window needs to redraw.
Swp_noreposition; same as the Swp_noownerzorder logo.
Swp_nosendchanging: Prevents the window from receiving wm_windowposchanging messages.
Swp_nosize: Maintains current dimensions (ignores CX and CY parameters).
Swp_nozorder: Maintains the current z-order (ignores the Hwndlnsertafter parameter).
Swp_showwindow: Display window.

Return value: If the function succeeds, the return value is not 0, and if the function fails, the return value is zero. To get more error messages, call the GetLastError function.
Note: If the Swp_showwindow and Swp_hidewindow flags are set, the window cannot be moved and resized. If you use Setwindowloog to change some of the window's data, you must call the function SetWindowPos to make a real change. Use the following combination flags: swp_nomoveiswp_nosizeiswp_framechanged.
There are two ways to set the window to the topmost window: one is to set the parameter hwndlnsertafter to hwnd_topmost and ensure that the Swp_nozorder flag is not set, and the other is to set the position of the window in the z-order so that it is on top of other existing windows. When a window is placed at the top-level window



Dword_prt SetWindowPos (HWND hwnd,hwnd hinsertafter,int x,int y,int cx,int cy,uint nflag)

The function of SetWindowPos function is to move a window in three-dimensional space, using it, you can change the position of a window, even can be changed on the z axis (the z-axis determines the relationship between a window and other windows), you can also change the size of the window. To implement the topmost type of window, we simply call the function, place the window in front of all the windows and keep it in front forever.


Table 1 parameter interpretation of the SetWindowPos function
Parameter name parameters meaning
HWND handle of the window to be moved (can be used with the form's HWND property)
Hwndinsertafter about how to place a window on the z-axis (see table 2)
X equals the Left property of the window
Y corresponds to the top property of the window
CX corresponds to the right property of a window
Cy corresponds to the bottom property of the window
Wflags about how to move a window (see table 3 for details)
Table 2 possible values and meanings of hwndinsertafter parameters
Possible value function of Hwndinsertafter
The handle of a window is placed behind the window specified by the handle
Hwnd_bottom (1) Place the window at the end of the z-axis, that is, behind all the Windows
Hwnd_top (0) Place the window in front of the z-axis, which is the front of all windows
Hwnd_topmost (-1) makes the window a "topmost" type of window, this type
Window is always in front of other windows, so it is closed
Hwnd_notopmost (-2) place the window on all "topmost" types
Behind the window, in front of other types of Windows
Table 3 possible values and meanings of wflags parameters
Possible values for the wflags parameter function
Swp_drawframe (&H20) moves the window and then re-draws the window and all of its contents
Swp_hidewindow (&h80) hides the window, the window is hidden and neither appears on the screen nor does it appear on either
On the service bar, but it is still active
Swp_noactivate (&h10) window does not activate after the window is moved, of course, if the window before moving is
The exception that is active
Swp_nocopybits (&h100) When the window is moved, do not redraw any of its contents
Swp_nomove (&H2) does not move the window (that is, ignores the x and Y parameters)
Swp_nosize (&H1) does not change window size (that is, omit CX and CY parameters)
Swp_noredraw (&H8) does not remove the image of the window in its former position
From the screen. In other words,leave behind a ghost image
of the window in its old position
Swp_nozorder (&H4) does not change the window to listen to the z-axis position (that is, ignoring the hwndinsertafter parameter)
Swp_showwindow (&h40) display window (must have been used before Swp_hidewindow
Hide window)


Comments:

If Swp_showwindow or Swp_hidewindow is specified, the window cannot be moved or resized.
All coordinates of the child window are client area coordinates (relative to the upper-left corner of the client area of the parent window).
A window can be a topmost window, you can set the Hwndinsertafter parameter to Hwnd_topmost and ensure that the Swp_nozorder flag is not set, or by setting its window position in the z-axis direction, So that it is on top of any existing topmost window. When a non-topmost window is set to topmost, then its own window will become, however its owners do not change.

If the swp_noactivate and Swp_nozorder flags are not specified (meaning that when the application requires that the window be activated while changing its position in the z-axis direction), the Hwndinsertafter is used only in the following cases:
The 1.hwnd_topmost and Hwnd_notopmost flags are not specified in the Hwndinsertafter.
The window specified by the 2.HWND handle is not the active window.

Inactive Windows cannot be activated if an application does not adjust an inactive window to the top of the z-axis. The application can change the position of an active window in the z direction without restriction, it can activate a window and move it to the top of the topmost or non-topmost window.
If a topmost window is relocated to the bottom of the z-axis (hwnd_bottom), or behind any non-topmost window, it is not in the topmost window. When a topmost window becomes a non-topmost window, Its owner and the window it owns will also become non-topmost windows.

A non-topmost window can have a topmost window, but not the other way around. Any window (for example: a dialog box) is owned by a topmost window, and it makes itself a topmost window, ensuring that all owned windows are on top of their owners.
If an application is not in the foreground, but to be a foreground program, it should call the SetForegroundWindow function.

See:
MoveWindow, SetActiveWindow, SetForegroundWindow

Conditions of Use:
Windows NT: Yes.
Windows 95: Yes.
Win32s: Yes.
Header file: defined in Winuser.h.
Static Library: User32.lib.

Example code:
Move to the upper-left corner of the screen:
SetWindowPos (m_hwnd,null,0,0,0,0,swp_nosize);
Make it a topmost window and move to the upper-left corner of the screen:
SetWindowPos (m_hwnd,hwnd_topmost,0,0,0,0,swp_nosize);
Display window:
SetWindowPos (m_hwnd,null,0,0,0,0,swp_showwindow| swp_nosize| Swp_nomove);
Hide window:
SetWindowPos (m_hwnd,null,0,0,0,0,swp_hidewindow| swp_nosize| Swp_nomove);
To change the window size:
CRect Newrect;
:: GetWindowRect (M_hwnd,&newrect);
:: SetWindowPos (M_hwnd,null,0,0,newrect.width ()/2,newrect.height ()/2,swp_nomove);
Swp_nocopybits = $ A; {Drop client area}
Swp_noownerzorder = $; {Ignore Hwndinsertafter, do not change the Z-sequence owner}
swp_nosendchanging = $; {Do not emit wm_windowposchanging messages}
Swp_drawframe = swp_framechanged; {Draw Border}
Swp_noreposition = Swp_noownerzorder; {}
Swp_defererase = $2000; {prevents wm_syncpaint messages from being generated}
Swp_asyncwindowpos = $4000; {If the calling process does not own a window, the system will make a request to the thread that owns the window}

SetWindowPos functions in detail

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.