powershell - 如何使用 PowerShell 在 GUI 表单中处理标签大小和位置?
问题描述
我创建了一个简单的 GUI。我想在任何分辨率屏幕上执行 GUI。在我的工作屏幕上,它显示图1。一旦我在其他屏幕上执行它,尺寸标签,位置不同,如图2所示。任何人都可以帮助我请如何处理?太感谢了。
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '400,400'
$Form.text = "Form"
$Form.TopMost = $false
$form.AutoScale = $true
$form.AutoScaleMode = "Font"
$Form.FormBorderStyle = "FixedDialog"
$Groupbox1 = New-Object system.Windows.Forms.Groupbox
$Groupbox1.height = 362
$Groupbox1.width = 367
$Groupbox1.text = "Group Box"
$Groupbox1.location = New-Object System.Drawing.Point(15,21)
$Label1 = New-Object system.Windows.Forms.Label
$Label1.text = "Do you need handling the job?"
$Label1.AutoSize = $true
$Label1.width = 25
$Label1.height = 10
$Label1.location = New-Object System.Drawing.Point(92,82)
$Label1.Font = 'Microsoft Sans Serif,10'
$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = "Yes"
$Button1.width = 60
$Button1.height = 30
$Button1.location = New-Object System.Drawing.Point(34,263)
$Button1.Font = 'Microsoft Sans Serif,10'
$Button2 = New-Object system.Windows.Forms.Button
$Button2.text = "No"
$Button2.width = 60
$Button2.height = 30
$Button2.location = New-Object System.Drawing.Point(268,263)
$Button2.Font = 'Microsoft Sans Serif,10'
$Form.controls.AddRange(@($Groupbox1))
$Groupbox1.controls.AddRange(@($Label1,$Button1,$Button2))
[void]$Form.ShowDialog()
更新
我试试这个
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.FormBorderStyle = "FixedDialog"
$Width = [System.Windows.Forms.Screen]::AllScreens.bounds.width
$Heigt = [System.Windows.Forms.Screen]::AllScreens.bounds.height
$Widht_Form = $Width[0] / 3.5
Write-Host "$Widht_Form"
$Heigt_Form = $Heigt[0] / 1.8
Write-Host "$Heigt_Form"
$Form.Width = $Widht_Form
$Form.Height = $Heigt_Form
$label1 = New-Object 'System.Windows.Forms.Label'
$Yes = New-Object 'System.Windows.Forms.Button'
$No = New-Object 'System.Windows.Forms.Button'
$Title = New-Object 'System.Windows.Forms.Label'
$timer1 = New-Object 'System.Windows.Forms.Timer'
$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
$Form_Load = {
$TotalTime = 10 #in seconds
$script:StartTime = (Get-Date).AddSeconds($TotalTime)
#Start the timer
$timer1.Start()
}
$Cancel_Click={
}
$timer1_Tick={
#Use Get-Date for Time Accuracy
[TimeSpan]$span = $script:StartTime - (Get-Date)
#Update the display
$Form.Text = $label1.Text = "{0:N0}" -f $span.TotalSeconds
if ($span.TotalSeconds -le 0) {
$timer1.Stop()
$Form.Close()
}
}
$Form_StateCorrection_Load=
{
$Form.WindowState = $InitialFormWindowState
}
$Form_Cleanup_FormClosed=
{
try
{
# $Cancel.remove_Click($Cancel_Click)
$Form.remove_Load($Form_Load)
$timer1.remove_Tick($timer1_Tick)
$Form.remove_Load($Form_StateCorrection_Load)
$Form.remove_FormClosed($Form_Cleanup_FormClosed)
}
catch [Exception]
{ }
}
$Form.SuspendLayout()
$Form.Controls.Add($label1)
$Form.Controls.Add($Yes)
$Form.Controls.Add($No)
$Form.Controls.Add($Title)
$Form.StartPosition = "CenterScreen"
$Form.BackColor = "#f6f6f6"
$Form.add_Load($Form_Load)
$label1.Font = 'Microsoft Sans Serif,20,style=Bold'
$Label1.ForeColor = "#176faa"
$label1.AutoSize = $true
$label1.width = 25
$label1.height = 10
$label1_height = $Heigt_Form / 2.5
$label1_width = $Widht_Form / 2.2
$label1.location = New-Object System.Drawing.Point($label1_width,$label1_height)
$Title.Text = "Do you need handling the job?"
$Title.ForeColor = "#176faa"
$Title.Font = 'Microsoft Sans Serif,16,style=Bold'
$Title.AutoSize = $true
$Title.width = 25
$Title.height = 10
$Title_height = $Heigt_Form / 5
$Title_width = $Widht_Form / 5
$Title.location = New-Object System.Drawing.Point($Title_width,$Title_height)
$Yes.AutoSize = $true
$Yes_height = $Heigt_Form * 0.7
$Yes_width = $Widht_Form / 8
$Yes.Location = New-Object System.Drawing.Size($Yes_width,$Yes_height)
$Yes.Size = New-Object System.Drawing.Size(90,35)
$Yes.Text = "Yes"
$Yes.Add_Click(
{
Write-Host "Call GUI Control"
Start-Sleep -s 1
$Form.Close()
}
)
$No.AutoSize = $true
$No_height = $Heigt_Form * 0.7
$No_width = ($Yes_width * 6) - 35
$No.Location = New-Object System.Drawing.Size($No_width,$No_height)
$No.Size = New-Object System.Drawing.Size(90,35)
# $No.BackColor = "#9fd5f3"
$No.Text = "No"
$No.Add_Click(
{
Write-Host "Continue the process"
$Form.Close()
}
)
$timer1.add_Tick($timer1_Tick)
$Form.ResumeLayout()
#Save the initial state of the form
$InitialFormWindowState = $Form.WindowState
#Init the OnLoad event to correct the initial state of the form
$Form.add_Load($Form_StateCorrection_Load)
#Clean up the control events
$Form.add_FormClosed($Form_Cleanup_FormClosed)
#Show the Form
return $Form.ShowDialog()
一旦在不同的屏幕中执行,这种方式也可以将标签和按钮网格化
解决方案
您可以使用 WMI 对象来调整此表单中的对象。WMI 对象返回有关系统的键值信息。
这是您可以创建其对象取决于屏幕大小的 GUI 的方法:
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$sw = (Get-WmiObject -Class Win32_DesktopMonitor).ScreenWidth[1]
$sh = (Get-WmiObject -Class Win32_DesktopMonitor).ScreenHeight[1]
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = "$($sw / 2),$($sh / 2)"
$Form.text = "Form"
$Form.TopMost = $false
$form.AutoScale = $true
$form.AutoScaleMode = "Font"
$Form.FormBorderStyle = "FixedDialog"
$Groupbox1 = New-Object system.Windows.Forms.Groupbox
$Groupbox1.height = $($sw / 2)
$Groupbox1.width = $($sh / 2)
$Groupbox1.text = "Group Box"
$Groupbox1.location = New-Object System.Drawing.Point(15,21)
$Label1 = New-Object system.Windows.Forms.Label
$Label1.text = "Do you need handling the job?"
$Label1.AutoSize = $true
$Label1.width = 25
$Label1.height = 10
$Label1.location = New-Object System.Drawing.Point($($sw / 16),$($sh / 12))
$Label1.Font = 'Microsoft Sans Serif,10'
$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = "Yes"
$Button1.width = 60
$Button1.height = 30
$Button1.location = New-Object System.Drawing.Point($($sw / 20),$($sh / 3))
$Button1.Font = 'Microsoft Sans Serif,10'
$Button2 = New-Object system.Windows.Forms.Button
$Button2.text = "No"
$Button2.width = 60
$Button2.height = 30
$Button2.location = New-Object System.Drawing.Point($($sw / 6),$($sh / 3))
$Button2.Font = 'Microsoft Sans Serif,10'
$Form.controls.AddRange(@($Groupbox1))
$Groupbox1.controls.AddRange(@($Label1,$Button1,$Button2))
[void]$Form.ShowDialog()
推荐阅读
- git - 严格的 Gitolite 配置
- objective-c - 指针在自动生成的 ModuleName-Swift.h 文件中缺少可空性类型说明符
- android - 索引0,选择时大小0
- php - 在 MySQL 结果中创建一个可点击的链接
- powershell - 在PowerShell中读取顺序数字注册表项
- ruby-on-rails - 使用 Postgres 按年份分组
- java - 将 byte[] 数组转换为 Unicode、UnicodeWithBOM 等
- xslt - XSLT:我们如何在“选择、何时、测试”语法中选择同一个测试中的多个条件?
- active-directory - ADAL v3 - 如何正确摆脱刷新令牌代码?
- python - raw_input 在要求输入后打印提示