【发布时间】:2021-09-26 22:24:11
【问题描述】:
我在 Powershell 中设计了以下表单的变体。但是,当我使用 ISE 运行它时,它会以与 Powershell 控制台不同的大小/分辨率弹出。我一直在尝试通过动态调整不同字体和控件的高度/宽度来控制这一点,但即使其他所有内容都正确调整,[system.Windows.Forms.Label] 控件的字体也不会同步调整与其他一切。
经过调查,应用程序似乎以不同的分辨率选择主屏幕,尽管辅助屏幕对两者都是一致的。为什么这两个应用程序会检测不同的主屏幕分辨率?
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Screen]::PrimaryScreen
Powershell 输出:
BitsPerPixel : 32
Bounds : {X=0,Y=0,Width=1368,Height=912}
DeviceName : \\.\DISPLAY1
Primary : True
WorkingArea : {X=0,Y=0,Width=1368,Height=882}
ISE 输出:
BitsPerPixel : 32
Bounds : {X=0,Y=0,Width=2736,Height=1824}
DeviceName : \\.\DISPLAY1
Primary : True
WorkingArea : {X=0,Y=0,Width=2736,Height=1764}
表单代码
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$monitor = [System.Windows.Forms.Screen]::PrimaryScreen # PrimaryScreen producing inconsistent results between Powershell/ISE
$w = $monitor.WorkingArea.Width / 2000
$h = $monitor.WorkingArea.Height / 1200
[System.Windows.Forms.Application]::EnableVisualStyles()
$validatorForm = New-Object System.Windows.Forms.Form
$validatorForm.Size = New-Object 'System.Drawing.Size' (2020*$w), (1220*$h)
$validatorForm.text = "Validator"
$validatorForm.BackColor = "#D0E0F0"
$validatorForm.TopMost = $true
$validatorForm.AutoScale = $true
$FontLabel = "Microsoft Sans Serif,$(11*$w)"
$FontText = "Microsoft Sans Serif,$(20*$w)"
$InputLabel = New-Object system.Windows.Forms.Label
$InputLabel.location = New-Object System.Drawing.Point((20*$w),(40*$h))
$InputLabel.width = 635*$w
$InputLabel.height = 40*$h
$InputLabel.Font = $FontLabel
$InputLabel.ForeColor = '#001166'
$InputLabel.text = "Copy + Paste an AF Element into this text box:"
$userInput = New-Object system.Windows.Forms.TextBox
$userInput.location = New-Object System.Drawing.Point((660*$w),(40*$h))
$userInput.Font = $FontText
$userInput.width = 1320*$w
$userInput.height = 40*$h
$userInput.text = '\\ExampleServer\Database\Path'
$SubmitButton = New-Object System.Windows.Forms.Button
$SubmitButton.location = New-Object System.Drawing.Point((660*$w),(130*$h))
$SubmitButton.width = 400*$w
$SubmitButton.height = 60*$h
$SubmitButton.Font = $LabelText
$SubmitButton.Text = "Run Validation"
$SubmitButton.BackColor = "#BBDDBB"
$validatorForm.controls.AddRange(@($InputLabel,$userInput,$SubmitButton))
$validatorForm.AcceptButton = $SubmitButton
[system.windows.forms.application]::run($validatorForm)
$validatorForm = New-Object System.Windows.Forms.Form
$validatorForm.Size = New-Object 'System.Drawing.Size' (2020*$w), (1220*$h)
$validatorForm.text = "Validator"
$validatorForm.BackColor = "#D0E0F0"
$validatorForm.TopMost = $true
$validatorForm.AutoScale = $true
$FontLabel = "Microsoft Sans Serif,$(11*$w)"
$FontText = "Microsoft Sans Serif,$(20*$w)"
$InputLabel = New-Object system.Windows.Forms.Label
$InputLabel.location = New-Object System.Drawing.Point((20*$w),(40*$h))
$InputLabel.width = 635*$w
$InputLabel.height = 40*$h
$InputLabel.Font = $FontLabel
$InputLabel.ForeColor = '#001166'
$InputLabel.text = "Copy + Paste an element into this text box:"
$userInput = New-Object system.Windows.Forms.TextBox
$userInput.location = New-Object System.Drawing.Point((660*$w),(40*$h))
$userInput.Font = $FontText
$userInput.width = 1320*$w
$userInput.height = 40*$h
$userInput.text = '\\ExampleServer\Database\Path'
$SubmitButton = New-Object System.Windows.Forms.Button
$SubmitButton.location = New-Object System.Drawing.Point((660*$w),(130*$h))
$SubmitButton.width = 400*$w
$SubmitButton.height = 60*$h
$SubmitButton.Font = $LabelText
$SubmitButton.Text = "Run Validation"
$SubmitButton.BackColor = "#BBDDBB"
$validatorForm.controls.AddRange(@($InputLabel,$userInput,$SubmitButton))
$validatorForm.AcceptButton = $SubmitButton
[system.windows.forms.application]::run($validatorForm)
【问题讨论】:
-
无法重现此内容。这两个对我来说都是一样的大小。尝试将其分解为最小的示例。也许您甚至会在途中发现问题。
-
@marsze - 我认为这相当小!我有两个分辨率不同的显示器,所以我的一个理论是每个应用程序都在选择不同的默认分辨率或类似的东西。两者都在同一屏幕上弹出打开的表单,但两个应用程序的控制台都有自己喜欢的监视器。
-
@iRon - 抱歉,不,我不小心从上面的缩减示例脚本中删除了该行,但我已经尝试过了,为了安全起见再次尝试。跨度>
-
@marsze - 我找到了问题,但不确定是什么原因造成的。现在包括有问题的细节。为什么这两个应用程序会为主监视器 (DISPLAY1) 选择不同的显示分辨率?
-
@marsze - 已将每个的输出粘贴到上述问题中。在检查 Windows 设置时,看起来 ISE 正在获得“显示设置”上显示的“真实”分辨率。但是,由于显示器具有极高的 DPI,Windows 将应用 200%(推荐)比例(显示 > 比例和布局)。那么也许 PowerShell 正在采用这种伪分辨率但没有应用缩放?也许如果您更改显示器上的缩放比例,它会重现?
标签: forms powershell winforms multiple-monitors