我如何获得活动的屏幕尺寸?

我正在寻找的是相当于System.Windows.SystemParameters.WorkArea的窗口当前所在的监视器。

澄清:有问题的窗口是WPF ,而不是WinForm

Screen.FromControlScreen.FromPointScreen.FromRectangle应该可以帮助你。 例如在WinForms中,它将是:

 class MyForm : Form { public Rectangle GetScreen() { return Screen.FromControl(this).Bounds; } } 

我不知道有一个WPF的等价电话。 所以,你需要做这样的扩展方法。

 static class ExtensionsForWPF { public static System.Windows.Forms.Screen GetScreen(this Window window) { return System.Windows.Forms.Screen.FromHandle(new WindowInteropHelper(window).Handle); } } 

您可以使用它来获取主屏幕的桌面工作区范围:

System.Windows.SystemParameters.WorkArea

这对于获取主屏幕的大小也是有用的:

System.Windows.SystemParameters.PrimaryScreenWidth System.Windows.SystemParameters.PrimaryScreenHeight

你也可能需要:

  • SystemParameters.VirtualScreenWidth
  • SystemParameters.VirtualScreenHeight

得到所有显示器的组合大小,而不是特别的。

添加到ffpf

 Screen.FromControl(this).Bounds 

当心你窗户的比例因子(100%/ 125%/ 150%/ 200%)。 您可以通过使用以下代码获取真实的屏幕大小:

 SystemParameters.FullPrimaryScreenHeight SystemParameters.FullPrimaryScreenWidth 

添加一个不使用WinForms而是NativeMethods的解决scheme。 首先你需要定义所需的本地方法。

 public static class NativeMethods { public const Int32 MONITOR_DEFAULTTOPRIMERTY = 0x00000001; public const Int32 MONITOR_DEFAULTTONEAREST = 0x00000002; [DllImport( "user32.dll" )] public static extern IntPtr MonitorFromWindow( IntPtr handle, Int32 flags ); [DllImport( "user32.dll" )] public static extern Boolean GetMonitorInfo( IntPtr hMonitor, NativeMonitorInfo lpmi ); [Serializable, StructLayout( LayoutKind.Sequential )] public struct NativeRectangle { public Int32 Left; public Int32 Top; public Int32 Right; public Int32 Bottom; public NativeRectangle( Int32 left, Int32 top, Int32 right, Int32 bottom ) { this.Left = left; this.Top = top; this.Right = right; this.Bottom = bottom; } } [StructLayout( LayoutKind.Sequential, CharSet = CharSet.Auto )] public sealed class NativeMonitorInfo { public Int32 Size = Marshal.SizeOf( typeof( NativeMonitorInfo ) ); public NativeRectangle Monitor; public NativeRectangle Work; public Int32 Flags; } } 

然后获取监视器句柄和监视器信息。

  var hwnd = new WindowInteropHelper( this ).EnsureHandle(); var monitor = NativeMethods.MonitorFromWindow( hwnd, NativeMethods.MONITOR_DEFAULTTONEAREST ); if ( monitor != IntPtr.Zero ) { var monitorInfo = new NativeMonitorInfo(); NativeMethods.GetMonitorInfo( monitor, monitorInfo ); var left = monitorInfo.Monitor.Left; var top = monitorInfo.Monitor.Top; var width = ( monitorInfo.Monitor.Right - monitorInfo.Monitor.Left ); var height = ( monitorInfo.Monitor.Bottom - monitorInfo.Monitor.Top ); } 

我想在打开我的第一个窗口之前获得屏幕分辨率,所以这里有一个快速的解决scheme,在实际测量屏幕尺寸之前打开一个不可见的窗口(您需要调整窗口参数到您的窗口,以确保两者都打开相同的屏幕 – 主要是WindowStartupLocation是重要的)

 Window w = new Window(); w.ResizeMode = ResizeMode.NoResize; w.WindowState = WindowState.Normal; w.WindowStyle = WindowStyle.None; w.Background = Brushes.Transparent; w.Width = 0; w.Height = 0; w.AllowsTransparency = true; w.IsHitTestVisible = false; w.WindowStartupLocation = WindowStartupLocation.Manual; w.Show(); Screen scr = Screen.FromHandle(new WindowInteropHelper(w).Handle); w.Close(); 

我需要设置我的窗口应用程序的最大大小。 这一个可以相应地改变应用程序已被显示在主屏幕或中学。 为了克服这个问题,e创build了一个简单的方法,我接下来给你看:

 /// <summary> /// Set the max size of the application window taking into account the current monitor /// </summary> public static void SetMaxSizeWindow(ioConnect _receiver) { Point absoluteScreenPos = _receiver.PointToScreen(Mouse.GetPosition(_receiver)); if (System.Windows.SystemParameters.VirtualScreenLeft == System.Windows.SystemParameters.WorkArea.Left) { //Primary Monitor is on the Left if (absoluteScreenPos.X <= System.Windows.SystemParameters.PrimaryScreenWidth) { //Primary monitor _receiver.WindowApplication.MaxWidth = System.Windows.SystemParameters.WorkArea.Width; _receiver.WindowApplication.MaxHeight = System.Windows.SystemParameters.WorkArea.Height; } else { //Secondary monitor _receiver.WindowApplication.MaxWidth = System.Windows.SystemParameters.VirtualScreenWidth - System.Windows.SystemParameters.WorkArea.Width; _receiver.WindowApplication.MaxHeight = System.Windows.SystemParameters.VirtualScreenHeight; } } if (System.Windows.SystemParameters.VirtualScreenLeft < 0) { //Primary Monitor is on the Right if (absoluteScreenPos.X > 0) { //Primary monitor _receiver.WindowApplication.MaxWidth = System.Windows.SystemParameters.WorkArea.Width; _receiver.WindowApplication.MaxHeight = System.Windows.SystemParameters.WorkArea.Height; } else { //Secondary monitor _receiver.WindowApplication.MaxWidth = System.Windows.SystemParameters.VirtualScreenWidth - System.Windows.SystemParameters.WorkArea.Width; _receiver.WindowApplication.MaxHeight = System.Windows.SystemParameters.VirtualScreenHeight; } } } 

这是一个“中心屏幕DotNet 4.5解决scheme ”,使用SystemParameters而不是System.Windows.Forms或My.Compuer.Screen :由于Windows 8已经改变了屏幕尺寸计算,唯一的办法是我看起来像这样(任务栏计算包括):

 Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded Dim BarWidth As Double = SystemParameters.VirtualScreenWidth - SystemParameters.WorkArea.Width Dim BarHeight As Double = SystemParameters.VirtualScreenHeight - SystemParameters.WorkArea.Height Me.Left = (SystemParameters.VirtualScreenWidth - Me.ActualWidth - BarWidth) / 2 Me.Top = (SystemParameters.VirtualScreenHeight - Me.ActualHeight - BarHeight) / 2 End Sub 

中心屏幕WPF XAML

在C#winforms中,我有起点(如果我们有几个监视器/ diplay和一个窗体调用另一个)的情况下帮助下面的方法:

 private Point get_start_point() { return new Point(Screen.GetBounds(parent_class_with_form.ActiveForm).X, Screen.GetBounds(parent_class_with_form.ActiveForm).Y ); }