Loading...

Build a PowerShell systray tool with menus, sub menus and pictures


In this post I will demonstrate how to build a tool that displays context menu and sub menus in the systray bar with picture for each menus.


In a previous post I explained how to build a systray tool with PowerShell.

I also explained in another post how to add sub-menus to your systray tools.


I have been asked how to add a specific picture for each menu and sub-menu.

Now let's see how to proceed.


What do we want ?

The icon of the main systray tool is the below one:


The systray tool will contain the below context menus:

- Menu 1

- Menu 2

- Restart the tool

- Exit


Both Menu 1 and Menu 1 will display two submenus.

The restart part will allow you to close the systray tool and open it again 10 seconds later (in case of bug).

The Exit button will allow you to close the systray tool


We will add picture on submenus for Menu1.

See below an overview of the final result:



Create the main part

In this part we will create the main systray structure.

To create a systray menu we will use the Windows Form class, as below:

System.Windows.Forms.NotifyIcon


We will then proceed as below:

1. Add the NotifyIcon class in a variable:

2. Change the text displayed when you pass the mouse over the icon:

3. Set a icon for our systray tool and make the tool visible:

4. Hide the PowerShell window when the systray tool:

5. Force a garbae collection (to manage RAM usage):

6. Create an application context for it to all run within.

7. See below the result:


Ok, we have just the systray tool but no menus, let's add them.


Add menus and submenus

To add menus we will use the class ContextMenuStrip:

System.Windows.Forms.ContextMenuStrip


To add submenus we will use the class ToolStripMenuItem:

System.Windows.Forms.ToolStripMenuItem


We will then proceed as below:

1. Create the main menu

2. Create submenus

3. Add the context menu object to our main systray tool object:

4. See below the result:


Now we have have a complete systray tool with menus and submenus, let's add some icon for each one.


Add icon to menu item

To add menu we will need to convert picture to a bitmap object.

For that we will use the class [System.Drawing.Bitmap] and integrate a picture file into it, as below:

1. Add icon to main menu

2. Add icon to sub menus

3. See below the result:


Add action to menu

The last step is to add action when you click on menu or submenu.

For that we will use the Add_Click event on each menu and submenu.

In the below example, we will add an action for each submenu. The action will be to display a message box.

See below the code:

WPF_Systray 3158189338260897839

Publier un commentaire

3 commentaires

Thomas a dit…
Ce commentaire a été supprimé par son auteur.
Thomas a dit…

End of the article is missing after "See below the code:"

Anonyme a dit…

Don't you also need a load assembly line at the start too?

Add-Type -AssemblyName System.Windows.Forms

Accueil item

Award

Mes articles en français

Author of Books

PowerShell GUI & WPF Group

PowerShell GUI & WPF Group

Join the FPSUGR

Join the FPSUGR

Stats