0

When I call GetWindowRect() for a non-resizeable(no WS_SIZEBOX) window it does not give me the correct position of window. I have tired adding WS_SIZEBOX. But this also didn't work unless I changed the window's size and resized it back.

Is there any way to get the correct position?

Orkhan Alikhanov
  • 9,122
  • 3
  • 39
  • 60
  • GetWindowRect works fine. You just haven't understood all of the nuances yet. Without a repro, what detail can we offer? – David Heffernan Dec 30 '15 at 11:07
  • for GetWindowRect there is difference between the window with WS_SIZEBOX style and without WS_SIZEBOX style. if window does not have WS_SIZEBOX style, GetWindowRect returns small left,top,bottom,right than the one having WS_SIZEBOX style. If you dont believe me just check it. read [this](http://stackoverflow.com/a/20027688/5555803) explains. – Orkhan Alikhanov Dec 30 '15 at 11:25
  • The fat windows borders used in Aero were a very significant appcompat problem. Windows intentionally lies about the window rectangle and pretends that a window still has the skinny 2 pixel borders to ensure that an app does not create a window with a client area that is too small to fit the content. How this appcompat is related to this question is impossible to tell. – Hans Passant Dec 30 '15 at 11:36

1 Answers1

1

GetWindowRect on Windows 7 appears to not include the right and bottom window frame edges (at least with the Aero theme ), if the window was created without the WS_SIZEBOX(or another name WS_THICKFRAME) style. The problem is on Aero, windows have the thickframe whether they can be resized or not. But the GetWindowRect function thinks that a non-resizeable window is thinner. Fortunately, there is another way to get window size and position by using DwmGetWindowAttribute() (After vista)

[DllImport(@"dwmapi.dll")]
private static extern int DwmGetWindowAttribute(IntPtr hwnd, int dwAttribute, out Rect pvAttribute, int cbAttribute);

public static bool GetWindowActualRect(IntPtr handle, out Rect rect) 
{
 const int DWMWA_EXTENDED_FRAME_BOUNDS = 9;
 int result = DwmGetWindowAttribute(handle, DWMWA_EXTENDED_FRAME_BOUNDS, out rect, Marshal.SizeOf(typeof(Rect)));

 return result >= 0;
}

Source: http://www.code4copy.com/csharp/post/getting-window-rect-using-handle-on-windows-8-and-other

Orkhan Alikhanov
  • 9,122
  • 3
  • 39
  • 60