How to get the height of the title bar of the main application windows?
Asked Answered
M

3

6

I'm using prism to load views to region. The problem is the loaded view overlapped the title bar of the main windows - the bar contains caption, close/minimize/maximize buttons. How can I get the title bar's height? Prefer to get it right in the xaml codes.

Miscreance answered 10/10, 2010 at 8:59 Comment(0)
M
17

After a while, I figure it out:

<Window xmlns:local="clr-namespace:System.Windows;assembly=PresentationFramework">
  <YourView Height="{x:Static local:SystemParameters.WindowCaptionHeight}" />
</Window>

Hope that helps!

Miscreance answered 11/10, 2010 at 6:32 Comment(3)
On my Intel Core i7-6700 CPU@ 3.40GHz running Windows 10 Pro 64-Bit (Version 10.0.15063) and Visual Studio 2015 Enterprise (Version 14.0.25431.01 Update 3) targeting .NET Framework 4.5.2 with ~16 GB RAM free and ~110 GB HD free, System.Windows.SystemParameters.WindowCaptionHeight returned 23 vs. the 39 that I verified via the Debugger. My XAML has as its root Element a Window with 0's for Margin, BorderThickness and Padding and its Content Element is a DockPanel with 0's for Margin. The DockPanel's ActualHeight was 39 < than the Window's.Sarchet
Exactly the same here.Commerce
Please try this instead!Conservatory
C
1

SystemParameters.WindowCaptionHeight is in pixels whereas WPF needs screen cordinates. You have to convert it!

<Grid>
    <Grid.Resources>
        <wpfApp1:Pixel2ScreenConverter x:Key="Pixel2ScreenConverter" />
    </Grid.Resources>
    <YourView Height="{Binding Source={x:Static SystemParameters.WindowCaptionHeight},Converter={StaticResource Pixel2ScreenConverter}}" />
</Grid>

aa

public class Pixel2ScreenConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        double pixels = (double) value;
        bool horizontal = Equals(parameter, true);

        double points = 0d;

        // NOTE: Ideally, we would get the source from a visual:
        // source = PresentationSource.FromVisual(visual);
        //
        using (var source = new HwndSource(new HwndSourceParameters()))
        {
            var matrix = source.CompositionTarget?.TransformToDevice;
            if (matrix.HasValue)
            {
                points = pixels * (horizontal ? matrix.Value.M11 : matrix.Value.M22);
            }
        }

        return points;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
Conservatory answered 29/11, 2018 at 16:2 Comment(0)
M
0

I think you can do this since .NET Framework 4.5.

Reference: https://learn.microsoft.com/en-us/dotnet/api/system.windows.shell.windowchrome.captionheight?view=windowsdesktop-7.0#system-windows-shell-windowchrome-captionheight

Here is what you can do:

double title_height = (new WindowChrome()).CaptionHeight;
Magnoliamagnoliaceous answered 21/12, 2022 at 17:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.