Jef*_*tes 137
Screen.FromControl,Screen.FromPoint并Screen.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)
Pyt*_*oll 60
您可以使用它来获取主屏幕的桌面工作区边界:
System.Windows.SystemParameters.WorkArea
这对于获得主屏幕的大小也很有用:
System.Windows.SystemParameters.PrimaryScreenWidth
System.Windows.SystemParameters.PrimaryScreenHeight
742*_*742 33
您可能还需要:
获得所有显示器的组合尺寸,而不是特别是一个.
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)
aDo*_*eSo 11
注意窗户的比例因子(100%/ 125%/ 150%/ 200%).您可以使用以下代码获取实际屏幕大小:
SystemParameters.FullPrimaryScreenHeight
SystemParameters.FullPrimaryScreenWidth
Run Code Online (Sandbox Code Playgroud)
小智 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)