在所有 windows 之上显示自动关闭的弹出消息

Show popup message with auto close and on top of all of the windows

我需要创建一个向用户显示消息的功能,如果用户没有响应,它会自动关闭 windows。

   function ShowMsg ($timeout,$message)
{
Add-Type -AssemblyName system.windows.forms
Add-Type -AssemblyName system.drawing

$form = New-Object System.Windows.Forms.Form
$form.Text = "Company Name"
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'

$label = New-Object System.Windows.Forms.label
$label.Text = $message
$label.Size = New-Object System.Drawing.Size(200,40)
$form.Controls.Add($label)

#add button to form
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,120)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)

$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = $timeout * 1000
$timer.add_tick({$form.Close()})
$timer.Start()

$form.Topmost = $true
$form.ShowDialog()

$form.Dispose()
}

$timeout = 30

ShowMsg -message “This is the message you will see on the window`nMessage on new line” -timeout $timeout
$r = ShowMsg -message “This is the message you will see on the window`nMessage on new line” -timeout $timeout
if ($r -eq [System.Windows.Forms.DialogResult]::OK)
{
    Write-Host “Press ok now”
}else{
    Write-Host "$r"
}

我尝试使用上面的代码,但在显示消息 3 或 4 次后,它开始关闭弹出窗口,在输出中,我看到“取消”

enter image description here

问题出在计时器上。

在创建计时器对象的表单声明中,最初将其设置为 $timer.Enabled = $false

接下来,添加一个 $form.Add_Shown({$timer.Enabled = $true; $timer.Start()}) 事件处理程序以在表单首次显示时启动计时器。

在计时器的 Tick 事件中,告诉它 Stop() 并关闭窗体。 别忘了处理计时器:

尝试

function ShowMsg ([int]$timeout, [string]$message){
    Add-Type -AssemblyName system.windows.forms
    Add-Type -AssemblyName system.drawing

    $form = New-Object System.Windows.Forms.Form
    $form.Text = "Company Name"
    $form.Size = New-Object System.Drawing.Size(300,200)
    $form.StartPosition = 'CenterScreen'
    $form.Topmost = $true

    $label = New-Object System.Windows.Forms.label
    $label.Text = $message
    $label.Location = New-Object System.Drawing.Point(10,10)
    $label.Size = New-Object System.Drawing.Size(200,40)
    $form.Controls.Add($label)

    #add button to form
    $okButton = New-Object System.Windows.Forms.Button
    $okButton.Location = New-Object System.Drawing.Point(75,120)
    $okButton.Size = New-Object System.Drawing.Size(75,23)
    $okButton.Text = 'OK'
    $okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    $form.Controls.Add($okButton)
    $form.AcceptButton = $okButton

    $timer = New-Object System.Windows.Forms.Timer
    $timer.Enabled = $false           # disabled at first
    $timer.Interval = $timeout * 1000
    $timer.Add_Tick({ $timer.Stop(); $form.Close() })

    # start the timer as soon as the form is shown
    $form.Add_Shown({$timer.Enabled = $true; $timer.Start()})

    $form.ShowDialog()

    # clean-up
    $timer.Dispose()
    $form.Dispose()
}