Created
February 3, 2014 09:39
-
-
Save altrive/8781020 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
#Usage: Show-BallonTip "Title" "Message" | |
function Show-BalloonTip | |
{ | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory, Position = 0)] | |
[string] $Title, | |
[Parameter(Mandatory, Position = 1)] | |
[ValidateNotNullOrEmpty()] | |
[string] $Text, | |
[Parameter(Position = 2)] | |
[ValidateSet("Error", "Info", "None", "Warning")] | |
[string] $IconType = 'Info', | |
[Parameter(Position = 3)] | |
[int] $Timeout = 10000 #Min: 10sec / Max:30sec | |
) | |
Add-Type -AssemblyName "System.Windows.Forms" | |
if (!(Test-Path -Path Variable:\script:icon)) | |
{ | |
#Extract icon from powershell.exe | |
$psExe = Join-Path $PSHOME "powershell.exe" -Resolve | |
$script:icon = [Drawing.Icon]::ExtractAssociatedIcon($psExe) | |
} | |
$baloonTip = New-Object Windows.Forms.NotifyIcon -Property @{ | |
BalloonTipIcon = [Windows.Forms.ToolTipIcon] $IconType | |
BalloonTipText = $Text | |
BalloonTipTitle = $Title | |
Icon = $script:icon | |
Text = $Text[0..31] #Displayed when MouseOver at taskbar icon | |
Visible = $true | |
} | |
$baloonTip.ShowBalloonTip($Timeout) | |
#TODO:add_BalloonTipClosed event handler can't use in this context? | |
Register-ObjectEvent $baloonTip -EventName BalloonTipClosed -Action { | |
Unregister-Event $EventSubscriber.SourceIdentifier | |
Remove-Job $EventSubscriber.Action | |
$Sender.Dispose() #Need to dispose notify icon | |
} | Out-Null | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment