繁体   English   中英

未按下时的消息框和操作

[英]Messagebox and action if not pressed

我正在尝试开发一个脚本来向在一天中的特定时间连接计算机的用户弹出一个消息框。 如果用户通过按下消息框的按钮确认他正在使用计算机,则该过程停止。 如果用户在 30 米内没有按任何东西,则表示他不在计算机旁,我们将关闭计算机。

我可以弹出带有消息的消息框,但是如何检查按钮是否已按下或等待 30 m,然后如果没有任何反应,请执行其他操作?

您可以使用Wscript.Shell COM 对象的.Popup()方法,该方法支持指定超时时间,对话框(消息框)自动关闭。

最简单的情况下

# Show a message box with an OK button that auto-closes
# after 3 seconds.
# Return value is:
#  -1, if the dialog auto-closes
#  1 if the user pressed OK
$response = 
  (New-Object -ComObject WScript.Shell).Popup(
    'I will close in 3 seconds',
     3, # timeout in seconds
    'Title',
    48 # exclamation-point icon; OK button by default
  )
if ($response -eq -1) { 'Auto-closed.' } else { 'User pressed OK.' }

显示带有 OK 和 Cancel 按钮的对话框的完整示例:如果对话框自动关闭或用户按下 OK,则脚本继续; 否则,处理中止:

# How long to display the dialog before it closes automatically.
$timeoutSecs = 30 * 60 # 30 minutes.
# Translate this into the time of day, to show to the user.
$timeoutTimeOfDay = (Get-Date).AddSeconds($timeoutSecs).ToString('T')

# Put up a message box with a timeout and OK / Cancel buttons.
$response = 
  (New-Object -ComObject WScript.Shell).Popup(
    @"
Your computer will shut down at $timeoutTimeOfDay.

Press OK to shut down now, or Cancel to cancel the shutdown.
"@, 
    $timeoutSecs, 
    'Pending shutdown', 
    49  # 48 + 1 == exclamation-point icon + OK / Cancel buttons
  )

if ($response -eq 2) { # Cancel pressed.
  Write-Warning 'Shutdown aborted by user request.'
  exit 2
}

# OK pressed ($response -eq 1) or timed out ($response -eq -1), 
# proceed with shutdown.
'Shutting down...' 
# Restart-Computer -Force

笔记:

  • 上面的对话框仅静态显示一天中启动关机的时间。

  • 如果您想要剩余秒数的实时倒计时,则需要做更多的工作:请参阅Fitzgery 的有用答案

这是我在评论中提到的 Function

Function Invoke-RestartTimer {
    <#
    .SYNOPSIS
    Restart Computer After Timer reaches 0
    
    .DESCRIPTION
    Pops-Up a GUI Window that Counts Down Seconds until the Computer restarts. It also has buttons to either Restart Now or Cancel the Restart.
    If the Restart is Cancelled it will write to the host with Write-Warning that the Restart was cancelled by the Current User.
    
    .PARAMETER Seconds
    Identifies the Amount of Seconds the Timer will countdown from
    
    .EXAMPLE
    Restart-Computer -Seconds 30
    
    .NOTES
    Multi-use Advanced Function for performing a Visual Countdown Restart.
    #>
    
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$false)]
        [Int32]$Seconds = "15"
        )
    
        #Builds Assemblies for Custom Forms used in the function
        Add-Type -AssemblyName System.Windows.Forms
        Add-Type -AssemblyName System.Drawing
    
        #Identifies logged on user for if restart is cancelled
        $CurrentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
    
        #Adds 1 to the identified time to ensure restart happens at 0 seconds and not -1 seconds.
        #$Seconds++
    
        #Builds Form
        $RestartForm                    = New-Object System.Windows.Forms.Form
        $RestartForm.StartPosition      = "CenterScreen"
        $RestartForm.Size               = New-Object System.Drawing.Size(300,150)
        $RestartForm.Text               = "Restart Window"
        $RestartForm.AllowTransparency  = $true
        $RestartForm.BackColor          = "LightGray"
        $RestartForm.FormBorderStyle    = "Fixed3D"
        
        #Builds Text Label
        $Script:TimerLabel = New-Object System.Windows.Forms.Label
        $TimerLabel.AutoSize            = $true
        $TimerLabel.Font                = "Microsoft Lucida Console,10"
        $RestartForm.Controls.Add($TimerLabel)
        
        #Builds Timer
        $Timer = New-Object System.Windows.Forms.Timer
        $Timer.Interval = 1000 #1 second countdown, in milliseconds
        $script:CountDown = $Seconds #seconds to countdown from
        $Timer.add_Tick({
        
$Script:TimerLabel.Text = "
The Computer will restart in $CountDown seconds.

Press the Restart Button to Restart Now."
        $Script:CountDown--

        If($CountDown -eq "-2"){#Needs to be 2 below wanted value. i.e 0 = -2
            $Timer.Stop()
            $RestartForm.Close()
            Restart-Computer -Force
            }
        })
        $Timer.Start()
        
        #Builds a Restart Button
        $RestartButton                  = New-Object System.Windows.Forms.Button
        $RestartButton.Text             = "Restart"
        $RestartButton.FlatStyle        = "Popup"
        $RestartButton.Location         = New-Object System.Drawing.Size(50,80)
        $RestartButton.Size             = New-Object System.Drawing.Size(80,23)
        $RestartButton.Font             = "Microsoft Lucida Console,10"
        $RestartButton.Add_Click({
        
            $Timer.Stop()
            $RestartForm.Close()
            Restart-Computer -Force
        
        })
        $RestartForm.Controls.Add($RestartButton)
        
        #Builds a Cancel Button
        $CancelButton                   = New-Object System.Windows.Forms.Button
        $CancelButton.Text              = "Cancel"
        $CancelButton.FlatStyle         = "Popup"
        $CancelButton.Location          = New-Object System.Drawing.Size(150,80)
        $CancelButton.Size              = New-Object System.Drawing.Size(80,23)
        $CancelButton.Font              = "Microsoft Lucida Console,10"
        $CancelButton.Add_Click({
        
            $Timer.Stop()
            $RestartForm.Close()
            Write-Warning "Restart was aborted by $CurrentUser"
        
        })
        $RestartForm.Controls.Add($CancelButton)
        
        [void]$RestartForm.ShowDialog()
        
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM