如何使 GUI 的标签 PowerShell 在任何执行的显示器中具有相同的位置



我做了一个包含一些标签的GUI。一旦我在其他计算机或笔记本电脑中执行此代码,这些标签的位置就会更改。如何使位置可以相同,无论我执行代码的显示如何?

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form             = New-Object System.Windows.Forms.Form
$Form.ClientSize  = '578,400'
$Form.text        = "Form"
$Form.BackColor   = "#c1daf7"
$Form.TopMost     = $false
$Form.WindowState = 'Maximized'
$Label1           = New-Object System.Windows.Forms.Label
$Label1.text      = "UNDER PROCESS"
$Label1.AutoSize  = $true
$Label1.Location  = New-Object System.Drawing.Point(600, 300)
$Label1.Font      = 'Microsoft Sans Serif,30,style=Bold,Underline'
$Label1.ForeColor = "#d0021b"
$Label2           = New-Object System.Windows.Forms.Label
$Label2.text      = "WAITING"
$Label2.AutoSize  = $true
$Label2.Location  = New-Object System.Drawing.Point(770, 500)
$Label2.Font      = 'Microsoft Sans Serif,20,style=Bold'
$Label2.ForeColor = "#fb0505"
$Form.controls.AddRange(@($Label1, $Label2))
[void]$Form.ShowDialog()

更新

我用完整的代码更新了我的代码。我试过这个,但它返回我错误:

Exception calling "ShowDialog" with "0" argument(s): "Form that is already visible cannot be displayed as a modal dialog box. Set the form's visible property to false before calling showDialog."

 Param (   
        [string]$Path = '*.*',
        [string]$MaxAttempts = 5
    ) 
    Add-Type -AssemblyName System.Windows.Forms
    [System.Windows.Forms.Application]::EnableVisualStyles()
    # set things up for the timer
    $script:nAttempts = 0
    $timer = New-Object System.Windows.Forms.Timer
    $timer.Interval = 1000  # 1 second
    $timer.Add_Tick({
        $global:Result = $null
        $script:nAttempts++
        $file = Get-Item -Path $Path
        if ($file) {
            $global:Result = [PSCustomObject]@{
                Exists   = $true
                FileName = $file.FullName
                Attempts = $script:nAttempts
            }
            $timer.Dispose()
            $Form.Close()
        }
        elseif ($script:nAttempts -ge $MaxAttempts) {
            $global:Result = [PSCustomObject]@{
                Exists   = $false
                FileName = ''
                Attempts = $script:nAttempts
            }
            $timer.Dispose()
            $Form.Close()
        }
    })
    Add-Type -AssemblyName System.Windows.Forms
    [System.Windows.Forms.Application]::EnableVisualStyles()
    $Form                            = New-Object system.Windows.Forms.Form
    $Form.ClientSize                 = '617,418'
    $Form.text                       = "Auto"
    $Form.BackColor                  = "#8b572a"
    $Form.TopMost                    = $false
    $Form.WindowState                = 'Maximized'
    $Label1                          = New-Object system.Windows.Forms.Label
    $Label1.text                     = "UNDER AUTOMATION PROCESS"
    $Label1.AutoSize                 = $true
    $Label1.width                    = 25
    $Label1.height                   = 10
    $Label1.Anchor                   = 'top,right,bottom,left'
    $Label1.ForeColor                = "#ffffff"
    $Label1.Anchor                   = "None"
    $Label1.TextAlign                = "MiddleCenter"
    $Label2                          = New-Object system.Windows.Forms.Label
    $Label2.text                     = "Waiting for the job..."
    $Label2.AutoSize                 = $true
    $Label2.width                    = 25
    $Label2.height                   = 10
    $Label2.ForeColor                = "#ffffff"
    $Label2.Anchor                   = "None"
    $Label2.TextAlign                = "MiddleCenter"
    $Form.controls.AddRange(@($Label1,$Label2))
    [void]$Form.Show()
    Write-Host $Form.Height
    Write-Host $Form.Width
    $Label1.location = New-Object System.Drawing.Point(($Form.Width*0.35), ($Form.Height*0.4))
    $Label2.location = New-Object System.Drawing.Point(($form.Width*0.43), ($Form.Height*0.5))
    $L_S = (($Form.Width/2) - ($Form.Height / 2)) / 15
    $L_S
    $Label1.Font = "Microsoft Sans Serif, $L_S, style=Bold"
    $Label2.Font = "Microsoft Sans Serif, $L_S, style=Bold"
    $Form.controls.AddRange(@($Label1,$Label2))
    # start the timer as soon as the dialog is visible
    $Form.Add_Shown({ $timer.Start() })
    [void]$Form.ShowDialog()
    # clean up when done
    $Form.Dispose()

