GetWindowRect与GetClientRect 的区别 .

摘要:
voidGetClientRectconst;功能:将CWnd客户端区域的客户坐标复制到lpRect所指向的结构。GetWindowRect相对于屏幕的左上角,因此结果可能是。函数原型:BOOLGetWindowRect;在Visual Studio 2005中,函数原型是void GetWindowRectconst;它是属于CWnd类Parameter:hWnd:window句柄的函数。首先调用GetWindowRect,然后调用ScreenToClient。此时,获得的rect等于直接使用GetClientRect获得的值。有时需要获取窗口矩形的大小和客户端区域矩形的大小,因此需要分别调用GetWindowRect和GetClientRect。
void GetClientRect(
LPRECT lpRect
) const;
作用:把CWnd客户区的客户坐标复制到lpRect指向的结构体中。
返回值:如果函数成功,返回值为非零:如果函数失败,返回值为零。若想获得更多错误信息,请调用GetLastError函数。
lpRect:指向RECT结构体或CRect类的指针,接受客户区的坐标。
因为此坐标是相对于CWnd客户区的左上角,故总是得到(0,0,宽度,高度)。
GetClientRect()得到的是客户区的矩形,不包括标题栏,外框。
GetWindowRect()得到的是整个窗口的矩形。
总结:
相同点:GetclientRect和GetWindowRect都得到矩形的左上角和右下角顶点的坐标。
区别:GetclientRect是相对于窗口的客户区左上角,故得到的总是(0,0,宽度,高度),即客户区的矩形。

GetWindowRect是相对于屏幕的左上角,故得到的可能是(10,10,30,40)。(10,10)表示窗口左上角顶点相对于屏幕左上角的坐标,(30,40)表示窗口右下角顶点相对于屏幕左上角的坐标。窗口的宽度为30-20=10,高度为40-10=30.

便于理解:
调用GetWindowRect后再调用ScreenToClient==GetClientRect,
调用GetClientRect后再调用ClientToScreen==GetWindowRect

其它解释:

GetWindowRect
函数功能:该函数返回指定窗口的边框矩形的尺寸。该尺寸以相对于屏幕坐标左上角的屏幕坐标给出。
函数原型:BOOL GetWindowRect(HWND hWnd,LPRECTlpRect);
在Visual Studio 2005中,函数原型为void GetWindowRect(LPRECT lpRect) const;
是属于CWnd类的函数.
参数:
hWnd:窗口句柄。
lpRect:指向一个RECT结构的指针,该结构接收窗口的左上角和右下角的屏幕坐标。
返回值:如果函数成功,返回值为非零:如果函数失败,返回值为零。若想获得更多错误信息,请调用GetLastError函数。
速查:Windows NT:3.1以上版本:Windows:95以上版本;Windows CE:1.0以上版本;头文件:Winuser.h;库文件:User32.lib。
先调用GetWindowRect后再调用ScreenToClient,这个时候得到的rect和直接使用GetClientRect得到的值是相等的。

有时候需要获得窗口矩形的大小和客户区矩形的大小二者的值,故需要分别调用GetWindowRect和GetClientRect。

如果只需要获得客户区矩形的大小,调用GetClientRect就行了。

GetWindowRect和GetClientRect函数的说明如下:

CWnd::GetClientRect
void GetClientRect( LPRECT lpRect ) const;
Parameters:
lpRect
Points to a RECT structure or a CRect object to receive the client coordinates. The left and top members will be 0. The right and bottom members will contain the width and height of the window.
Remarks:
Copies the client coordinates of the CWnd client area into the structure pointed to by lpRect. The client coordinates specify the upper-left and lower-right corners of the client area. Since client coordinates are relative to the upper-left corners of the CWnd client area, the coordinates of the upper-left corner are (0,0).

CWnd::GetWindowRect
void GetWindowRect( LPRECT lpRect ) const;
Parameters:
lpRect
Points to a CRect object or a RECT structure that will receive the screen coordinates of the upper-left and lower-right corners.
Remarks:
Copies the dimensions of the bounding rectangle of the CWnd object to the structure pointed to by lpRect. The dimensions are given in screen coordinates relative to the upper-left corner of the display screen. The dimensions of the caption, border, and scroll bars, if present, are included.

GetWindowRect() 得到的是在屏幕坐标系下的RECT;(即以屏幕左上角为原点)
GetClientRect() 得到的是在客户区坐标系下的RECT; (即以所在窗口左上角为原点)

GetWindowRect()取的是整个窗口的矩形;
GetClientRect()取的仅是客户区的矩形,也就是说不包括标题栏,外框等;

第一个函数获得的是窗口在屏幕上的位置,得到的结果可能是这样CRect(10,10,240,240);
第二个函数和它不同,它只获得了客户区的大小,因此得到的结果总是这样CRect(0,0,width,height);

