'AdjustWindowRectEx returns different results in VS and C++Builder
I try to use AdjustWindowRectEx() function to get the sizes of the window frame with aero theme enabled.
But I found that AdjustWindowRectEx() function returns different results in Visual Studio 2012 and C++ Builder XE7.
All tests performed on Windows 7.
Firstly I try to get the window frame sizes for these styles (it is which corresponds to BorderStyle = bsDialog in C++ Builder):
DWORD styles = WS_CAPTION | WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_SYSMENU;
DWORD ex_styles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_DLGMODALFRAME | WS_EX_WINDOWEDGE | WS_EX_CONTROLPARENT | WS_EX_APPWINDOW;
RECT r = {0};
AdjustWindowRectEx(&r, styles, false, ex_styles);
It returns:
- VS: left: -8; top: -30; right: 8; bottom: 8
- CB: left: -3; top: -25; right: 3; bottom: 3
Here, the code executed in Visual Studio returns the correct result. And on C++Builder - there are wrong return values.
Let's try another styles (it is equivalent to BorderStyle = bsToolWindow in C++ Builder):
DWORD styles = WS_CAPTION | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_SYSMENU | WS_OVERLAPPED;
DWORD ex_styles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_TOOLWINDOW | WS_EX_WINDOWEDGE | WS_EX_CONTROLPARENT | WS_EX_APPWINDOW;
It returns:
- VS: left: -8; top: -26; right: 8; bottom: 8
- CB: left: -3; top: -21; right: 3; bottom: 3
Here, the code executed in Visual Studio returns the correct result also. But in Builder not.
But I found that return values are equal both on Visual Studio and C++ Builder with these styles (it is corresponds to BorderStyle = bsSizeable in C++ Builder):
DWORD styles = WS_CAPTION | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_SYSMENU | WS_THICKFRAME | WS_OVERLAPPED | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
DWORD ex_styles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_WINDOWEDGE | WS_EX_CONTROLPARENT | WS_EX_APPWINDOW;
It returns:
- VS: left: -8; top: -30; right: 8; bottom: 8
- CB: left: -8; top: -30; right: 8; bottom: 8
And my question is: why AdjustWindowRectEx() function returns different values for the same request styles on Visual Studio and C++ Builder?
Solution 1:[1]
By experience, I found out that to get correct window frame sizes using AdjustWindowRectEx() function compiled in C++ Builder (and Visual Stuidio also), it is necessary to add the WS_THICKFRAME style to request.
Also to adapt the code to different window styles, it is necessary to check whether it is appropriate to add WS_THICKFRAME: namely, if the styles WS_CAPTION and WS_THICKFRAME aren't set (in case when the window has no frames), the WS_THICKFRAME style isn't added to request.
RECT r = {0};
DWORD styles = GetWindowLongPtr(Handle, GWL_STYLE);
styles |= (!(styles & WS_CAPTION) && !(styles & WS_THICKFRAME)) ? 0 : WS_THICKFRAME;
AdjustWindowRectEx(&r, styles, false, GetWindowLongPtr(Handle, GWL_EXSTYLE));
Of course, the real reason stays unknown.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ig_M |
