SetWindowPos usage of MFC

Source: Internet
Author: User

Transferred from: http://hi.baidu.com/max_new/blog/item/e2bbe607b1f127c57b8947c0.html

Many software, especially the desktop area is not very large software, usually provides a regular top-of-the-box features (perhaps the software is not so called, but the role is the same), its role is to keep the window is always on top of other windows, you can eliminate the frequent switch window action.

If you want to do this, there is an API that can be implemented: SetWindowPos, the declaration is this:

Private Declare Function setwindowpos Lib "user32" Alias "SetWindowPos" (ByVal hwnd as Long, ByVal Hwndinsertafter as Long , ByVal x As Long, ByVal y as Long, ByVal CX as Long, ByVal Cy as Long, ByVal wflags as long) as long

Although the parameters are many, the actual use is very simple. The HWND is the handle of the window, x, Y, CX, Cy are the x and Y coordinates of the window, and the width and height respectively. The hwndinsertafter is used to specify the z position (or z-order) of the window. If you are constantly in touch with 3D software, you know that Z represents depth. This parameter accepts 5 values: Hwnd_bottom, Hwnd_notopmost, Hwnd_top, Hwnd_topmost, or a handle to another window. Instead, wflags is used to specify additional options.

You can use it to change the position and size of the window, and it allows you to change the z position at the same time (of course, you can change the size and position of the form without using the API in VB). For example, to get the window back to the bottom, you can use this:

SetWindowPos Me.hwnd, Hwnd_bottom, 10&, 10&, 80&, 120&, 0&

Want to live in the top, just to change the hwnd_bottom to Hwnd_topmost, and Hwnd_notopmost is to cancel the top, Hwnd_top is to change the window Z position to the front. If this parameter passes a handle to another window, it changes the z position of the window to the bottom of the other window.

Very simple thing. But if you do it like above, do you want to calculate the position and size of the window simply by changing the z position? What's the last argument for? Wflags can let SetWindowPos ignore or perform some kind of behavior. Here is a partial:

Swp_drawframe and swp_framechanged: Force send wm_nccalcsize message to window

Swp_hidewindow: Hide Window

Swp_noactivate: Do not activate window

Swp_nomove: Keep current position (ignore x and Y)

Swp_noredraw: Window does not repaint automatically

Swp_nosize: Keep current size (ignoring CX and CY)

Swp_nozorder: Keeps the window in the current position of the list (ignores Hwndinsertafter)

Swp_showwindow: Display window

These parameters can be combined using an OR operation, so if you don't want to change the position and size of the window, you just need to pass the last parameter (Swp_nomove Or swp_nosize). As follows:

SetWindowPos Me.hwnd, Hwnd_topmost, 0&, 0&, 0&, 0&, Swp_nomove Or swp_nosize

The values for x, Y, CX, CY are ignored here. A combination of other values, you can try it yourself.
Well, this seemingly complex API has become clear, so it's the end of the last word.

The wm_nccalcsize message is sent when the client area size of the window is calculated, primarily to allow the program to recalculate the size of the client area after it receives the message. Let's start with whether or not it can be sent to the program like many messages starting with wm_, but the complexity of its use makes me choose to change the size of the form and change it back. When we change the size of the window, it is clear that it will recalculate the client area size to adjust the appearance. Since this function can force a wm_nccalcsize message to be sent, we should try it.

SetWindowPos Me.hwnd, 0&, 0&, 0&, 0&, 0&, swp_nosize or Swp_nozorder or Swp_nomove or SWP_FRAMECHANGED

In order not to change the window size, position, and Z-order (that is, to keep the window intact), I use Swp_nosize or Swp_nozorder or swp_nomove, let it ignore all previous arguments, and finally add an Or swp_framechanged is to have it recalculate the client area size. Put the above line in the previous sentence to change the style of the sentence below, then execute the program.

Explanation on MSDN:::

Its internal definition:
BOOL SetWindowPos (
HWND hwnd,//Handle of window
HWND Hwndinsertafter,//Placement-order handle
int X,//Horizontal position
int Y,//Vertical position
int CX,//width
int CY,//height
UINT uflags//window-positioning Flags
);

Parameter explanation:
HWnd
Identifies the window. Window handle

Hwndinsertafter Window Overlay Position
Identifies the window to precede the positioned window in the Z order. This parameter must is a window handle or one of the following values:
Value meaning
Set the value:
Hwnd_bottom =1 at the bottom of the "normal layer"
Places the window at the bottom of the Z order. If the HWnd parameter identifies a topmost window, the window loses its topmost status and are placed at the bottom of all Other windows.