ScreenToClient() 就是把屏幕坐标系下的RECT坐标转换为客户区坐标系下的RECT坐标。

The GetClientRect function retrieves the coordinates of a window's client area. The client coordinates specify the upper-left and lower-right corners of the client area. Because client coordinates are relative to the upper-left corner of a window's client area, the coordinates of the upper-left corner are (0,0).

GetClientRect得到的是客户区的大小,也就是说这样得到的左上角永远是(0,0)

The GetWindowRect function retrieves the dimensions of the bounding rectangle of the specified window. The dimensions are given in screen coordinates that are relative to the upper-left corner of the screen.

GetWindowRect 是窗口相对于整个屏幕的坐标,屏幕左上点为0,0

相互转化用ScreenToClient 或者 ClientToScreen

ClientToScreen
The ClientToScreen function converts the client coordinates of a specified point to screen coordinates.
BOOL ClientToScreen(
HWND hWnd, // window handle for source coordinates
LPPOINT lpPoint // pointer to structure containing screen coordinates
);
Parameters
hWnd
Handle to the window whose client area is used for the conversion.
lpPoint
Pointer to a POINT structure that contains the client coordinates to be converted. The new screen coordinates are copied into this structure if the function succeeds.
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero.

虽然存在调用GetWindowRect后再调用ScreenToClient==GetClientRect,但ScreenToClient()和ClientToScreen()两者都是属于WINDOWS API函数,可能是存在一定的冗余设计,但意义不同。
不过在.Net Framework下对WINDOWS API函数进行了重新整理和优化,在获取控件或窗口的屏幕坐标和客户区坐标时更方便的多,只需要得到与控件或窗口相对应屏幕坐标和客户区坐标属性值就可以了。

ScreenToClient
The ScreenToClient function converts the screen coordinates of a specified point on the screen to client coordinates.
BOOL ScreenToClient(
HWND hWnd, // window handle for source coordinates
LPPOINT lpPoint // address of structure containing coordinates
);
Parameters:
hWnd
Handle to the window whose client area will be used for the conversion.
lpPoint
Pointer to a POINT structure that contains the screen coordinates to be converted.
Return Values:
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero.

免责声明:文章转载自《GetWindowRect与GetClientRect 的区别 .》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇TMS320DM642学习----第六篇(CCS中.dat文件类型详解)asp.net MVC中防止跨站请求攻击(CSRF)的ajax用法下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

java线程管理

java线程管理 参见: http://harmony.apache.org/subcomponents/drlvm/TM.html 1. 修订历史 2. 关于本文档 2.1. 目的 2.2. 面向的读者 2.3. 文档约定 2.4. 文档使用 3. 概览 3.1. 主要特点 3.2. VM中的线程管理器 3.3. 可移植性 4. 体系结构...

wifi连接流程分析

Wifi 连接部分http://blog.csdn.net/typename/article/details/5879121 当用户选择一个AP时会弹出一个AP参数配置对话框,此对话框会显示当前选择的AP信号强度,若此AP设置了密码则需要用户输入密码才能登录。WifiSettings中的 onPreferenceTreeClick会被调用         ...

安卓进阶:元注解Support Annotation Library使用详解

Support Annotation Library是一个函数包,包含一系列有用的元注解。 注解目录:  Nullness注解  资源类型注解 类型定义注解 线程注解 RGB颜色值注解 值范围注解 权限注解 重写函数注解 返回值注解 @VisibleForTesting @Keep 实用主义至上,不记录历史和版本这些,现在直接了解一下这个函数包有什么用...

网络编程,从socket到epoll

网络编程,从socket到epoll 参考链接:https://www.bilibili.com/video/BV11Z4y157RY?p=2&spm_id_from=pageDriver socket基本知识: socket分类: socekt提供了流和数据报两种通信机制,即流socket和数据报socket。 简单的socket通信流程: 先...

Linux动态频率调节系统CPUFreq之二:核心(core)架构与API

上一节中,我们大致地讲解了一下CPUFreq在用户空间的sysfs接口和它的几个重要的数据结构,同时也提到,CPUFreq子系统把一些公共的代码逻辑组织在一起,构成了CPUFreq的核心部分,这些公共逻辑向CPUFreq和其它内核模块提供了必要的API,像cpufreq_governor、cpufreq_driver等模块通过这些API来完成一个完整的CP...

Qt绘图之QGraphicsScene QGraphicsView QGraphicsItem详解

Graphics View提供了一个界面,它既可以管理大数量的定制2D graphical items,又可与它们交互,有一个view widget可以把这些项绘制出来,并支持旋转与缩放。这个柜架也包含一个事件传播结构,对于在scene中的这些items,它具有双精度的交互能力。Items能处理键盘事件,鼠标的按,移动、释放、双击事件,也可以跟踪鼠标移动。...