这个函数和函数调用哪里错了?怎么调用函数后生成不了控件?


PowerShell交流中心分类: Questions这个函数和函数调用哪里错了?怎么调用函数后生成不了控件?
KIVIN 管理员 提问于 4周 前

[System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)|out-null
[System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”)|out-null
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = “Test”
$objForm.Size = New-Object System.Drawing.Size(1000,700)
$objForm.StartPosition = “CenterScreen”
$objForm.MaximizeBox = $false
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq “Escape”) {$objForm.Close()}})

Function CreateCheckBox($CheckBoxName,$LocationX, $LocationY,$SizeX,$SizeY,$Text,$color = “Blue”)
{

$CheckBoxName = New-Object System.Windows.Forms.CheckBox
$CheckBoxName.Location = New-Object System.Drawing.Size($LocationX,$LocationX)
$CheckBoxName.Size = New-Object System.Drawing.Size($SizeX,$SizeY)
$CheckBoxName.Text = $Text
$CheckBoxName.ForeColor = $color
$objForm.Controls.Add($CheckBoxName)
return $CheckBoxName

}

&CreateCheckBox($Test,420,30,500,20,”Test”,’Blue’)

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

1 次回答
最佳答案
KIVIN 管理员 回答于 4周 前

还是自己搞定了.
CLS

[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")|out-null
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")|out-null
$Global:objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Test"
$objForm.Size = New-Object System.Drawing.Size(1000,700)
$objForm.StartPosition = "CenterScreen"
$objForm.MaximizeBox = $false
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") {$objForm.Close()}})
function Global:CreateNewCheckBox($Name,$LocationX, $LocationY,$SizeX,$SizeY,$Text,$color)
{
$ExpName = ('$Global:'+$Name + ' = New-Object System.Windows.Forms.CheckBox')
$ExpLocation = '$'+ $Name + '.Location = New-Object System.Drawing.Size('+$LocationX+','+$LocationY+')'
$ExpSize = '$'+ $Name + '.Size = New-Object System.Drawing.Size('+$SizeX+','+$SizeY+')'
$ExpText = '$'+ $Name + '.Text = '+ '$Text'
$ExpColor = '$'+ $Name + '.ForeColor = ' +'$color'
$Load = '$objForm.Controls.Add('+'$'+$Name+')'
$return = 'return ' + '$'+ $Name

write-host $ExpName
Write-Host $ExpLocation
Write-Host $ExpSize
Write-Host $ExpText
Write-Host $ExpColor
Write-Host $Load
# Write-Host $return

Invoke-Expression $ExpName
Invoke-Expression $ExpLocation
Invoke-Expression $ExpSize
Invoke-Expression $ExpText
Invoke-Expression $ExpColor
Invoke-Expression $Load
#Invoke-Expression $return

}

CreateNewCheckBox TestX 420 20 500 20 "Tester..X." Red
CreateNewCheckBox TestY 420 40 500 20 "Tester2..Y." Blue
CreateNewCheckBox Testz 420 60 500 20 "Tester2..Z." Green

Write-Host $TestX.Text
$Testz.ForeColor = 'Gray'

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