如何使用 PowerShell 创建适合任何屏幕分辨率的 GUI? [英] How to create GUI can fit in any screen resolution using PowerShell?

查看:91
本文介绍了如何使用 PowerShell 创建适合任何屏幕分辨率的 GUI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能帮我修复这个GUI?我创建了一个 GUI,然后我需要 GUI 可以适合任何屏幕尺寸而无需任何尺寸更改.这个脚本,当我在不同分辨率的其他电脑上执行时,大小发生了变化.

anyone can help me how to fix this GUI? I created a GUI, then I need the GUI can fit to any screen size without any size change. This script, when I execute in other computer with different resolution, the size changed.

请帮帮我.谢谢

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()

我已经尝试了一些像 autosize 这样的功能,但它仍然是同样的问题.

I already try some function like autosize, but it's still the same problem.

推荐答案

看自动缩放而不是自动缩放.

Look at Autoscale instead of Autosize.

$form.AutoScale = $true

我通常使用字体模式,选择一种系统字体

I usually use the font mode and choose one of the system fonts

$form.AutoScaleMode = "Font"

您必须记住,表观 大小将根据您使用的显示器/分辨率而变化,除非您检测到当前分辨率并将其用于表单的百分比.

You have to remember that the apparent size WILL change according to the monitor/resolution you use unelss you detect the current resolution and use a percentage of that for your form.

最简单的方法是控制像素大小和缩放比例.

The easiest way is to just control the pixel size and scaling.

这篇关于如何使用 PowerShell 创建适合任何屏幕分辨率的 GUI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