繁体   English   中英

powershell表单图片框功能不起作用

[英]powershell form picture box function not working

我有以下代码,我可以在函数之外运行,它工作得很好。 我正在尝试制作一个将显示多个图像的表单,因此我需要将其格式化为一个函数。

Add-Type -AssemblyName System.Windows.Forms, System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()

$Form                                 = New-Object system.Windows.Forms.Form
$Form.StartPosition                   = "CenterScreen"
$Form.Size                            = New-Object System.Drawing.Point(800,800)
$Form.text                            = "Example Form"
$Form.TopMost                         = $false
$Form.MaximumSize                     = $Form.Size
$Form.MinimumSize                     = $Form.Size

$Picture = (get-item ("C:\Users\User\Desktop\t.png"))
$img = [System.Drawing.Image]::Fromfile($Picture)
$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Width =  $img.Size.Width
$pictureBox.Height =  $img.Size.Height
$pictureBox.Image = $img
$pictureBox.Location  = [Drawing.Point]::new(15, 15)
$Form.controls.add($pictureBox)


$Form.ShowDialog()

但是当我试图把它组装成一个函数时它不起作用

Add-Type -AssemblyName System.Windows.Forms, System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()

$Form                                 = New-Object system.Windows.Forms.Form
$Form.StartPosition                   = "CenterScreen"
$Form.Size                            = New-Object System.Drawing.Point(800,800)
$Form.text                            = "Example Form"
$Form.TopMost                         = $false
$Form.MaximumSize                     = $Form.Size
$Form.MinimumSize                     = $Form.Size

function pictureBox {
    [CmdletBinding()]
    param (
        [Parameter (Mandatory = $True)]
        [string] $p,

        [Parameter (Mandatory = $True)]
        [int] $lx,

        [Parameter (Mandatory = $True)]
        [int] $ly

    )
        $Picture              = (get-item ($p))
        $img                  = [System.Drawing.Image]::Fromfile($Picture)
        $pictureBox           = new-object Windows.Forms.PictureBox
        $pictureBox.Size      = [Drawing.Size]::new($img.Size.Width,$img.Size.Height)
        $pictureBox.Location  = [Drawing.Point]::new($lx, $ly)
        $pictureBox.Image     = $img
        
    }

$v1 = pictureBox -p "C:\Users\User\Desktop\t.png" -lx 15 -ly 15

$Form.controls.add($v1)

$Form.ShowDialog()

我究竟做错了什么?

您的函数pictureBox没有返回任何内容,它创建了一个新的PictureBox instance ,更新了它的属性,但是该对象永远不会输出,因此$v1被分配了AutomationNull.Value

# Same code for creating the Form here

function pictureBox {
    [CmdletBinding()]
    param (
        [Parameter (Mandatory = $True)]
        [string] $p,

        [Parameter (Mandatory = $True)]
        [int] $lx,

        [Parameter (Mandatory = $True)]
        [int] $ly
    )

    $img = [Drawing.Image]::Fromfile($PSCmdlet.GetUnresolvedProviderPathFromPSPath($p))
    [Windows.Forms.PictureBox]@{
        Size      = [Drawing.Size]::new($img.Size.Width, $img.Size.Height)
        Location  = [Drawing.Point]::new($lx, $ly)
        Image     = $img
    }
}

$v1 = pictureBox -p "C:\Users\User\Desktop\t.png" -lx 15 -ly 15
$Form.controls.add($v1)
$Form.ShowDialog()

暂无
暂无

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

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