在PowerShell中,如何调整表单以适应屏幕分辨率?

In PowerShell, how to adjust forms to the screen resolution?

在一些编码和测试过程中,我意识到我与表单相关的代码突然搞砸了,使用了不同分辨率的不同计算机。

为了解决这个问题,我的想法是,采用 1920 x 1080 的常见屏幕分辨率大小,并开始与打开代码的实际屏幕大小进行比较。 (我已经对主屏幕很满意了,不需要所有屏幕)。

现在,或多或少比较容易将常用屏幕尺寸与计算机上使用的尺寸进行比较,您在 运行 代码中。

试图计算分辨率并根据它调整框架,我无法使用圆形或 [int],将它变成一个衬里。

当您查看以下代码时:

CLS

Add-Type -assembly System.Windows.Forms

$monitor = [System.Windows.Forms.Screen]::PrimaryScreen

[void]::$monitor.WorkingArea.Width
[void]::$monitor.WorkingArea.Height

$ResolutionStartPointWidth = '1920'
$ResolutionStartPointHeight = '1080'

#Calculating the difference of Width and Height towards actual screen resolution
$ResolutiondifferenceWidth = 
($monitor.WorkingArea.Width/$ResolutionStartPointWidth)
$ResolutiondifferenceHeight = 
($monitor.WorkingArea.Height/$ResolutionStartPointHeight)



# MainForm

$MainForm = New-Object System.Windows.Forms.Form
$MainForm.Text ='Adjusting Form Size'
$MainForm.Size = '1200,800'
$MainForm.StartPosition = "CenterScreen"
$MainForm.AutoSize = $true
$MainForm.BringToFront()
$MainForm.BackgroundImageLayout = "Stretch"

$Mainform.ShowDialog()

我尝试在以下行中调整屏幕大小:

$MainForm.Size = '1200,800'

并试过例如:

$MainForm.Size = [int](1200*$ResolutiondifferenceWidth,800)

我也尝试了其他结构,但随着我尝试的越来越多,一切都变得越来越荒谬。

目标是,计算“1200*$ResolutiondifferenceWidth”,然后四舍五入或代入一个不带小数的数字,结果作为宽度给$MainForm.Size.

当涉及到 "Form" 位置和按钮大小甚至更多时,结果也会有所帮助。

感谢您的任何建议,

麦克

一方面,您通过引用将 $ResolutionStartPointWidth$ResolutionStartPointHeight 定义为字符串。 另外,我相信你甚至不需要这些变量。

尝试:

Add-Type -assembly System.Windows.Forms

$monitor = [System.Windows.Forms.Screen]::PrimaryScreen

# Calculating the factors to multiply the Width and Height
$widthFactor  = 1200 / $monitor.WorkingArea.Width
$heightFactor = 800  / $monitor.WorkingArea.Height

# MainForm

$MainForm = New-Object System.Windows.Forms.Form
$MainForm.Text ='Adjusting Form Size'

# using System.Drawing.Size will automatically round the width and height to integer numbers like with:
# [Math]::Round(1920 * $widthFactor)
# [Math]::Round(1080 * $heightFactor)
$MainForm.Size = New-Object System.Drawing.Size (1920 * $widthFactor), (1080 * $heightFactor)

$MainForm.StartPosition = "CenterScreen"
$MainForm.AutoSize = $true
$MainForm.BringToFront()
$MainForm.BackgroundImageLayout = "Stretch"

$Mainform.ShowDialog()

# always clean up when done with the form
$MainForm.Dispose()