MoveWindow and SetWindowPos

Source: Internet
Author: User

Transferred from: http://blog.sina.com.cn/s/blog_82c346de0100u7kq.html
MoveWindow and SetWindowPos(2011-09-14 15:56:51) reproduced
tags:   gossip  mfc

MoveWindow can only set the size and position of the form; SetWindowPos has all the functions of MoveWindow. You can also set the cascading relationships of a form, such as. Place the specified form at the top of all forms--always on top to use this function, or to place the specified form on the lower level of a form. And so on). There are no pros and cons, just the size of the function is different.

You can do without MoveWindow, but only with SetWindowPos; You can also use MoveWindow only if you want to set the size or position of the form.



voidMoveWindow(IntXIntYIntNwidth,IntNheight,BOOLBrepaint=TRUE );
voidMoveWindow (LpcrectLpRect,BOOLBrepaint=TRUE);
Number of references
x Specifies the new position to the left of CWnd.
y Specifies the new position at the top of the CWnd.
NWIDTH Specifies the new width of CWnd.
nheight specifies a new height for CWnd.
BREPAINT Specifies whether to repaint CWnd.

If true, the CWnd receives a WM_PAINT message in the OnPaint message handler as usual. Suppose this parameter is false. It does not occur regardless of the type of repaint operation. This applies to the customer area, the non-client area (which contains the title bar and the scroll bar), and whatever part of the parent form is exposed because of CWnd movement. When this parameter is false, the application must clearly invalidate or redraw the parts of CWnd and the parent form that must be redrawn. A Lprectcrect object or RECT structure that specifies the new size and position. Explains how the function changes the position and size of the form. For the top-level CWnd object, the x and Y parameters are relative to the upper-left corner of the screen. For child objects. They are relative to the upper-left corner of the client area of the parent form.
The MoveWindow function sends a WM_GETMINMAXINFO message. When handling this message, CWnd gets an opportunity to change the default value of the maximum and minimum form. Assume that the number of parameters passed to the MoveWindow member function exceeds these values. It is possible to replace these values with the minimum or maximum values in the Wm_getminmaxinfo processing function.



BOOL SetWindowPos(Constcwnd*pWndInsertAfter,IntXIntYIntCxInt Cy,uintNflags);
The return value assumes that the function succeeds and returns a value other than 0. Otherwise, 0 is returned.
The parameter pWndInsertAfter identifies the CWnd object that precedes the CWnd object in the z-axis order. This parameter can be a pointer to a CWnd object. can also be a pointer to the following values: lWndbottomPlace the form at the bottom of the z-axis order. Assuming this CWnd is a top-level form, the form loses its top-level state; the system places the form at the bottom of all other forms.

L wndtop Place the form at the top of the z-axis order. L wndtopmost Place the form above all non-top-level forms. This form will maintain its top-level position even if it loses its active state.  wndnotopmost Position the form again at the top of all non-top-level forms (which means underneath the full top-level form).

This flag has no effect on forms that are already non-top-level forms. The usage rules for this function and these parameters are described in the Description section. x Specifies the new position to the left of the form. y Specifies the new position at the top of the form. CX Specifies the new width of the form.

CY Specifies the new height of the form. NFLAGS Specifies the size and location options.

This parameter can be a combination of the following values: l swp_drawframe draws a border around the form (defined when the form is created).

l     swp_framechanged              sends a wm_nccalcsize message to the form, even if the size of the form does not change. Assuming this flag is not specified, the Wm_nccalcsize message is sent only when the size of the form changes. l     swp_hidewindow              Hide the form. swp_noactivate              do not activate the form. Assume that this flag is not set. The form is activated and moved to the top of a top-level or non-top form group (a setting that relies on pwndinsertafter parameters). l     swp_nocopybits              Discard the content of this customer area. Assuming this parameter is not specified, the valid contents of the client area are saved and copied back to the customer area after the form is changed in size or position. l     swp_nomove              maintains the current position (ignoring the x and Y parameters).

L swp_noownerzorder does not change the position of the owner form on the z-axis order. L swp_noredraw do not redraw changes. Assuming this flag is set, no matter what kind of change occurs.

This applies to the customer area, the non-client area (including the title and scroll bar), and whatever part of the parent form that is covered by the moved form. When this flag is set. The application must be clearly invalid or redraw whatever part of the form and parent form you want to repaint.

L swp_noreposition and swp_ Noownerzorder the same. L swp_nosendchanging Prevents the form from receiving wm_windowposchanging messages. L swp_nosize Keep the current size (ignoring CX and CY parameters). L swp_nozorder Keep the current order (ignore pWndInsertAfter).

L swp_showwindow display the form.


 
Call this member function to change the size, position, and Z-order of the subform, pop-up form, and top-level form.

Forms are sorted on the screen according to their z-axis order. The form at the top of the z-order is the program at the top of all other forms.

