C# 如何获得活动屏幕尺寸?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/254197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the active screen dimensions?
提问by chilltemp
What I am looking for is the equivalent of System.Windows.SystemParameters.WorkArea
for the monitor that the window is currently on.
我正在寻找的是相当于System.Windows.SystemParameters.WorkArea
窗口当前打开的监视器。
Clarification:The window in question is WPF
, not WinForm
.
澄清:有问题的窗口是WPF
,而不是WinForm
。
采纳答案by Jeff Yates
Screen.FromControl
, Screen.FromPoint
and Screen.FromRectangle
should help you with this. For example in WinForms it would be:
Screen.FromControl
,Screen.FromPoint
并且Screen.FromRectangle
应该可以帮助您解决这个问题。例如在 WinForms 中,它将是:
class MyForm : Form
{
public Rectangle GetScreen()
{
return Screen.FromControl(this).Bounds;
}
}
I don't know of an equivalent call for WPF. Therefore, you need to do something like this extension method.
我不知道对 WPF 的等效调用。因此,您需要执行类似这种扩展方法的操作。
static class ExtensionsForWPF
{
public static System.Windows.Forms.Screen GetScreen(this Window window)
{
return System.Windows.Forms.Screen.FromHandle(new WindowInteropHelper(window).Handle);
}
}
回答by faulty
Add on to ffpf
添加到ffpf
Screen.FromControl(this).Bounds
回答by Pyttroll
You can use this to get desktop workspace bounds of the primary screen:
您可以使用它来获取主屏幕的桌面工作区边界:
System.Windows.SystemParameters.WorkArea
System.Windows.SystemParameters.WorkArea
This is also useful for getting just the size of the primary screen:
这对于获取主屏幕的大小也很有用:
System.Windows.SystemParameters.PrimaryScreenWidth
System.Windows.SystemParameters.PrimaryScreenHeight
System.Windows.SystemParameters.PrimaryScreenWidth
System.Windows.SystemParameters.PrimaryScreenHeight
回答by 742
Also you may need:
您还可能需要:
to get the combined size of all monitors and not one in particular.
获得所有显示器的组合大小,而不是特别的一个。
回答by Andre
I wanted to have the screen resolution before opening the first of my windows, so here a quick solution to open an invisible window before actually measuring screen dimensions (you need to adapt the window parameters to your window in order to ensure that both are openend on the same screen - mainly the WindowStartupLocation
is important)
我想在打开第一个窗口之前获得屏幕分辨率,所以这里有一个在实际测量屏幕尺寸之前打开一个不可见窗口的快速解决方案(您需要将窗口参数调整到您的窗口以确保两者都在相同的屏幕 - 主要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();
回答by Ricardo Magalh?es
I needed to set the maximum size of my window application. This one could changed accordingly the application is is been showed in the primary screen or in the secondary. To overcome this problem e created a simple method that i show you next:
我需要设置窗口应用程序的最大大小。这可以相应地更改应用程序是显示在主屏幕或辅助屏幕中。为了克服这个问题,我创建了一个简单的方法,接下来我将向您展示:
/// <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;
}
}
}
回答by Nasenbaer
This is a "Center Screen DotNet 4.5 solution", using SystemParametersinstead of System.Windows.Formsor My.Compuer.Screen: Since Windows 8 has changedthe screen dimension calculation, the only way it works for me looks like that (Taskbar calculation included):
这是一个“Center Screen DotNet 4.5 解决方案”,使用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
回答by aDoubleSo
Beware of the scale factor of your windows (100% / 125% / 150% / 200%). You can get the real screen size by using the following code:
注意你的窗口的比例因子(100% / 125% / 150% / 200%)。您可以使用以下代码获得真实的屏幕尺寸:
SystemParameters.FullPrimaryScreenHeight
SystemParameters.FullPrimaryScreenWidth
回答by R.Rusev
Adding a solution that doesn't use WinForms but NativeMethods instead. First you need to define the native methods needed.
添加一个不使用 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;
}
}
And then get the monitor handle and the monitor info like this.
然后像这样获取监视器句柄和监视器信息。
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 );
}
回答by Oleg Bash
in C# winforms I have got start point (for case when we have several monitor/diplay and one form is calling another one) with help of the following method:
在 C# winforms 中,我在以下方法的帮助下获得了起点(例如,当我们有多个监视器/显示并且一个窗体正在调用另一个窗体时):
private Point get_start_point()
{
return
new Point(Screen.GetBounds(parent_class_with_form.ActiveForm).X,
Screen.GetBounds(parent_class_with_form.ActiveForm).Y
);
}