hwnd_notopmost=-2 "normal layer" on all non-"normal" layers
Places the window above all non-topmost windows (that's, behind all topmost windows). This flag have no effect if the window is already a non-topmost window.

Hwnd_top =0 on the top floor of the "normal layer"
Places the window at the top of the Z order.

Hwnd_topmost = 1 "top layer" above the "normal layer" used
Places the window above all non-topmost windows. The window maintains its topmost position even then it is deactivated.

X window Left coordinate
Specifies the new position of the left side of the window.

Y coordinates on window
Specifies the new position of the top of the window.

CX window width (unit pixels)
Specifies the new width of the window, in pixels.

Cy Window height (unit pixels)
Specifies the new height of the window, in pixels.

Uflags Additional parameters
Specifies the window sizing and positioning flags. This parameter can be a combination of the following values:
Value meaning parameter values:

Swp_drawframe =0x0020 window with border
Draws a frame (defined in the window ' s class description) around the window.

swp_framechanged =0x0020 send Border Change message
Sends a WM_NCCALCSIZE message to the window, even if the window's size is not being changed. IF This flag was not specified, Wm_nccalcsize was sent only when the window's size is being changed.

Swp_hidewindow =0x0080 window Hidden
Hides the window.

Swp_noactivate =0x0010 do not activate the window
Does not activate the window. IF This flag isn't set, the window is activated and moved to the top of either the topmost or non-topmost group (Dependin G on the setting of the Hwndinsertafter parameter).

Swp_nocopybits =0x0100 do not keep copies of the display cache
Discards the entire contents of the client area. IF This flag isn't specified, the valid contents of the client area was saved and copied back to the client area after The window is sized or repositioned.

Swp_nomove =0x0002 non-removable window (ignore x, y parameter)
Retains the current position (ignores the X and Y parameters).

Swp_noownerzorder =0x0200 does not change the parent window stacking order
Does not change the owner window's position in the Z order.

Swp_noredraw =0x0008 do not repaint the window
Does not redraw changes. IF This flag was set, 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 the window being moved. When this flag was set, the application must explicitly invalidate or redraw any parts of the window and the parent window that Need redrawing.

Swp_noreposition =0x0200
Same as the Swp_noownerzorder flag.

Swp_nosendchanging =0x0400 does not accept messages for window position changes
Prevents the window from receiving the wm_windowposchanging message.

Swp_nosize =0x0001 window size unchanged (ignoring cx,cy parameters)
Retains the current size (ignores the CX and CY parameters).

Swp_nozorder =0x0004 does not change the stacking order (ignoring the hwndinsertafter parameter)
Retains the current Z order (ignores the Hwndinsertafter parameter).

Swp_showwindow =0x0040 Display window
Displays the window.

Note: If you want to use more than one of the above uflags parameters, simply add and assign several parameters to the uflags.

Return values
The function execution successfully returns a value other than 0 and does not return 0 successfully.

Remarks Special Instructions (not translated)
If the Swp_showwindow or Swp_hidewindow flag is set, the window cannot be moved or sized.

All coordinates-child windows is client coordinates (relative to the upper-left corner of the parent window ' s client Area).

A window can be made a topmost window either by setting the Hwndinsertafter parameter to hwnd_topmost and ensuring that th E swp_nozorder flag is no set, or by setting a window's position in the Z order so it's above any existing topmost Windows. When a non-topmost window was made topmost, its owned Windows was also made topmost. Its owners, however, is not changed.

If neither the swp_noactivate nor Swp_nozorder flag is specified (that's, when the application requests that a window be Simultaneously activated and its position in the Z order changed), the value specified in Hwndinsertafter was used only in The following circumstances:

· Neither the Hwnd_topmost nor Hwnd_notopmost flag is specified in Hwndinsertafter.

· The window identified by HWND was not the active window.

An application cannot activate a inactive window without also bringing it to the top of the the Z order. Applications can change an activated window's position in the Z order without restrictions, or it can activate a window an D then move it to the top of the topmost or non-topmost windows.

If A topmost window is repositioned to the bottom (hwnd_bottom) of the Z order or after any non-topmost windows, it is no l Onger topmost. When a topmost window was made Non-topmost, its owners and its owned Windows was also made Non-topmost windows.

A non-topmost window can own a topmost window, but the reverse cannot occur. Any window (for example, a dialog box) owned by a topmost window was itself made a topmost window, to ensure this all owned Windows stay above their owner.

If an application are not in the foreground, and should are in the foreground, it must call the SetForegroundWindow function .

The above is from MSDN.

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.