Last active
September 9, 2021 01:47
-
-
Save Andy-AO/ba504458578dc85b52101a21402bb182 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[cmdletbinding()] | |
Param ( | |
[string] | |
$Title = 'Title', | |
[string] | |
$Text = 'Text', | |
[scriptblock] | |
$OnClicked = { | |
'Pressed!' | Write-Host -ForegroundColor 'Blue' | |
} | |
) | |
Add-Type -AssemblyName System.Windows.Forms | |
$NotifyIconForShowNotification = New-Object System.Windows.Forms.NotifyIcon | |
$balloon = $NotifyIconForShowNotification | |
$balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon((Get-Process -id $pid).Path) | |
$balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Info | |
$balloon.BalloonTipText = $Text | |
$balloon.BalloonTipTitle = $Title | |
$balloon.Visible = $true | |
$suffix = Get-Random | |
$SourceIdentifier = "click_event_$suffix" | |
Register-ObjectEvent $balloon BalloonTipClicked -sourceIdentifier $SourceIdentifier | |
try { | |
$balloon.ShowBalloonTip(5000) | |
Wait-Event -SourceIdentifier $SourceIdentifier > $null | |
if ($OnClicked) { | |
& $OnClicked | |
} | |
} | |
finally { | |
Unregister-Event -SourceIdentifier $SourceIdentifier -ErrorAction SilentlyContinue | |
Remove-Event $SourceIdentifier -ErrorAction SilentlyContinue | |
$balloon.Dispose() | |
Remove-Variable 'balloon' -Force -ErrorAction Ignore | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment