WinApi 中的 GetClientRect 和 GetWindowRect 有什么区别?
我应该在 InvalidateRect 中使用哪些来刷新我的窗口?为什么?
What of these should I use in InvalidateRect to refresh my window? And why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
窗口矩形包括非客户区域,即窗口边框、标题栏等。客户矩形不包括。
GetWindowRect
返回屏幕坐标中的矩形,而GetClientRect
返回客户端坐标中的矩形。InvalidateRect
接收客户端坐标中的矩形。如果您想要使整个客户区无效,请将NULL
传递给InvalidateRect
。您可以传入GetClientRect
返回的矩形,但传递NULL
更简单、更清晰。The window rect includes the non-client area, i.e. the window borders, caption bar etc. The client rect does not.
GetWindowRect
returns a rect in screen coordinates whereasGetClientRect
returns a rect in client coordinates.InvalidateRect
receives a rect in client coordinates. If you want to invalidate your entire client area, then passNULL
toInvalidateRect
. You could pass in the rect returned byGetClientRect
, but it is far simpler and clearer to passNULL
.一个非常简单的解释是,
GetWindowRect()
为您提供包含窗口边框的矩形。GetClientRect()
为您提供不包括边框的矩形 - 分配给窗口特定绘图的区域。请注意,
GetWindowRect()
返回屏幕坐标中的矩形 - 相对于屏幕/监视器的坐标。GetClientRect()
返回一个相对于自身的矩形。A very simple explanation is that
GetWindowRect()
gives you the rectangle that includes the borders of the window.GetClientRect()
gives you the rectangle that excludes the borders - the area that is allocated to the window specific drawing.Please note that
GetWindowRect()
returns a rectangle in screen coordinates - coordinates that are relative to the screen/monitor.GetClientRect()
returns a rectangle that is relative to itself.GetClientRect
获取窗口客户区的坐标。具体来说,这是窗口镶边内部区域,并且排除标题等。 MSDN 页面 总结得很好:GetWindowsRect
获取整个窗口的坐标。这包括标题、状态栏等。但是根据MSDN 页面因此,除非 Windows 7 已修复此问题,否则请仔细检查您获得的结果并确保您拥有正确的
WINVER
值。GetClientRect
gets the coordinates of the window's client area. Specifically this is the area inside the window chrome and excludes the header etc. One of the comments on the MSDN page sums it up quite well:GetWindowsRect
gets the coordinates of the whole window. This includes the header, status bar etc. However according to a comment on the MSDN pageSo unless this have been fixed for Windows 7 double check the result you get and make sure you have the correct value of
WINVER
.来自 MSDN:
获取窗口矩形
检索指定窗口的边框的尺寸。尺寸以相对于屏幕左上角的屏幕坐标给出。
获取客户端矩形
检索窗口客户区的坐标。客户坐标指定客户区域的左上角和右下角。因为客户坐标是相对于窗口客户区域的左上角的,所以左上角的坐标是(0,0)。
更多:客户端矩形不包括标题栏、边框、滚动条、状态栏...
From MSDN:
GetWindowRect
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.
GetClientRect
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).
More: client rect does not include title bar, borders, scroll bars, status bar...