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

jmp7cifd  于 2022-12-24  发布在  其他
关注(0)|答案(3)|浏览(449)

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

xzlaal3s

xzlaal3s1#

过了一会儿,我想通了:

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

希望能有所帮助!

uemypmqf

uemypmqf2#

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>

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

tkclm6bt3#

我认为从. 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
以下是您可以执行的操作:

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

相关问题