GetWindowRect和js getclientrectt的区别详解

GetWindowRect,GetClientRect,和ScreenToClient
GetWindowRect,GetClientRect,和ScreenToClient
先调用GetWindowRect后再调用ScreenToClient,这个时候得到的rect和直接使用GetClientRect得到的值是相等的。有时候需要获得窗口矩形的大小和客户区矩形的大小二者的值,故需要分别调用GetWindowRect和GetClientRect。如果只需要获得客户区矩形的大小,调用GetClientRect就行了。GetWindowRect和GetClientRect函数的说明如下:
CWnd::GetClientRect&&&&& void GetClientRect( LPRECT lpRect )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::GetWindowRectvoid GetWindowRect( LPRECT lpRect )Parameters:lpRectPoints 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
ClientToScreenThe 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);ParametershWndHandle to the window whose client area is used for the conversion.lpPointPointer 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 ValuesIf 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函数进行了重新整理和优化,在获取控件或窗口的屏幕坐标和客户区坐标时更方便的多,只需要得到与控件或窗口相对应屏幕坐标和客户区坐标属性值就可以了。
ScreenToClientThe 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:hWndHandle to the window whose client area will be used for the conversion.lpPointPointer 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.
例如:右下角坐标等于左上角坐标加上窗口的尺寸 && && CRect & && GetWindowRect(&rt); && CPoint & && pt & = & rt.BottomRight();&&
我的总结:如果需要获得窗体在屏幕上的位置,使用GetWindowRect如果需要获得窗体的大小,使用GetClientRect
发表评论:
TA的最新馆藏[转]&[转]&[转]&[转]&[转]&&今天项目中需要对屏幕坐标系和客户区坐标系进行转换,在使用以下两对函数时感到很困惑。在参考了这篇文章之后,有点明白,但是这篇帖子后边的附图让我感到很困惑(让我认为这两对函数的处理不是可逆的),但是经过试验之后才发现这两对函数其实是一对互逆操作,测试如下:
GetClientRect(&m_rcClient);//0,0;258,80
ClientToScreen(m_rcClient);//726,536;984,616
、、、、、、、、、、、、、
GetWindowRect(&tempRect);//726,536;984,616
ScreenToClient(tempRect);//0,0;258,80
----------------------------补充说明---------------------------------------------------
GetWindowRect是取得窗口在屏幕坐标系下的RECT坐标(包括客户区和非客户区),这样可以得到窗口的大小和相对屏幕左上角(0,0)的位置。
    GetClientRect取得窗口客户区(不包括非客户区)在客户区坐标系下的RECT坐标,可以得到窗口的大小,而不能得到相对屏幕的位置,因为这个矩阵是在客户区坐标系下(相对于窗口客户区的左上角)的。  
    ClientToScreen把客户区坐标系下的RECT坐标转换为屏幕坐标系下的RECT坐标.
    ScreenToClient把屏幕坐标系下的RECT坐标转换为客户区坐标系下的RECT坐标.     我们对同一个窗口先GetWindowRect取得一个RECT,再用ScreenToClient转换到客户坐标系。然后GetClientRect取得一个RECT,再用ClientToScreen转换到屏幕坐标系。显然,GetWindowRect取得的矩阵不小于GetClientRect取得的矩阵。因为前者包含了非客户区,而后包括了客户区。   
    对GetWindowRect取得的矩阵ScreenToClient后,矩阵的大小没有变小,(-3,-29)是窗口的左上角的坐标,相对窗口客户区左上角。   
    对GetClientRect取得的矩阵ClientToScreen后,矩阵也没有变大,新得到的矩阵是窗口客户区在屏幕坐标系上的RECT。
GetWindowRect
  函数功能:该函数返回指定窗口的边框矩形的尺寸。该尺寸以相对于屏幕坐标左上角的屏幕坐标给出。
  函数原型:BOOL GetWindowRect(HWND hWnd,LPRECTlpRect);
  在Visual Studio 2005中,函数原型为void GetWindowRect(LPRECT lpRect)
  是属于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 )
Parameters:
&&& 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.
&&& 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 )
Parameters:
Points to a CRect object or a RECT structure that will receive the screen coordinates of the upper-left and lower-right corners.
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
Handle to the window whose client area is used for the conversion.
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:
Handle to the window whose client area will be used for the conversion.
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.
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:43325次
积分:1439
积分:1439
排名:第19915名
原创:63篇
转载:238篇
(1)(1)(11)(5)(1)(9)(11)(14)(16)(70)(3)(6)(8)(10)(1)(13)(6)(11)(43)(7)(15)(5)(8)(3)(2)(6)(2)(18)GetClientRect_历史版本_百度百科
原内容错误,应该修改。
基本信息栏
本就应是函数getclientrect,没有函数getclientrects
内容扩充 添加或修改图片、图册
也可以是一个CRrect对象指针,这句中的CRrect应该是CRect吧。
原文中[输出]应该为 “:”
没有直接说明函数的作用
并非是子窗口,就是要获取rect的窗口的客户区左上角!
GetClientRect与GetWindowRect功能是不同的,前者是获得客户区的尺寸,而后者是获得整个窗口相对于桌面的尺寸位置。
修正翻译上的错误,增加函数返回值和函数使用的要求。
与C++标准库对照之后,发现有出入MFC中,GetClientRect()与GetWindowRect()+ScreenToClient()效果相同,但为什么实际值不同?
例如这段代码:CS path = L"D:\\1.jpg"; CI HRESULT hr = img.Load(path); CR int cx = img.GetWidth(); int cy = img.GetHeight(); GetDlgItem(IDC_PIC)-&GetWindowRect(rect); ScreenToClient(&rect); GetDlgItem(IDC_PIC)-&MoveWindow(rect.left,rect.top,cx,cy,TRUE); CWnd *pWnd = GetDlgItem(IDC_PIC); pWnd-&GetClientRect(&rect); CDC *pDC = pWnd-&GetDC(); img.Draw(pDC-&m_hDC,rect); ReleaseDC(pDC);为什么:GetDlgItem(IDC_PIC)-&GetWindowRect(rect);ScreenToClient(&rect);不能写成GetDlgItem(IDC_PIC)-&GetClientRect(rect); ?
GetClientRect:该函数获取窗口客户区的坐标。客户区坐标指定客户区的左上角和右下角。由于客户区坐标是相对窗口客户区的左上角而言的,因此左上角坐标为(0,0)GetWindowRect:该函数返回指定窗口的边框矩形的尺寸。该尺寸以相对于屏幕坐标左上角的屏幕坐标给出。这两个函数的不同点就在于有无边框,边框也是要占位置的,所以实际值当然不一样
已有帐号?
无法登录?
社交帐号登录MFC笔记_图文_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
评价文档:
上传于||暂无简介
大小:68.87KB
登录百度文库,专享文档复制特权,财富值每天免费拿!
你可能喜欢

我要回帖

更多关于 mfc getclientrect 的文章

 

随机推荐