如何获得活动屏幕尺寸?

chi*_*emp 132 c# wpf

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

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

Jef*_*tes 137

Screen.FromControl,Screen.FromPointScreen.FromRectangle应该帮助你.例如在WinForms中它将是:

class MyForm : Form
{
  public Rectangle GetScreen()
  {
    return Screen.FromControl(this).Bounds;
  }
}
Run Code Online (Sandbox Code Playgroud)

我不知道WPF的等效调用.因此,您需要执行类似此扩展方法的操作.

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

  • 在我的Windows 10 Pro(v10.0.14393)上的4监视器系统上面向.NET 4.5的VS 2015 WPF应用程序中,监视器上显示"window"_我的主要部分(例如,它的"顶部<0"),"FromHandle"返回我的主显示器的`屏幕`(即使`窗口`在辅助显示器中_完全_)!?!叹.看起来我必须自己搜索`Screen.AllScreens`数组.为什么事情不能"正常"?!?Arrrrgh. (5认同)

Pyt*_*oll 60

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

System.Windows.SystemParameters.WorkArea

这对于获得主屏幕的大小也很有用:

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

  • 我很困惑......这似乎只能返回主要的屏幕尺寸.我想知道窗口当前处于的屏幕尺寸... (17认同)
  • 这并不能回答问题,即使您只想获取主显示器的大小,系统参数(在 WPF 中)也不正确。它们返回与设备无关的单位,而不是像素。为了更好的实现,请参阅这个答案:http://stackoverflow.com/questions/254197/how-can-i-get-the-active-screen-dimensions/254241#254241 (2认同)
  • PrimaryScreenHeight/Width 的工作方式与预期完全一致,MSDN 对此有以下说明:“获取一个指示主显示监视器的屏幕高度(以像素为单位)的值。” WorkArea 并没有具体说像素,但是文档和使用示例让我相信它也是以像素为单位的。您是否有指示使用设备独立单元的链接? (2认同)

742*_*742 33

您可能还需要:

获得所有显示器的组合尺寸,而不是特别是一个.

  • **提示:** 要使用它,需要添加引用 `PresentationFramework.dll` 和 `using System.Windows;` (2认同)

R.R*_*sev 17

添加不使用WinForms但使用NativeMethods的解决方案.首先,您需要定义所需的本机方法.

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后像这样获取监视器句柄和监视器信息.

        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 );
        }
Run Code Online (Sandbox Code Playgroud)

  • 如果你的窗口有比例因子(100%/125%/150%/200%),你能得到真实的屏幕尺寸吗? (2认同)

fau*_*lty 12

添加到ffpf

Screen.FromControl(this).Bounds
Run Code Online (Sandbox Code Playgroud)


aDo*_*eSo 11

注意窗户的比例因子(100%/ 125%/ 150%/ 200%).您可以使用以下代码获取实际屏幕大小:

SystemParameters.FullPrimaryScreenHeight
SystemParameters.FullPrimaryScreenWidth
Run Code Online (Sandbox Code Playgroud)

  • 这是针对主屏幕的 - 如果您的应用程序窗口位于虚拟(扩展)屏幕上(即您的 PC 连接了一个或两个外部显示器)怎么办? (5认同)

小智 5

我想在打开第一个窗口之前获得屏幕分辨率,因此这里有一个快速解决方案,可以在实际测量屏幕尺寸之前打开一个不可见的窗口(您需要根据窗口调整窗口参数,以确保两个窗口都打开)相同的屏幕 - 主要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();
Run Code Online (Sandbox Code Playgroud)