How do I compute the non-client window size in WPF? How do I compute the non-client window size in WPF? windows windows

How do I compute the non-client window size in WPF?


For a resizable window you need to use a different set of parameters to compute the size:

var titleHeight = SystemParameters.WindowCaptionHeight  + SystemParameters.ResizeFrameHorizontalBorderHeight;var verticalBorderWidth = SystemParameters.ResizeFrameVerticalBorderWidth;

These sizes will change when you modify the theme.


I'm pretty sure that the GetSystemMetrics function (which the SystemParameters class calls internally with the appropriate arguments) is returning the correct values for your system, it's just returning the correct values in the case whether the Aero theme is disabled. By turning on Aero, you get beefier borders and taller window captions, all the name of juicy graphical goodness.

If you want to get the correct size of these window elements, regardless of the user's current theme (remember, you can run Windows Vista and beyond with the Classic theme, the Aero Basic theme, or the full Aero theme, all of which are going to have different-sized UI elements), you need to use a different method available in Vista and later.

You need to send the window a WM_GETTITLEBARINFOEX message in order to request extended title bar information. The wParam is unused and should be zero. The lParam contains a pointer to a TITLEBARINFOEX structure that will receive all of the information. The caller is responsible for allocating memory for this structure and setting its cbSize member.

To do all of this from a .NET application, you'll obviously need to do some P/Invoke. Start by defining the constants you need, as well as the TITLEBARINFOEX structure:

internal const int WM_GETTITLEBARINFOEX = 0x033F;internal const int CCHILDREN_TITLEBAR = 5;[StructLayout(LayoutKind.Sequential)]internal struct TITLEBARINFOEX{    public int cbSize;    public Rectangle rcTitleBar;    [MarshalAs(UnmanagedType.ByValArray, SizeConst = CCHILDREN_TITLEBAR + 1)]    public int[] rgstate;    [MarshalAs(UnmanagedType.ByValArray, SizeConst = CCHILDREN_TITLEBAR + 1)]    public Rectangle[] rgrect;}

Then define the SendMessage function accordingly:

[DllImport("user32.dll", CharSet = CharSet.Auto)]internal static extern IntPtr SendMessage(                                          IntPtr hWnd,                                          int uMsg,                                          IntPtr wParam,                                          ref TITLEBARINFOEX lParam);

And finally, you can call all of that mess using something like the following code:

internal static TITLEBARINFOEX GetTitleBarInfoEx(IntPtr hWnd){    // Create and initialize the structure    TITLEBARINFOEX tbi = new TITLEBARINFOEX();    tbi.cbSize = Marshal.SizeOf(typeof(TITLEBARINFOEX));    // Send the WM_GETTITLEBARINFOEX message    SendMessage(hWnd, WM_GETTITLEBARINFOEX, IntPtr.Zero, ref tbi);    // Return the filled-in structure    return tbi;}

EDIT: Now tested and working on my notebook running Windows 7.


Refer to the following:

http://blogs.microsoft.co.il/blogs/alex_golesh/archive/2009/09/20/wpf-quick-tip-how-to-get-wpf-window-client-area-size.aspx

I presume you are trying to calculate the size you must make the Applications Window in order to give the right amount of client area to fully show some WPF content?

If so, then just remember that WPF's pixels are at 96dpi, and your display may be running at a different dpi...also as mentioned by other answers the theme affects how big you'd have to size your main Window to get your desired client area.

Alternatively, you might be able to use MinWidth/MinHeight on the child control of the Window.