如何使用C#确定屏幕宽度/高度
我想根据用户屏幕的最大宽度/高度动态设置Window
宽度和高度。 如何以编程方式确定?
对于主屏幕:
System.Windows.SystemParameters.PrimaryScreenWidth System.Windows.SystemParameters.PrimaryScreenHeight
( 请注意,还有一些其他主屏幕相关属性取决于各种因素, Full*
和Maximised*
)
虚拟屏幕:
SystemParameters.VirtualScreenWidth SystemParameters.VirtualScreenHeight
如果您想要运行程序的监视器的特定尺寸(如果某人正在运行多个监视器),您还可以使用:
var helper = new WindowInteropHelper(this); //this being the wpf form var currentScreen = Screen.FromHandle(helper.Handle);
这将返回引用程序运行的监视器的屏幕对象。 从那里你可以使用currentScreen.Bounds.Width
/ Height
属性(对于完整大小)或currentScreen.WorkingArea.Width
/ Height
(减去任务栏等),具体取决于你想要的。
使用屏幕对象
Screen.PrimaryScreen.Bounds.Width
您可以使用SizeChanged
事件
SizeChanged="MyWindow_SizeChanged"
然后在你的事件处理程序中
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); } }
其中MainGrid
是MyWindow
所有内容的容器。
从Ranorex Studio 8.0.1 + git.8a3e1a6f调用它时,我无法使用.NET 4.0.30319.42000上的任何解决方案与Windows 10企业版,所以我使用了该行
using WinForms = System.Windows.Forms; […] SetWindowPos(processes[0].MainWindowHandle, 0, y, x, WinForms.SystemInformation.PrimaryMonitorSize.Width, WinForms.SystemInformation.PrimaryMonitorSize.Height, SWP.SHOWWINDOW);