我正在寻找的是相当于System.Windows.SystemParameters.WorkArea
窗口当前所在的监视器.
澄清: 有问题的窗口WPF
不是WinForm
.
Screen.FromControl
,Screen.FromPoint
并Screen.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
获得所有显示器的组合尺寸,而不是特别是一个.
添加不使用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; } }
然后像这样获取监视器句柄和监视器信息.
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 ); }
添加到ffpf
Screen.FromControl(this).Bounds
注意窗户的比例因子(100%/ 125%/ 150%/ 200%).您可以使用以下代码获取实际屏幕大小:
SystemParameters.FullPrimaryScreenHeight SystemParameters.FullPrimaryScreenWidth