gpt4 book ai didi

powershell - 在 PowerShell 中正确显示托盘气球工具提示

转载 作者:行者123 更新时间:2023-12-04 08:46:31 25 4
gpt4 key购买 nike

短版 :我想我需要帮助来正确使用 PowerShell 中由于 Windows 消息而调用的事件,以消除气球工具提示的图标。

长版 :

我有一个长时间运行的 PowerShell 命令(一个构建),我希望在它通过系统托盘/通知区域中的气球工具提示完成时收到通知。

我能够创建一个 Write-BalloonTip 脚本(如下),大致可以满足我的要求。唯一的问题是,as sometimes happens with tray icons ,托盘图标不会消失,直到我将鼠标悬停在它上面。通过重新使用相同的全局变量来表示 NotifyIcon ,我可以重新使用该脚本并保留它,以便只保留一个系统托盘图标(直到我将鼠标悬停在它上面)。这仍然感觉像一个黑客。我试图添加一个事件处理程序,以便在 BalloonTipClosed 上得到通知。事件,然后在那里处理它。在事件处理程序中,我尝试了所有三种建议用于摆脱挥之不去的图标的技术,但均无济于事。

烦人的部分是一个简单的 .Dispose似乎对脚本的后续调用起作用,这让我认为根本没有调用事件脚本块。

我已经验证 BalloonTipClosed 在提示在单独的 WinForms 应用程序中消失后被调用。

我错过了一些基本的东西吗?任何帮助深表感谢。谢谢!

这是“Write-BalloonTip.ps1”的代码:

param
(
$text,
$title = "",
$icon = "Info",
$timeout=15000
)

[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | out-null
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") | out-null

if ($global:writeBalloonTipIcon)
{
# This gets rid of the previous one
$global:writeBalloonTipIcon.Dispose()
}

$global:writeBalloonTipIcon = new-object System.Windows.Forms.NotifyIcon
$global:writeBalloonTipIcon.Icon = [System.Drawing.SystemIcons]::Information

# FIXME: This *should* cleanup the icon after it's done, but it doesn't seem to work
$global:writeBalloonTipIcon.add_BalloonTipClosed(
{
# this *should* work, but it's not. What am I missing?
$global:writeBalloonTipIcon.Icon = $null;
$global:writeBalloonTipIcon.Visible = $false;
$global:writeBalloonTipIcon.Dispose();
});

$global:writeBalloonTipIcon.Visible = $true;
$global:writeBalloonTipIcon.ShowBalloonTip($timeout, $title, $text, $icon);

最佳答案

我认为您需要在 STA 线程中执行此代码。默认情况下,PowerShell(此处显示 v2)在 MTA 线程中执行:

PS U:\> [System.Threading.Thread]::CurrentThread


ManagedThreadId : 5
ExecutionContext : System.Threading.ExecutionContext
Priority : Normal
IsAlive : True
IsThreadPoolThread : False
IsBackground : False
ThreadState : Running
ApartmentState : MTA
CurrentUICulture : en-US
CurrentCulture : en-US
Name : Pipeline Execution Thread

关于powershell - 在 PowerShell 中正确显示托盘气球工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2179294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com