Spotlight on the NotifyIcon Control

The “Spotlight on Controls” series focuses on a single WinForms control in PowerShell Studio 2012, details the important Properties, Methods, and Events of the control and demonstrates how to utilize the control. Most of the information about the controls is still applicable to previous versions of PrimalForms.

Last time we took a look at the ListView control. This time we will look at the NotifyIcon control:

NotifyIcon [System.Windows.Forms.NotifyIcon]

Specifies a component that creates an icon in the notification area of the Window’s taskbar.

NotifyIcon

Default Event: MouseDoubleClick

Why use a NotifyIcon control?

Use the NotifyIcon control, to alert users of special events, such as when a task is completed. Typically this control is used for informational purposes, but it can also be used as a source for user interaction.

Note: When you add a NotifyIcon it will appear at the bottom of the Designer:

NotifyIcon in Designer

 

Important Properties:

BalloonTipText

This property sets the text associated with the balloon ToolTip. Use this property to display a message in the Balloon Tooltip.

Setting the BalloonTipText in the script editor:

$NotifyIcon.BalloonTipText = "This is the balloon text"

BalloonText

BalloonTipTitle

This property sets the title of the balloon ToolTip.
The title text is displayed above the balloon text.

Balloon Title

BalloonTipIcon

This property sets the icon to associate with the balloon Tooltip. In other words, the icon displayed inside the tooltip itself.
Note: The BalloonTipTitle property must be set in order to view the balloon icon.

Values (Default: None):

None
No icon is displayed.
Icon None

Info
An information icon.
Icon Info

Warning
A warning icon.
Icon Warning

Error
An error icon.
Icon Error

Icon

This property sets the icon to display in the system tray.

Important: This property must be set; otherwise the tooltip balloon will not show!

The designer will allow you to browse and select an icon to display when the tooltip is shown.

Click on the browse button in the Property Panel:

Icon Property in the PropertyPanel

Or use the menu in the designer:

Choose Icon Designer

Next select the Icon file

Open Icon File Dialog

Finally the Icon will be displayed in the system tray:

Icon in System Tray

Note: If a “phantom” icon remains in the system tray after closing the form, then it is recommended set the Visible property to False in order to clear the icon before closing form.

ContextMenuStrip

This property sets the shortcut menu to show when the user right-clicks the icon.
Set this property to an existing ContextMenuStrip in order to assign a menu to the system tray icon. See Spotlight on the ContextMenuStrip Control for more details.

NotifyIcon's ContextMenuStrip

Visible

This property indicates whether the icon is visible in the notification area of the taskbar.

Values (Default: True):

True / False

Important Events:

BalloonTipClicked

This event occurs when the balloon tip is clicked. Use this event to react to user clicks in the ToolTip balloon.

$notifyicon1_BalloonTipClicked={
    Write-Host 'The Balloon Tip was Clicked'
}

BalloonTip Click

 

Clicked & DoubleClicked

These events occur when the system tray icon is clicked. If you need more information such as which mouse button was used, then it is recommended to use the MouseClick events (See below).

MouseClick

This event occurs when a user clicks on the system tray icon.

[System.Windows.Forms.LabelEditEventHandler]

Properties Description
Button Gets which mouse button was pressed.
Clicks Gets the number of times the mouse button was pressed and released.
Delta (Not applicable)
Location Gets the location of the mouse during the generating mouse event.
X Gets the x-coordinate of the mouse during the generating mouse event.
Y Gets the y-coordinate of the mouse during the generating mouse event.

 

$notifyicon1_MouseClick=[System.Windows.Forms.MouseEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.MouseEventArgs]
    Write-Host "System Tray Icon Mouse Click: $($_.Button) Clicks: $($_.Clicks)"
}

MouseDoubleClick

These events occur when a user double clicks on a system tray icon. This event has the same arguments as the MouseClick event.

$notifyicon1_MouseDoubleClick=[System.Windows.Forms.MouseEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.MouseEventArgs]
    Write-Host "System Tray Icon Mouse Double Click: $($_.Button) Clicks: $($_.Clicks)"
}

Important Methods:

ShowBalloonTip

This method displays a balloon tip in the taskbar for the specified time period.

 

[void] ShowBalloonTip([int] timeout)

Note: The method uses the properties of the NotifyIcon to display the balloon tip. Therefore they must be set before calling this method.


$NotifyIcon.ShowBalloonTip(0)
 

[void] ShowBalloonTip([int] timeout, [string] tipTitle, [string] tipText, [ToolTipIcon] tipIcon)

Note: This variation, displays a balloon tip with the specified title, text, and icon in the taskbar for the specified time period. You need not set the NotifyIcon’s properties if you use this method variation.

$NotifyIcon.ShowBalloonTip($Timeout, $BalloonTipTitle, $BalloonTipText, $BalloonTipIcon)

 

Helper Function:

The following is a helper function that allows you to display the NotifyIcon. The help function also assigns the calling executable’s icon, if the NotifyIcon’s Icon property hasn’t been assigned.

function Show-NotifyIcon
{
<#
    .SYNOPSIS
        Displays a NotifyIcon's balloon tip message in the taskbar's notification area.
    
    .DESCRIPTION
        Displays a NotifyIcon's a balloon tip message in the taskbar's notification area.
        
    .PARAMETER NotifyIcon
         The NotifyIcon control that will be displayed.
    
    .PARAMETER BalloonTipText
         Sets the text to display in the balloon tip.
    
    .PARAMETER BalloonTipTitle
        Sets the Title to display in the balloon tip.
    
    .PARAMETER BalloonTipIcon    
        The icon to display in the ballon tip.
    
    .PARAMETER Timeout    
        The time the ToolTip Balloon will remain visible in milliseconds. 
        Default: 0 - Uses windows default.
#>
     param(
      [Parameter(Mandatory = $true, Position = 0)]
      [ValidateNotNull()]
      [System.Windows.Forms.NotifyIcon]$NotifyIcon,
      [Parameter(Mandatory = $true, Position = 1)]
      [ValidateNotNullOrEmpty()]
      [String]$BalloonTipText,
      [Parameter(Position = 2)]
      [String]$BalloonTipTitle = '',
      [Parameter(Position = 3)]
      [System.Windows.Forms.ToolTipIcon]$BalloonTipIcon = 'None',
      [Parameter(Position = 4)]
      [int]$Timeout = 0
     )
    
    if($NotifyIcon.Icon -eq $null)
    {
        #Set a Default Icon otherwise the balloon will not show
        $NotifyIcon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon([System.Windows.Forms.Application]::ExecutablePath)
    }
    
    $NotifyIcon.ShowBalloonTip($Timeout, $BalloonTipTitle, $BalloonTipText, $BalloonTipIcon)
}

Example Use:

Show-NotifyIcon -NotifyIcon $notifyicon1 -BalloonTipText $textboxText.Text `
 -BalloonTipTitle $textboxTitle.Text -BalloonTipIcon $combobox1.SelectedItem

 

You can download the NotifyIcon Sample from our Downloads section.