Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between GetClientRect and GetWindowRect in WinApi?

What of these should I use in InvalidateRect to refresh my window? And why?

like image 737
Abzac Avatar asked Sep 26 '11 20:09

Abzac


3 Answers

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 whereas GetClientRect returns a rect in client coordinates.

InvalidateRect receives a rect in client coordinates. If you want to invalidate your entire client area, then pass NULL to InvalidateRect. You could pass in the rect returned by GetClientRect, but it is far simpler and clearer to pass NULL.

like image 178
David Heffernan Avatar answered Sep 19 '22 10:09

David Heffernan


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.

like image 17
Jörgen Sigvardsson Avatar answered Sep 19 '22 10:09

Jörgen Sigvardsson


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:

I would say that this function return size of the area that I can render to.

GetWindowsRect gets the coordinates of the whole window. This includes the header, status bar etc. However according to a comment on the MSDN page

Apps under Vista that are not linked with WINVER=6 will receive a misleading set of values here, that do not account for the extra padding of "glass" pixels Vista Aero applies to the window.

So unless this have been fixed for Windows 7 double check the result you get and make sure you have the correct value of WINVER.

like image 12
ChrisF Avatar answered Sep 19 '22 10:09

ChrisF