首页 > 后端开发 > C++ > WPF中如何高效获取当前屏幕尺寸?

WPF中如何高效获取当前屏幕尺寸?

Linda Hamilton
发布: 2025-01-04 14:03:39
原创
917 人浏览过

How to Efficiently Get the Current Screen's Dimensions in WPF?

WPF:获取当前屏幕的尺寸

使用 SystemParameters 检索主屏幕的尺寸很简单,但确定当前屏幕的尺寸屏幕可能更复杂,尤其是在多屏幕配置中。为了应对这一挑战,请考虑以下方法:

利用 WinForms 屏幕包装器

围绕 System.Windows.Forms.Screen 对象创建与 WPF 兼容的包装器访问屏幕相关信息的便捷方式。下面是一个示例实现:

public class WpfScreen
{
    public static IEnumerable<WpfScreen> AllScreens()
    {
        foreach (Screen screen in System.Windows.Forms.Screen.AllScreens)
        {
            yield return new WpfScreen(screen);
        }
    }

    public static WpfScreen GetScreenFrom(Window window)
    {
        WindowInteropHelper windowInteropHelper = new WindowInteropHelper(window);
        Screen screen = System.Windows.Forms.Screen.FromHandle(windowInteropHelper.Handle);
        WpfScreen wpfScreen = new WpfScreen(screen);
        return wpfScreen;
    }

    public static WpfScreen GetScreenFrom(Point point)
    {
        int x = (int)Math.Round(point.X);
        int y = (int)Math.Round(point.Y);
        System.Drawing.Point drawingPoint = new System.Drawing.Point(x, y);
        Screen screen = System.Windows.Forms.Screen.FromPoint(drawingPoint);
        WpfScreen wpfScreen = new WpfScreen(screen);
        return wpfScreen;
    }

    public static WpfScreen Primary
    {
        get { return new WpfScreen(System.Windows.Forms.Screen.PrimaryScreen); }
    }

    private readonly Screen screen;

    internal WpfScreen(System.Windows.Forms.Screen screen)
    {
        this.screen = screen;
    }

    public Rect DeviceBounds
    {
        get { return this.GetRect(this.screen.Bounds); }
    }

    public Rect WorkingArea
    {
        get { return this.GetRect(this.screen.WorkingArea); }
    }

    private Rect GetRect(Rectangle value)
    {
        return new Rect
        {
            X = value.X,
            Y = value.Y,
            Width = value.Width,
            Height = value.Height
        };
    }

    public bool IsPrimary
    {
        get { return this.screen.Primary; }
    }

    public string DeviceName
    {
        get { return this.screen.DeviceName; }
    }
}
登录后复制

在 XAML 中访问屏幕尺寸

虽然无法直接从 XAML 访问屏幕尺寸,但您可以组合使用附加属性与包装类:

<UserControl>
    <UserControl.Resources>
        <local:WpfScreenExtension x:Key="ScreenExtension" />
    </UserControl.Resources>

    <StackPanel>
        <TextBlock Text="Device Bounds Width: {Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=Width, Converter={StaticResource ScreenExtension}}" VerticalAlignment="Center" />
        <TextBlock Text="Working Area Height: {Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=Height, Converter={StaticResource ScreenExtension}}" VerticalAlignment="Center" />
    </StackPanel>
</UserControl>
登录后复制
public class WpfScreenExtension : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        UserControl userControl = value as UserControl;
        if (userControl == null)
            return null;

        Rect bounds = WpfScreen.GetScreenFrom(userControl).DeviceBounds;
        if (bounds.IsEmpty)
            return String.Empty;

        if (parameter?.ToString() == "Width")
            return bounds.Width;
        else if (parameter?.ToString() == "Height")
            return bounds.Height;
        else
            return bounds;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}
登录后复制

以上是WPF中如何高效获取当前屏幕尺寸?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板