如何使用Powershell在Windows窗体中使用GUI显示图片框?

问题描述 投票:0回答:1

我想显示图片框,但是一旦使用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:\3.png"
$cover = [Drawing.Image]::FromFile($file2)
$form.BackgroundImage = $img2

[reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
$file = (get-item 'D:\6.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:\6.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

任何人都可以给我个主意。非常感谢您的帮助。谢谢。

powershell picturebox showdialog
1个回答
0
投票
$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:\6.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()
© www.soinside.com 2019 - 2024. All rights reserved.