Adjustwindowrect and SetWindowPos

Source: Internet
Author: User

These two functions are often used together, so put them together to say:

1 Adjustwindowrect

function function: The function calculates the size of the required window rectangle based on the size of the desired customer rectangle. The computed window rectangle can then be passed to the CreateWindow function, which creates a window of the desired size for the client area.

Function prototype: BOOL adjustwindowrect (LPRECT LPRECT, DWORD dwstyle,bool bmenu);

Parameters:

LpRect: A pointer to a RECT structure that contains the coordinates of the upper-left and lower-right corners of the desired customer area. When the function returns, the structure holds the coordinates of the upper-left and lower-right corners of the window for the desired customer area.

Dwstyle: Specifies the window style of the window that will be calculated dimensions.

Bmenu: Indicates whether the window has a menu.

Return value: If the function succeeds, the return value is not 0, and if the function fails, the return value is zero. Get the error message, see GetLastError.

Note: The customer rectangle refers to the smallest rectangle that contains exactly one customer area, and the window rectangle is the smallest rectangle that completely contains a window that contains the customer area and the non-client area.

The Adjustwindowrect function does not add extra space when two or more rows are pulled down under a menu bar.

2 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.

Check g 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 in the topmost window, all the windows that belong to it are the topmost window, and its owner's z-order does not change.

If both the hwnd_topmost and Hwnd_notopmost flags are not specified, that is, if the application requires the window to change its position in the Z-order while it is active, the value specified in the parameter hwndinsertafter is used only in the following conditions:

The Hwnd_notopmost and Hwnd_topmost flags are not set in the Hwndlnsertafter parameter.

The window identified by the HWND parameter is not an active window.

If a non-activated window is not set to the top of the z-order, the application cannot activate the window. The application can change the position of the activated window in the z-order without any restrictions, or activate a window and move it to the top of the topmost window or the top of a non-topmost window.

If a top-level window is relocated to the bottom of the Z-order (hwnd_bottom) or after any non-highest-order window, the window is no longer the topmost window. When a top-level window is set to a non-topmost level, its owner window and the owning window are non-topmost windows.

A non-topmost window can have a top-most window, but vice versa. Any window that belongs to the top-level window (such as a dialog box) itself is placed in the top-level window to ensure that all the owning windows are above their owners.

If the application is not in the foreground, but should be in the foreground, you should call the SetForegroundWindow function to set it.

Windows CE: If this is a visible top-level window and the SWP_NOACTIVATE flag is not specified, this function activates the window, if this is the current activation window, and the Swp_noactivate or Swp_hidewindow flag is specified, Another visible top-level window is activated.

When the swp_framechanged flag is specified in the nflags parameter in this function, WindowsCE redraw the entire non-client area of the window, which may change the size of the client area. This is also the only way to recalculate the client area, and it is commonly used after changing the window style by calling the SetWindowLong function.

SetWindowPos will cause the wm_windowposchanged message to be sent to the window, and the flags passed in this message are the same as those passed to the function. This function does not pass other messages.

Windows CE 1.0 does not support Hwnd_topmost and Hwnd_notopmost constants in the Hwndlnsertaber parameter.

Windows CE1.0 does not support the Swp_drawframe and swp_nocopybits flags in the Fufags parameter.

For example, the window created below is at the top level and specifies the client area size

1#include <windows.h>2 3 4 hinstance g_hins;5 HWND G_hwnd;6 7 8 LRESULT CALLBACK WndProc (hwnd,uint,wparam,lparam);9 Ten  One intWINAPI WinMain (hinstance hinstance,hinstance hprevinstance,lpstr lpCmdLine,inticmdshow) A {  -g_hins=hinstance; -  the     StaticTCHAR Szappname[]=text ("Hellowin");  - HWND hwnd; - msg msg; - wndclass wndclass; +  -wndclass.style=cs_hredraw|Cs_vredraw; +Wndclass.lpfnwndproc=WndProc; AWndclass.cbclsextra=0;  atWndclass.cbwndextra=0;  -Wndclass.hinstance=hinstance; -wndclass.hicon=LoadIcon (null,idi_application); -Wndclass.hcursor=loadcursor (null,idc_arrow); -Wndclass.hbrbackground=(Hbrush) getstockobject (White_brush); -Wndclass.lpszmenuname=NULL; inWndclass.lpszclassname=Szappname; -  to     if(! RegisterClass (&wndclass))  +     {  -MessageBox (Null,text ("Error"), szappname,mb_iconerror|MB_OK);  the         return 0;  *     }  $ Panax NotoginsengHwnd=createwindow (Szappname,text ("The Hello program"), Ws_overlappedwindow, - cw_usedefault,cw_usedefault,cw_usedefault,cw_usedefault,null,null,hinstance,null);  the  +       A  the     intIreturn; +  -     //can also be placed here $  $ ShowWindow (hwnd,icmdshow); -  - UpdateWindow (hWnd); the  - Wuyi  the  -      while(GetMessage (&msg,hwnd,0,0))  Wu     {  -TranslateMessage (&msg);  AboutDispatchMessage (&msg);  $     }  -  -     returnMsg.wparam; - }  A  + LRESULT CALLBACK WndProc (HWND hwnd,uint message,wparam wparam,lparam LPARAM) the {  -     StaticHDC hdc; $     StaticHDC HBITMAPDC; the  the paintstruct PS; the  the     intIreturn; -  in     StaticRect rect = {0,0, -, $};  the  the     Switch(message) About     {  the      Casewm_create: theIreturn = Adjustwindowrect (&rect,ws_overlappedwindow,true);  the  +SetWindowPos (Hwnd,hwnd_topmost, -, -, Rect.right-rect.left,rect.bottom-rect.top, Swp_nomove);//Top Floor -           the          Break; Bayi  the      CaseWM_PAINT: theHdc=beginpaint (hwnd,&PS);  -  -EndPaint (HWnd, &PS);  the  the          Break;  the  the      CaseWm_destroy: -PostQuitMessage (0) ;  the  the          Break ;  the 94     default:  the         returnDefWindowProc (hWnd, message, WParam, lParam); the  the     } 98  About     return 1;  - 101}

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.