0

Windowsフォームダイアログの検索ボックスから別のスクリプトにパラメーターを渡そうとしていますが、ユーザーがファイルを選択したときにパラメーターを引き出すことができないようです。パラメータは、ユーザーがインストールしたいフォントの完全なファイルの場所である必要があります。以下のスクリプトは形式であり、パラメータが通過するために必要なスクリプトは下部にあります。

cls
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")    

#Windows form settings
$objForm = New-Object System.Windows.Forms.Form 
$objForm.Icon = $Icon
$objForm.Text = "Font Installer"
$objForm.Size = New-Object System.Drawing.Size(350,350) 
$objForm.StartPosition = "CenterScreen"
$objForm.FormBorderStyle = "FixedDialog"
$objForm.BackgroundImage = $Image
$objForm.BackgroundImageLayout = "None"

#Browse for file
$d = New-Object Windows.Forms.OpenFileDialog
$d.initialDirectory = $initialDirectory
$d.filter = "All files (*.*)| *.*"
$d.ShowHelp = $true
$d.InitialDirectory = "c:\"
$d.Title = "Choose your Font"
$d.FileName
$d.filter = " Font Files (*.ttf; *.fon; *.fnt; *.ttc; *.otf; *.mmm; *.pbf; *.pfm)| *.ttf;     *.fon; *.fnt; *.ttc; *.otf; *.mmm; *.pbf; *.pfm"
#Browse Button
$button1 = New-Object system.Windows.Forms.Button
$button1.Text = "Select Font"
$button1.Add_Click({$d.ShowDialog( )})
$button1.Location = New-Object System.Drawing.Size(100,120)
$button1.Size = New-Object System.Drawing.Size(150,23)
$objForm.controls.add($button1)

#Install Button
$run = New-Object System.Windows.Forms.Button
$run.Location = New-Object System.Drawing.Size(100,170)
$run.Size = New-Object System.Drawing.Size(150,100)
$run.Text = "Install"
$Font1 = New-Object System.Drawing.Font("Arial Black",19,    [System.Drawing.FontStyle]::regular)
$run.Font = $Font1
$run.BackColor ="green"
#invoke expression - open install script and sent the parameter to it
$run.Add_Click({ 
Invoke-Expression "& `"c:\Users\Khussain\Desktop\Fonts\scripts\testparam.ps1`"        $d.filename";



})
$objForm.Controls.Add($run)



$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

パラメータを取得する必要があるスクリプトの名前はtestparam.ps1で、コードは次のとおりです。

param(
    [string] $path = ""
)
Write-Host "this is a test the parameter is $path"
4

2 に答える 2

1

これだけ試してみてください:

$run.Add_Click({ c:\Users\Khussain\Desktop\Fonts\scripts\testparam.ps1 $($d.filename);})
于 2013-01-28T13:55:03.810 に答える
1

追加ハンドラーを次のように変更してみてください。

$run.Add_Click({ 
    if($d.filename)
    {
        $path = $d.filename
        c:\Users\Khussain\Desktop\Fonts\scripts\testparam.ps1 "'$path'"
    }
})
于 2013-01-28T13:58:58.693 に答える