0

我目前正在努力尝试使用“Register-objectevent”Cmdlet 更新 Winforms UI。

我想要做的是让 Register-ObjectEvent 更新表单中的标签,以便在计时器上打勾。

我已经对此进行了数小时的研究,并且我知道这与多线程/调用有关,但是我无法弄清楚如何使其工作!

如果有人可以向我展示/帮助我获取此脚本以通过计时器更新表单上的标签,那就太棒了!我有很多可以从多线程中受益的 Winform,但我需要先了解它!

这是我正在尝试使用的脚本,非常感谢您的帮助:)

[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[System.Windows.Forms.Application]::EnableVisualStyles() | out-null
$form1 = New-Object System.Windows.Forms.Form


$OnLoadForm_StateCorrection=
{
$form1.WindowState = $InitialFormWindowState
}
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 600
$System_Drawing_Size.Width = 1200
$form1.ClientSize = $System_Drawing_Size
$form1.MaximizeBox = $False
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.KeyPreview = $True
$form1.FormBorderStyle = 1
$form1.Name = "form1"
$form1.StartPosition = 1
$form1.backcolor = [System.Drawing.Color]::FromArgb(255,240,240,240)



$timer = New-Object System.Timers.Timer
$timer.Interval = 1000 
$timer.AutoReset = $true  
$timeout = 0




$num=0
$action = { 
$num++
    write-host "test"
    $vollabel.text=$num
    $timer.stop()
}  
Register-ObjectEvent -InputObject $timer -SourceIdentifier TimerElapsed -EventName          Elapsed -Action $action
$timer.start()


$vollabel = New-Object System.Windows.Forms.Label
$vollabel.Location = "0,0"
$form1.Controls.Add($vollabel)




$InitialFormWindowState = $form1.WindowState
$form1.add_Load($OnLoadForm_StateCorrection)
$form1.Add_Shown({$form1.Activate()})
$form1.ShowDialog()| Out-Null
4

1 回答 1

0

我对您的脚本进行了一些简化以进行概念验证-添加您需要的任何内容:

@('System.Drawing','System.Windows.Forms') | %{ [reflection.assembly]::LoadWithPartialName($_) | Out-Null }
[System.Windows.Forms.Application]::EnableVisualStyles() | out-null
$form1 = New-Object System.Windows.Forms.Form -Property @{
    MaximizeBox = $False
    KeyPreview = $True
    FormBorderStyle = 1
    Name = "form1"
    StartPosition = 1
    backcolor = [System.Drawing.Color]::FromArgb(255,240,240,240)
    ClientSize = New-Object System.Drawing.Size -Property @{Height = 600;Width = 1200}
}
$form1.DataBindings.DefaultDataSourceUpdateMode = 0 

$timer = New-Object System.Windows.Forms.Timer -Property @{Interval = 1000} #Forms.Timer doesn't support AutoReset property
$script:num=0 #scope must be at script level to increment in event handler 
$timer.start()
$timer.add_Tick({
    $script:num +=1
    write-host "test $script:num"
    $vollabel.text=$script:num
})

$vollabel = New-Object System.Windows.Forms.Label -Property @{Location = "0,0"}
$form1.Controls.Add($vollabel)
$form1.ShowDialog()| Out-Null
$timer.stop() #This will keep running in the background unless you stop it

几点注意事项:

  • Form.ShowDialog() 被阻塞并停止脚本执行。
  • 系统。Windows.Forms .Timer 的属性与 System.Timers.Timer 略有不同,可以将 ScriptBlock 或函数名称作为参数添加到 add_Tick()
  • 事件处理程序 ScriptBlock 有自己的范围,但您可以使用 $ScopeName:VariableName 语法共享变量。除非我将范围设置为 $Script,否则我无法增加 $num
于 2018-09-27T22:08:08.300 回答