All the coordinates of the subform are customer coordinates (relative to the upper-left corner of the parent form's client area). The form can be moved to the top of the z-axis order, either by setting the pWndInsertAfter parameter to &wndtopmost and by ensuring that the SWP_NOZORDER flag is not set. It is also possible to set the form's z-axis order so that it is above all existing top-level forms. When a non-top-level form is set to the top-level form. The form it owns is also set to the top level. Its owner does not change.

Suppose that the top-level form is once again anchored to the bottom of the z-axis order (&wndbottom) or after any non-top-level form. It will no longer be the top-level form.

When the When top layer form is changed to a non-top-level form. All of its owners and all the forms it owns are turned into non-top-level forms. Assuming neither the SWP_NOACTIVE flag is specified nor the SWP_NOZORDER flag is specified (which means that the application requires the form to be activated at the same time and placed in the specified z-order), the value specified in the pWndInsertAfter parameter will only apply in the following environment: l  Neither &wndtopmost nor &wndnotopmost have been specified in the pWndInsertAfter parameter.
This form is not an active form. The application cannot activate an inactive form but does not take it to the top of the z-order at the same time.

The application can change the z-axis order of the active form without any restrictions. A non-top-level form may have a top-level form, but vice versa. Any form that is owned by a top-level form, such as a dialog box, changes itself to a top-level form. To ensure that all of the forms that are owned are located above their owners.

In the version number of Windows 3.1 or later. The ability to move the form to the top of the z-axis order and lock it there by setting their ws_ex_topmost style. Such a top-level form maintains the top-level position even after it loses its active state. For example, selecting the Always on top command of WinHelp makes the help form the top layer and remains visible after you return to the application. To create a top-level form. You should set pWndInsertAfter as &wndtopmost when calling SetWindowPos, or ws_ex_topmost style when creating a form. Assuming that the z-axis order includes any form that has a ws_ex_topmost style, the form moved with &wndtopmost will be placed at the top of all non-top-level forms, but no matter what the top-level form is below. When an application activates an inactive form that does not have a ws_ex_topmost style, the form is moved above all non-top-level forms, but underneath all the top-level forms. Assume that the pWndInsertAfter parameter is set to &wndbottom when calling SetWindowPos. And CWnd is a top-level form. The form loses its top-level state (Ws_ex_bottom style is cleared), and the system places the form at the bottom of the z-order.

MoveWindow

function function: This function changes the position and size of the specified form.

For the top-level form, the position and dimensions are relative to the upper-left corner of the screen: for subforms. The position and dimensions are relative to the upper-left coordinate of the parent form client area.



function prototype : BOOL MoveWindow (HWND hwnd.int x.int y,int nwidth,int nheight,bool brepaint).

number of references:

hWnd : Form handle.



x : Specifies the left edge of the new position of the form.

Y : Specifies the top boundary of the new position of the form.



nwidth : Specifies the new width of the form.

nhaight : Specifies the new height of the form.

brepaint : determines whether the form is refreshed. Assuming that the parameter is true, the form receives a WM_PAINT message, assuming that the parameter is false. Does not occur no matter what the refresh action.

It applies to the customer area. Non-client area (includes title bar and scroll bar). And the area of the parent form that is exposed because the subform is moved. Assuming that the parameter is false, the application must clearly invalidate the form or repaint the form and the parent form that needs to be refreshed.



return value : Assuming the function succeeds, the return value is not 0 , assuming the function failed. The return value is zero. If you want to get a lot of other error messages. Please call the GetLastError function.

Note : Assume that Brepaint is true. The system sends a WM_PAINT message to the form procedure immediately after the form moves (that is, the UpdateWindow function is called by the MoveWindow function). Assuming Brepaint is false, the system places the WM_PAINT message in the form's message queue. The message loop only dispatches WM_PAINT messages when other messages in the message queue are dispatched.

MoveWindow sends wm_wfnowposchanging to the form. Wm_windowposchanged,wm_move,wm_size and wm_nccalcsize messages,

quick Check : Windows nt:3.1 above version number: windows:95 or above version number. Windows ce:1.0 above version number: header file: Winuser.h. Library file: User32.lib.

I collect and learn other people's articles, I thank the author of these articles or translators

Change the size and position of a control

The function MoveWindow () or SetWindowPos () of the CWnd class can change the size and position of the control.

void MoveWindow (int x,int y,int nwidth,int nheight);
void MoveWindow (Lpcrect lpRect);
The first way to use this method is to give the new coordinates and width and height of the control.
Another way to use the CRect object to store the location;
Cases:
CWnd *pwnd;
PWnd = GetDlgItem (IDC_EDIT1); //Gets the control pointer, idc_edit1 the control ID number
Pwnd->movewindow (CRect (0,0,100,100)); //In the upper left corner of the form, an edit control with a width of 100 and a height of 100 is displayed

The SetWindowPos () function is more flexible and is used in situations where the position of the control is changed only if the size is unchanged or only the size is changed:
BOOL SetWindowPos (const cwnd* pwndinsertafter,int x,int y,int cx,int cy,uint nflags);
I will not use the first number of parameters. is generally set to null;
x, y control location, CX, CY Control width, and height.
Nflags often use values:
Swp_nozorder: Ignores the first number of parameters;
Swp_nomove: Ignore x, Y, maintain position unchanged;
Swp_nosize: Ignore CX, CY, maintain the same size;
Cases:
CWnd *pwnd;
PWnd = GetDlgItem (Idc_button1);Gets the control pointer, idc_button1 the control ID number
Pwnd->setwindowpos (Null,50,80,0,0,swp_nozorder | Swp_nosize);Move the button to the form (50,80)
PWnd = GetDlgItem (IDC_EDIT1);
Pwnd->setwindowpos (Null,0,0,100,80,swp_nozorder | Swp_nomove);Set the size of the edit control to (100,80). The position does not change
PWnd = GetDlgItem (IDC_EDIT1);
Pwnd->setwindowpos (Null,0,0,100,80,swp_nozorder);The size and position of the edit control are changed
The above methods also apply to various forms

MoveWindow and SetWindowPos

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.