如何在脚本中同时运行winforms窗口窗口

时间:2013-11-19 22:42:18

标签: winforms powershell

我正在尝试与我的主脚本一起运行System.windows.form对象。 例如,我想运行一个PowerShell脚本,它有一个循环而且永远不会停止。我希望能够使用WPF表单对象上的按钮终止执行。

我已尝试过form.show()方法,但它并没有让我控制。

clear host
Function hey(){
    # code to exit, or do what I want it to do.
}
Add-Type -AssemblyName System.Windows.Forms

$form = New-Object Windows.Forms.Form
$form.Size = New-Object Drawing.Size @(1024,768)
$form.StartPosition = "CenterScreen"
$btn = New-Object System.Windows.Forms.Button 
$btn.Location = New-Object System.Drawing.Point(15, 15)
$btn.add_click({$(hey)})
$btn.Text = "Click here"
$form.Controls.Add($btn)


while($true){
    write-host "int = $int."
    start-sleep 3
    $int = $int + 1
    $drc = $form.ShowDialog()
}

非常感谢你!

1 个答案:

答案 0 :(得分:0)

如果你只想让“点击这里”按钮关闭表格并退出while循环,你可以这样做:

Function hey(){
    $form.Close()
}

Add-Type -AssemblyName System.Windows.Forms

$form = New-Object Windows.Forms.Form
$form.Size = New-Object Drawing.Size @(1024,768)
$form.StartPosition = "CenterScreen"
$btn = New-Object System.Windows.Forms.Button 
$btn.Location = New-Object System.Drawing.Point(15, 15)
$btn.add_click({$(hey)})
$btn.Text = "Click here"
$form.Controls.Add($btn)

while ($true) {
    write-host "int = $int."
    start-sleep 3
    $int = $int + 1
    $drc = $form.ShowDialog()
    if ($drc = "Cancel") { break }
}