GetWindowRect and GetClientRect Comparative study

Source: Internet
Author: User
Tags virtual environment

One: About coordinates

In MFC, it is often involved in coordinate calculations, GetWindowRect and GetClientRect, which are the usual functions for getting the size and coordinates of a window or control (in fact, a window) in a logical coordinate system.

What do you mean, logical coordinates first? Referring to the logical coordinates, it is a relative concept is the device coordinates, is to shield off the different device attribute differences and set the abstract coordinate system, plainly, is independent of the device coordinates of the unified interface, programmers do not need to go to the specific device mapping operations, and only need to be in a virtual environment to draw, is the CDC.

It is then driven by the device to take charge of the conversion between the virtual coordinates and the actual device coordinates. Usually there is a different mapping transformation relationship between the logical coordinates and the device coordinates, the default mode is mm_text, the direction and units of the logical coordinates are the same as the device coordinates, in pixels, the x-axis is positive, the y-axis is positive, The coordinate origin is located in the upper-left corner of the window

And then say the difference between the above two functions:

GetWindowRect gets the coordinates of the upper-left corner of the window relative to the current interface, such as a dialog box program, which is relative to the upper-left corner of the dialog box, from left to right is the x direction, from the top down is the y direction.

Note: The relative coordinate origin of this function is divided into two cases:

1 window has not been initialized when finished: The origin is the upper-left corner of the entire window

2 after initialization is complete, the origin is the upper left corner of the device screen.

GetClientRect gets the coordinates relative to the upper-left corner of the window's client area.

Two: Verification

Here's a piece of code to understand:

Create a new MFC dialog box to add a static box to the dialog resource. Add the following code to the OnInitDialog:

BOOL Cmfcdialogtestdlg::oninitdialog () {/*............. Other code .......*//*****************for testing***************/CRect WndRect1;    CRect WndRect2; /** * * First look at the dialog box * * * **/     This->getwindowrect (&WNDRECT1);//printrect ("Dialog:getwindowrect", wndRectScreen1);     This->screentoclient (&WNDRECT1);//printrect ("Dialog:screentoclient", wndRectClient1);     This->getclientrect (&WNDRECT2);//printrect ("Dialog:getclientrect", WndRectClient2);     This->clienttoscreen (&WNDRECT2);//printrect ("Dialog:clienttoscreen", wndRectScreen2);    /** * * re-look at the control **********/CRect CtrlRect1;    CRect CtrlRect2; CStatic*pctrl = (cstatic*) GetDlgItem (idc_stc_test); Pctrl->getclientrect (&CTRLRECT1);//printrect ("Static:getclientrect", ctrlRectClient1);Pctrl->clienttoscreen (&CTRLRECT1);//printrect ("Static:clienttoscreen", ctrlRectScreen1);Pctrl->getwindowrect (&CTRLRECT2);//printrect ("Static:getwindowrect", ctrlRectScreen2);Pctrl->screentoclient (&CTRLRECT2);//printrect ("Static:screentoclient", CtrlRectClient2);    /*****************ending********************//*............. Other code .......*/}

Three: Analysis

We observe the effect of invoking the two functions separately for the entire dialog box and the static control. First look at the dialog box:

(1) The result of the program running in the dialog box section:

dialog box: GetWindowRect: +&wndrect10x0017f410 {top=0 bottom=378 left=0 right=566} CRect *screentoclient:+&wndrect10x0017f410 {top=-25 bottom=353 left=-3 right=563} CRect *getclientrect: +&wndrect20x0017f3f8 {top=0 bottom=350 left=0 right=560} CRect *clienttoscreen:+&wndrect20x0017f3f8 {top=25 bottom=375 left=3 right=563} CRect *

We give a picture analysis:

The first getwindowrect gets the coordinates of the entire window relative to the upper-left corner of the window, which is actually the size of the dialog box.

Then screentoclient, notice that we found that both top and left were negative values? Why, because the conversion at this time is based on the origin of the client, that is, the upper-left corner of the client area. Because the upper left corner of the original window is at the top left of the client origin, it is negative.

The second getclientrect gets the size of the client area, because it does not contain the blue area around the window, so it is smaller than the first window size.

And then ClientToScreen, this is a good explanation for calculating the coordinates relative to the upper-left corner of the window. You can figure it out for yourself.

(2) The program running result of the control:

Control: GetClientRect: +&ctrlrect10x0017f3e0 {top=0 bottom=170 left=0 right=285} CRect *clienttoscreen: +&ctrlrect10x0017f3e0 {top=127 bottom=297 left=73 right=358} CRect *getwindowrect: +&ctrlrect20x0017f3c8 {top=127 bottom=297 left=73 right=358} CRect *screentoclient: +&ctrlrect20x0017f3c8 {top=0 bottom=170 left=0 right=285} CRect *

or the image analysis:

Similarly, the first getclientrect gets the size of the control.

Then ClientToScreen, you can see that at this time, the displacement of the control relative to the upper-left corner of the dialog window is (127,73).

The second getwindowrect, the value is the same as the above value, this very good understanding, their meaning is the same, is the control in the window coordinates.

Then screentoclient, the resulting value is the same as the value of the first GetClientRect. No more talking.

Why is there a slight difference between a dialog box and a control's conversion (two conversions are not the same), mainly because the dialog box contains the non-client area, making the relative coordinates inconsistent.

GetWindowRect and GetClientRect Comparative study

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.