如何获取主应用程序窗口标题栏的高度?

发布于 2024-09-26 12:02:39 字数 96 浏览 2 评论 0原文

我正在使用棱镜将视图加载到区域。问题是加载的视图与主窗口的标题栏重叠 - 该栏包含标题、关闭/最小化/最大化按钮。如何获取标题栏的高度?更愿意在 xaml 代码中得到正确的结果。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜灵血窟げ 2024-10-03 12:02:39

过了一会儿,我明白了:

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

希望有帮助!

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!

不…忘初心 2024-10-03 12:02:39

SystemParameters.WindowCaptionHeight 以像素为单位,而 WPF 需要屏幕坐标。你必须转换它!

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

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();
    }
}

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();
    }
}
苦笑流年记忆 2024-10-03 12:02:39

我认为从 .NET Framework 4.5 开始你就可以做到这一点。

参考:

这是您可以执行的操作:

double title_height = (new WindowChrome()).CaptionHeight;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文