【问题标题】:Monitor Size Discrepancy causing Form Issues: Powershell vs ISE导致表单问题的监视器大小差异:Powershell 与 ISE
【发布时间】: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


【解决方案1】:

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

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

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

$FontLabel = "Microsoft Sans Serif,$(11)"
$FontText  = "Microsoft Sans Serif,$(20*$w)"

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 2021-06-27
    相关资源
    最近更新 更多