更新了我的代码,我试过这个,但它仍然给我一个错误。任何人都可以帮我修复它。谢谢

更新的 2nd

Param (   
    [string]$Path = '*.*',
    [string]$MaxAttempts = 5
) 
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
# set things up for the timer
$script:nAttempts = 0
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 1000  # 1 second
$timer.Add_Tick({
    $global:Result = $null
    $script:nAttempts++
    $file = Get-Item -Path $Path
    if ($file) {
        $global:Result = [PSCustomObject]@{
            Exists   = $true
            FileName = $file.FullName
            Attempts = $script:nAttempts
        }
        $timer.Dispose()
        $Form.Close()
    }
    elseif ($script:nAttempts -ge $MaxAttempts) {
        $global:Result = [PSCustomObject]@{
            Exists   = $false
            FileName = ''
            Attempts = $script:nAttempts
        }
        $timer.Dispose()
        $Form.Close()
    }
})
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form                            = New-Object system.Windows.Forms.Form
# $Form.ClientSize                 = '617,418'
$Form.text                       = "AutoGM"
$Form.BackColor                  = "#9b9b9b"
$Form.TopMost                    = $false
$Form.Width                      = [System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Width
$Form.Height                     = [System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Height
$FontSize                        = ($Form.Width / 100) + ($Form.Height/100) + 5
$Label1                          = New-Object system.Windows.Forms.Label
$Label1.text                     = "UNDER PROCESS"
$Label1.AutoSize                 = $true
# $Label1.width                    = 25
# $Label1.height                   = 10
$Label1.Anchor                   = "None"
$Label1.Location                 = New-Object System.Drawing.Point(($form.Width*0.3), ($Form.Height*0.3))
$Label1.Font                     = "Microsoft Sans Serif,$FontSize,style=Bold"
$Label1.ForeColor                = "#000000"
$Label2                          = New-Object system.Windows.Forms.Label
$Label2.text                     = "Waiting..."
$Label2.AutoSize                 = $true
# $Label2.width                    = 25
# $Label2.height                   = 10
$Label2.Location                 = New-Object System.Drawing.Point(($form.Width*0.4), ($Form.Height*0.4))
$Label2.Anchor                   = "None"
$Label2.Font                     = "Microsoft Sans Serif,$FontSize"
$Label2.ForeColor                = "#000000"
$img = [System.Drawing.Image]::Fromfile(".img.png")
$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Location = New-Object System.Drawing.Point(($form.Width*0.45), ($Form.Height*0.5))
$pictureBox.Width = $Form.Size.Width / 5
$pictureBox.Height = $Form.Size.Height / 5
$pictureBox.Image = $img
$form.controls.add($pictureBox)
$Form.controls.AddRange(@($Label1,$Label2))
# Write-Host $Form.Height
# Write-Host $Form.Width
# $Label1.location = New-Object System.Drawing.Point(($Form.Width*0.35), ($Form.Height*0.4))
# $Label2.location = New-Object System.Drawing.Point(($form.Width*0.43), ($Form.Height*0.5))
# $L_S = (($Form.Width/2) - ($Form.Height / 2)) / 15
# $Label1.Font = "Microsoft Sans Serif, $L_S, style=Bold"
# $Label2.Font = "Microsoft Sans Serif, $L_S, style=Bold"
# $Form.controls.AddRange(@($Label1,$Label2))
# start the timer as soon as the dialog is visible
$Form.Add_Shown({ $timer.Start() })
[void]$Form.ShowDialog()
# clean up when done
$Form.Dispose()

正如Niraj所说,这取决于所使用的显示器的分辨率,所以相反,问问自己你想让它显示在哪里? 在中中? 左中? 这可以通过一些数学来实现,请参阅下面的简单示例,它使用$form宽度 en 高度 计算标签的正确位置。

$Label1.Location  = New-Object System.Drawing.Point(($form.Width*0.5), ($Form.Height*0.5))
$Label2.Location  = New-Object System.Drawing.Point(($form.Width*0.5), ($Form.Height*0.4))

最新更新