导致表单问题的监视器大小差异:Powershell 与 ISE

Monitor Size Discrepancy causing Form Issues: Powershell vs ISE

我在 Powershell 中设计了以下表单的变体。但是,当我使用 ISE 运行 它时,它会在与 Powershell 控制台不同的 size/resolution 中弹出。我一直试图通过动态调整不同字体和控件的 height/width 来对此进行控制,但即使其他一切都调整正确,[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)

我仍然不完全确定为什么检测到的主显示器分辨率在 ISE/Powershell 之间会有所不同,但我找到了变异行为的来源:

由于主监视器的分辨率极高,它有一个额外的缩放参数 200%。所以 ISE 似乎检测到 'true' 分辨率为 2736x1824,其中 Powershell 检测到伪分辨率为 1368x912,但没有检测到它运行时用于填充显示器的 200% 缩放比例。

虽然我无法像我希望的那样正确检测或解释缩放比例,但我已经解决了 [system.Windows.Forms.Label] 的大小不一致问题,这是该行为的合适解决方法。一件令人惊讶的事情是 Label 字体大小是自动缩放的,所以当我从 $FontLabel 中删除手动缩放因子时,它会正确缩放,尽管它​​仍然需要 $FontText:

的缩放因子
$FontLabel = "Microsoft Sans Serif,$(11)"
$FontText  = "Microsoft Sans Serif,$(20*$w)"

因此 通过将我的缩放因子应用于除标签字体之外的所有内容,我能够使表单在两个应用程序中适当缩放。