如何在WPF中获取当前屏幕的尺寸?

114
我知道可以使用以下方法获取主屏幕的尺寸:
System.Windows.SystemParameters.PrimaryScreenWidth;
System.Windows.SystemParameters.PrimaryScreenHeight;

但是我如何获取当前屏幕的大小?(多屏用户并不总是使用主屏幕,而且并不是所有屏幕都使用相同的分辨率,对吗?)

能够从XAML访问大小会很好,但从代码(C#)中获取也可以。


1
定义“current”。一个窗口可以同时出现在多个屏幕上。 - Jim Balter
16个回答

1

我明白需求。

问题是,有一些WPF方法可以获取这些值 - 但是,确实如其中一位贡献者所说,不能直接获取。

解决方案不是获取所有这些变通方法,而是根据清晰的设计和开发来改变初始方法。

A)将初始主窗口设置为屏幕

B)获取包括大量有用的WPF方法在内的ActualWindow的值

C)您可以添加任意数量的窗口以获得所需的行为,例如可调整大小,最小化等...但现在您始终可以访问已加载和呈现的屏幕

请注意以下示例,周围有一些代码使使用此类方法成为必要,但它应该有效(它将为您提供屏幕每个角的点):

单个,双显示器和不同分辨率上的工作示例(在原始主窗口类中):

InitializeComponent();
[…]
ActualWindow.AddHandler(Window.LoadedEvent, new RoutedEventHandler(StartUpScreenLoaded));

路由事件:
private void StartUpScreenLoaded(object sender, RoutedEventArgs e)
    {
        Window StartUpScreen = sender as Window;

        // Dispatcher Format B:
        Dispatcher.Invoke(new Action(() =>
        {
            // Get Actual Window on Loaded
            StartUpScreen.InvalidateVisual();
            System.Windows.Point CoordinatesTopRight = StartUpScreen.TranslatePoint(new System.Windows.Point((StartUpScreen.ActualWidth), (0d)), ActualWindow);
            System.Windows.Point CoordinatesBottomRight = StartUpScreen.TranslatePoint(new System.Windows.Point((StartUpScreen.ActualWidth), (StartUpScreen.ActualHeight)), ActualWindow);
            System.Windows.Point CoordinatesBottomLeft = StartUpScreen.TranslatePoint(new System.Windows.Point((0d), (StartUpScreen.ActualHeight)), ActualWindow);

            // Set the Canvas Top Right, Bottom Right, Bottom Left Coordinates
            System.Windows.Application.Current.Resources["StartUpScreenPointTopRight"] = CoordinatesTopRight;
            System.Windows.Application.Current.Resources["StartUpScreenPointBottomRight"] = CoordinatesBottomRight;
            System.Windows.Application.Current.Resources["StartUpScreenPointBottomLeft"] = CoordinatesBottomLeft;
        }), DispatcherPriority.Loaded);
    }

这里的“Screen”是指窗口,而不是窗口所在的物理显示器,对吗? - Nils

1
如果您使用任何全屏窗口(其WindowState = WindowState.Maximized, WindowStyle = WindowStyle.None),您可以像这样将其内容包装在System.Windows.Controls.Canvas中:
<Canvas Name="MyCanvas" Width="auto" Height="auto">
...
</Canvas>

然后,您可以使用MyCanvas.ActualWidthMyCanvas.ActualHeight来获取当前屏幕的分辨率,考虑DPI设置并以设备独立单位表示。它不会添加任何边距,因为最大化的窗口本身就已经有了。

(Canvas接受UIElement作为子元素,因此您应该能够将其与任何内容一起使用。)


0

对于 WPF 应用(.NET框架)

 double H;
 double W;

你可以声明一个名为SizeChanged窗口事件(跟踪客户端屏幕大小的变化)。
这样,你就可以在运行时获取屏幕的大小。
private void WindowSizeChange(object sender, SizeChangedEventArgs e)
{
     double H = this.ActualHeight;
     double W = this.ActualWidth;
}

欢迎来到SO。这是否跟踪WPF窗口的大小或窗口所在的当前屏幕? - Nils

-2
在XAML中将窗口居中 WindowStartupLocation="CenterOwner" 然后在WindowLoaded()中调用 double ScreenHeight = 2 * (Top + 0.5 * Height);

-6

它可以与之配合使用

this.Width = System.Windows.SystemParameters.VirtualScreenWidth;
this.Height = System.Windows.SystemParameters.VirtualScreenHeight;

在2个显示器上进行了测试。


1
如果您查看2010年5月18日15:52的答案,它与您的完全相同,您会发现VirtualScreen跨越所有屏幕 - 因此,如果您有多个屏幕,这永远不起作用! - Nils

-6
double screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
double screenhight= System.Windows.SystemParameters.PrimaryScreenHeight;

5
和之前的回答一样,这只针对于“主”屏幕。我需要目前正在使用的屏幕。 - Nils

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接