Skip to content

Instantly share code, notes, and snippets.

@Andy-AO
Last active September 9, 2021 01:47
Show Gist options
  • Save Andy-AO/ba504458578dc85b52101a21402bb182 to your computer and use it in GitHub Desktop.
Save Andy-AO/ba504458578dc85b52101a21402bb182 to your computer and use it in GitHub Desktop.
[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