任务栏上的批处理图标消息

Batch icon message on task bar

是否可以制作一个批处理,或者如果需要,是否可以制作一个.vbs,是否可以使批处理在下面有一个带有标志、电池和音量的小图标。

我希望它成为 "Shutting down in xx minutes/hours",然后单击它可能会取消它。

提前致谢:)

好吧,我知道如何完成您想要的部分内容 -- 通过借鉴 PowerShell 制作系统托盘气球提示。但我不知道如何让它听取气球的解雇。也许其他人可以在我的基础上提供另一个答案?

反正我是用这个来做一个flac批量转mp3的转换脚本。出于您自己的邪恶目的,请随意破解它。

@echo off
setlocal

for %%I in (*.flac) do (

    rem // This initiates the systray balloon.
    call :systray converting from "%%~nxI" to "%%~nI.mp3"
)

goto :EOF

rem // Here's the :systray function
:systray <message>
setlocal enabledelayedexpansion
set "args=%*"
set "args=!args:'=''!"
set "code="[void] [Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms');^
$o=New-Object Windows.Forms.NotifyIcon;$o.Icon='%systemroot%\System32\PerfCenterCpl.ico';^
$o.BalloonTipIcon='Info';$o.BalloonTipText='!args!';$o.BalloonTipTitle='%~nx0';$o.Visible=1;^
$o.ShowBalloonTip(10000);Start-Sleep -M 12000;$o.Dispose();Exit""
start /b "" "powershell" %code%
endlocal & goto :EOF

n值在$o.ShowBalloonTip(n1)Start-Sleep -Mn2 以毫秒为单位。加盐调味。


更新: 我发现了一些有关为 $o.BalloonTipClickedlovely example in the wild 注册事件的信息。基本上,替换为:

$o.ShowBalloonTip(10000);Start-Sleep -M 12000;$o.Dispose();Exit

...有了这个:

$o.ShowBalloonTip(10000);register-objectevent $o BalloonTipClicked clicked;^
if (wait-event clicked -t 12) {$true} else {$false}; $o.Dispose(); Exit

您还需要在单线程单元中执行 powershell 才能使事件正常运行。

start /b "" "powershell" -STA %code%

现在,您需要弄清楚如何在批处理过程中恢复相关性。一方面,您可能无法再使用 start /b 使气球提示成为非阻塞状态,并且您可能会使用 for /F 循环来捕获 [=19] 的输出=]命令。

让您更加担心的是,我建议 "Shutting down in xx minutes" 并非完全用户友好。如果 "Shutting down in 30 minutes" 出现在 29 分钟前,但用户刚刚看到怎么办? "Shutting down at 9:51 AM" 可能会更好。

所以考虑到所有这些,因为您想要的是事件驱动的,并且由于批处理语言不能那么容易地处理日期时间数学,我建议在 PowerShell 中完成所有该死的事情。使用 .ps1 扩展名保存。右键单击并使用 PowerShell 运行。或者,如果您想从 cmd 控制台执行它,请执行 powershell ".\scriptname.ps1".

set-executionpolicy remotesigned

if ([threading.thread]::CurrentThread.GetApartmentState() -eq "MTA") {
    & powershell.exe -window minimized -sta $MyInvocation.MyCommand.Definition
    exit
}

[void] [Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')

$minutes = 30
$launch_time = (Get-Date).AddMinutes($minutes).ToShortTimeString()

$o = New-Object Windows.Forms.NotifyIcon
$o.Icon = "$env:SystemRoot\System32\PerfCenterCpl.ico"
$o.BalloonTipIcon = "Info"
$o.BalloonTipText = "Shutting down at $launch_time"
$o.BalloonTipTitle = "Shutdown pending..."
$o.Visible = 1
function show-balloon { $o.ShowBalloonTip($minutes * 60 * 1000) }
show-balloon

$o_hover = [Windows.Forms.MouseEventHandler]{ show-balloon }
$o.add_MouseMove($o_hover)

register-objectevent $o BalloonTipClicked clicked

if (wait-event clicked -t ($minutes * 60)) {
    remove-event clicked
    $o.BalloonTipText = "Have a nice day!"
    $o.BalloonTipTitle = "Shutdown aborted"
    $o.ShowBalloonTip(10000)
    if (wait-event clicked -t 10) { remove-event clicked }
} else {
    # Initiate shutdown sequence on my mark.  Authorization rojo alpha 3.  Mark.
    stop-computer
}

unregister-event clicked
$o.Dispose()

Bill_Stewart,如果您正在阅读本文,我知道您很高兴。碰巧的是,这次 PowerShell 确实是完成这项工作的正确工具。