[英]How to determine the screen width/height using C#
I want to set the width & height of a Window
dynamically based on the user screens maximum width/height.我想根据用户屏幕的最大宽度/高度动态设置Window
的宽度和高度。 How can I determine this programmatically?如何以编程方式确定这一点?
For the primary screen:对于主屏幕:
System.Windows.SystemParameters.PrimaryScreenWidth
System.Windows.SystemParameters.PrimaryScreenHeight
( Note that there are also some other primary screen related properties which depend on various factors, Full*
& Maximised*
) (请注意,还有一些其他与主屏幕相关的属性取决于各种因素, Full*
和Maximised*
)
Virtual screen:虚拟屏幕:
SystemParameters.VirtualScreenWidth
SystemParameters.VirtualScreenHeight
If you want the specific dimensions of the monitor your program is running on (if someone is running more than one monitor) you could also use:如果您想要运行程序的监视器的特定尺寸(如果有人运行多个监视器),您还可以使用:
var helper = new WindowInteropHelper(this); //this being the wpf form
var currentScreen = Screen.FromHandle(helper.Handle);
This will return a screen object referencing the monitor the program is running on.这将返回一个屏幕 object 引用正在运行程序的监视器。 From there you can use the currentScreen.Bounds.Width
/ Height
property (for the full size) or the currentScreen.WorkingArea.Width
/ Height
(minus task bar, etc.) depending on what you want.从那里您可以根据需要使用currentScreen.Bounds.Width
/ Height
属性(全尺寸)或currentScreen.WorkingArea.Width
/ Height
(减去任务栏等)。
use Screen Object使用屏幕 Object
Screen.PrimaryScreen.Bounds.Width
You can use the SizeChanged
event您可以使用SizeChanged
事件
SizeChanged="MyWindow_SizeChanged"
Then in your event handler,然后在您的事件处理程序中,
private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (this.MinWidth > 0 && this.MinHeight > 0)
{
double heightScaleFactor = e.NewSize.Height / this.MinHeight;
double widthScaleFactor = e.NewSize.Width / this.MinWidth;
mainGrid.LayoutTransform = new ScaleTransform(heightScaleFactor, widthScaleFactor);
}
}
where MainGrid
is a container for all the contents in MyWindow
.其中MainGrid
是MyWindow
中所有内容的容器。
I couldn't use any of the solutions above under .NET 4.0.30319.42000 with Windows 10 Enterprise when calling it from the Ranorex Studio 8.0.1+git.8a3e1a6f, so I used the line当从 Ranorex Studio 8.0.1+git.8a3e1a6f 调用它时,我无法在 .NET 4.0.30319.42000 和 Windows 10 Enterprise 下使用上述任何解决方案,所以我使用了该行
using WinForms = System.Windows.Forms;
[…]
SetWindowPos(processes[0].MainWindowHandle,
0,
y,
x,
WinForms.SystemInformation.PrimaryMonitorSize.Width,
WinForms.SystemInformation.PrimaryMonitorSize.Height,
SWP.SHOWWINDOW);
You can get the screen height and width:你可以得到屏幕的高度和宽度:
int height = System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Height;
int width = System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Width;
Then set the Window's Height
and Width
properties to those in the Initialization.然后将 Window 的Height
和Width
属性设置为 Initialization 中的属性。
this.Height = height;
this.Width = width;
Works to get the screen's height and width in WinForms or in ASP .NET.用于在 WinForms 或 ASP .NET 中获取屏幕的高度和宽度。 No muss, no fuss, except you'll need to reference the System.Windows.Forms
assembly in your project if it's not a WinForm project.没有麻烦,没有大惊小怪,除非您需要在您的项目中引用System.Windows.Forms
程序集,如果它不是 WinForm 项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.