Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用C确定屏幕宽度/高度#_C#_Wpf_Screen_Css - Fatal编程技术网

C# 如何使用C确定屏幕宽度/高度#

C# 如何使用C确定屏幕宽度/高度#,c#,wpf,screen,css,C#,Wpf,Screen,Css,我想根据用户屏幕的最大宽度/高度动态设置窗口的宽度和高度。如何以编程方式确定此值?对于主屏幕: System.Windows.SystemParameters.PrimaryScreenWidth System.Windows.SystemParameters.PrimaryScreenHeight (请注意,还存在一些其他与主屏幕相关的属性,这些属性取决于各种因素,Full*&最大化*) 虚拟屏幕: SystemParameters.VirtualScreenWidth SystemPara

我想根据用户屏幕的最大宽度/高度动态设置
窗口的宽度和高度。如何以编程方式确定此值?

对于主屏幕:

System.Windows.SystemParameters.PrimaryScreenWidth
System.Windows.SystemParameters.PrimaryScreenHeight
(请注意,还存在一些其他与主屏幕相关的属性,这些属性取决于各种因素,
Full*
&
最大化*

虚拟屏幕:

SystemParameters.VirtualScreenWidth
SystemParameters.VirtualScreenHeight
使用屏幕对象

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

中所有内容的容器。如果需要运行程序的监视器的特定尺寸(如果有人运行多个监视器),还可以使用:

var helper = new WindowInteropHelper(this); //this being the wpf form 
var currentScreen = Screen.FromHandle(helper.Handle);

这将返回一个屏幕对象,该对象引用正在运行程序的监视器。从这里,您可以使用
currentScreen.Bounds.Width
/
Height
属性(用于全尺寸)或
currentScreen.WorkingArea.Width
/
Height
(减去任务栏等),具体取决于您的需要

从Ranorex Studio 8.0.1+git.8a3e1a6f调用Windows 10 Enterprise时,我无法在.NET 4.0.30319.42000下使用上述任何解决方案,因此我使用了该行

using WinForms = System.Windows.Forms;
[…]
                SetWindowPos(processes[0].MainWindowHandle,
                    0,
                    y,
                    x,
                    WinForms.SystemInformation.PrimaryMonitorSize.Width,
                    WinForms.SystemInformation.PrimaryMonitorSize.Height,
                    SWP.SHOWWINDOW);

您可以获得屏幕高度和宽度:

int height = System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Height;
int width = System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Width;
然后将窗口的
高度
宽度
属性设置为初始化中的属性

this.Height = height;
this.Width = width;

用于在WinForms或ASP.NET中获取屏幕的高度和宽度。没有混乱,没有麻烦,除非你需要在你的项目中引用
System.Windows.Forms
程序集,如果它不是WinForm项目。

最大化你的窗口是不可接受的吗?@Lazarus:不,不适用于我想处理的情况。基于当前的解决方案呢?如何得到它?我想这确实使用了当前的解决方案,你试过了吗?终于找到了这个问题的答案!当用户在应用程序打开时更改缩放比例时,VirtualScreenWidth[Height]是否不会更新?在Win10上,似乎您必须重新启动应用程序才能重新填充这些值。@Hashman:无法告诉您任何有关这方面的信息,甚至没有win 10设置。不确定这是如何解决问题的。这是一篇有效的帖子,但正如Lazarus所提到的。。“它没有解决这个问题”:)这很好