如何使用 Powershell 以 windows 形式显示带有 GUI 的图片框?

How to display picturebox with GUI in the windows form using Powershell?

我想显示图片框,但是我用form.show()后它不显示。但是,如果我更改为 Form.showdialog,图片框将显示,但是,在我关闭 GUI 之前,该过程无法继续。图片框显示了但是不动,跟图片一样卡住了。

Function Handling
{

    $Form.Close()
    $Form.Dispose()
    $Form = New-Object system.Windows.Forms.Form
    $Form.ControlBox = $true
    $Form.BackColor = "#d0021b"
    $Form.WindowState = "Maximized"
    $Form.TopMost = $false
    [void]$Form.Show()

  # Message Box
    [System.Windows.MessageBox]::Show("OK", "[Error]", "0", "Error")
    $ExitCode = "1"
    if($ExitCode -ne "107A")
    {

      $Form.Close()
      $Form.Dispose()
      Exit
    }
    else{

      $Form.Close()
      $Form.Dispose()
      Exit
    }
}

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$form.BackgroundImageLayout = 'Center'
$Form.WindowState = 'Maximized'
$Form.BackColor = "#ffffff"

$file2 = Get-ChildItem -Path "D:.png"
$cover = [Drawing.Image]::FromFile($file2)
$form.BackgroundImage = $img2

[reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
$file = (get-item 'D:.gif')
$img = [System.Drawing.Image]::Fromfile($file)

$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Image = $img
$pictureBox.SizeMode = "Autosize"
$pictureBox.Anchor = "Bottom, left"
$Form.controls.add($pictureBox)

[reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
$file3 = (get-item 'D:.gif')
$img3 = [System.Drawing.Image]::Fromfile($file3)

$pictureBox2 = new-object Windows.Forms.PictureBox
$pictureBox2.Image = $img3
$pictureBox2.SizeMode = "Autosize"
$pictureBox2.Anchor = "Bottom, right"
$Form.controls.add($pictureBox2)

$form.Show()

Write-Host "next process"
####
# some process
###

Start-Sleep -s 2
Handling

任何人都可以给我想法。非常感谢您的帮助。谢谢。

你能这样试试吗?把你的后台操作放在 Start-Sleep -s 2 的位置,gif 文件仍然显示移动。

$Form = New-Object system.Windows.Forms.Form
$Form.Location= New-Object System.Drawing.Size(100,100)
$Form.Size= New-Object System.Drawing.Size(550,170)
$Form.StartPosition = "Manual"
$Form.Visible=$false
$Form.Enabled = $true
$Form.Add_Shown({$Form.Activate()})

[reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
$file = (get-item 'D:.gif')
$img = [System.Drawing.Image]::Fromfile($file);

[System.Windows.Forms.Application]::EnableVisualStyles();

$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Location = New-Object System.Drawing.Size(0,1)
$pictureBox.Size = New-Object System.Drawing.Size($img.Width,$img.Height)
$pictureBox.Image = $img
$Form.controls.add($pictureBox)

$WaitForm.Topmost = $True

$rs = [Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace()
$rs.Open()
$rs.SessionStateProxy.SetVariable("Form", $Form)
$data = [hashtable]::Synchronized(@{text=""})
$rs.SessionStateProxy.SetVariable("data", $data)
$p = $rs.CreatePipeline({ [void] $Form.ShowDialog()})
$p.Input.Close()
$p.InvokeAsync()

## Enter the rest of your script here while you want the form to display
Start-Sleep -s 2

$WaitForm.close()