简体   繁体   English

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

[英]How to make a label of GUI PowerShell has same location in any display executed?

I made a GUI contain some label. 我做了一个GUI包含一些标签。 Once I execute this code in other computer or notebook, the location of those label changed. 一旦在其他计算机或笔记本电脑上执行此代码,这些标签的位置就会更改。 How do I make the location can be same no matter the display that I execute the code? 无论执行代码的显示如何使位置都可以相同?

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

UPDATED 更新

I updated my code with a full code. 我用完整的代码更新了代码。 I tried this but it return me error: 我试过了,但返回错误:

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

I updated my code, I tried this, but it still return me an error. 我更新了代码,尝试了此操作,但仍然返回错误。 Anyone can help me to fix it please. 任何人都可以帮助我修复它。 Thanks 谢谢

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

As stated by Niraj it depends on the resolution of the monitor being used so instead ask yourself where do you want it to show? 正如Niraj所说,这取决于所用显示器的分辨率,因此请问自己想在哪里显示它? in mid-mid? 在中旬? mid-left? 左中? this can be achieved using some math, see below for a simple example, it uses the $form width en height to calculate the correct position for the labels. 这可以通过一些数学运算来实现,请参见下面的简单示例,它使用$ form width和height来计算标签的正确位置。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM