Example Code

Icon in Windows system tray with LabVIEW

Code and Documents

Attachment

Overview

This is a LabVIEW API for creating an icon in the Windows system tray (AKA notification area), complete with nested menus and events.  It uses the .NET framework NotifyIcon class.  While developing this, I found this similar implementation, but continued with my own to implement a few more features I required.

Description

If you are looking around for a way to have a system tray icon for your LabVIEW application, as I was, you'll find that .NET is the way to go.  You can try calling the Win32 API via Shell32.dll function Shell_NotifyIcon, as suggested here, but that is often difficult to do from LabVIEW directly.  You will also have a very hard time getting events, such as mouse clicks on the icon or menus, back to LabVIEW.  Another option is existing ActiveX wrappers people have made.  NI has one that comes with CVI (posted here), and there are others around the web that are sometimes free but sometimes not (random example, random example, VI wrapped example).  ActiveX gives a nice API and solves the event issue, but you are limited to the features that have been exposed to you through that particular ActiveX wrapper, which is up to the wrapper developer.  However, .NET acting as the new Windows API gives a nice, featured interface that is easy to use from LabVIEW.

NotifyIcon Palette.png

Features:

  • Basic stuff:
    • Dynamically change icon.
    • Show Balloon Tips.
    • Create context menus (right-click menu)
    • Examples!
  • More interesting stuff:
    • Events captured by LabVIEW event structure.  Rather than have each event (menu click, balloon tip click, icon double-click, etc.) fire their own callback VI, I let the developer decide which events to capture, then those events are all sent to the LabVIEW Event structure as a NotifyIcon User event.  Check out the included examples to see how this works.  The idea is that all events are piped to 1 location and handled by the structure, instead of having VIs fire at will.

NotifyIcon Event Example.png

    • Create nested context menus.  I actually decided this would be a fun case for trying out the new native recursion in LabVIEW, so you can nest menus to your heart's content  Overkill? yes, but it's fun, and better than not being able to nest menus at all like in most of the ActiveX implementations I tried.

NotifyIcon menus.png

    • Return a reference to any MenuItem.  To go with the nested menus, you can call this recursive code to return a reference to any MenuItem.  You can then make any direct .NET calls to change useful properties like Visible, or Checked.  Again, see the examples.

NotifyIcon misc examples.png

VIs are saved in LabVIEW 2009, and this was developed with .NET 3.5.  I you have a different .NET version/requirement just give it a try.  I would be surprised if this wasn't pretty backward/forward compatible.

Installation instructions: Download NotifyIcon.zip, then unzip to your user.lib folder and restart LabVIEW. 

BUG FIX 4/5/2011 - Some LabVIEW references to objects were not being properly closed, though the objects themselves were being destroyed.  This could cause a memory leak which would be noticable if you do a lot of icon or menu changing.  NotifyIcon.zip has been updated to fix this.  Thanks to DSmith for finding the issue.  See the comment from DSmith below for more information.

Steps to Implement or Execute Code

Run either of the two examples included in the attachment for guidance on using this API.

Requirements

Software

LabVIEW 2009 or later

Hardware

None

**This document has been updated to meet the current required format for the NI Code Exchange. For more details visit this discussion thread**

Brian A.
National Instruments
Applications Engineer

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
Jervin Justin
NI Employee (retired)
on

Very nicely done! And thanks for the helpful links to existing documents like the CVI implementation as well.

Jervin Justin
NI TestStand Product Manager
Balaji_DP
Active Participant
Active Participant
on

i need this show notify vi's in labview 8.6 can you  help me...?

Regards,
Balaji DP
Brinoceros
NI Employee (retired)
on

Unfortunately, because native recursion is a feature of LabVIEW 2009, this code can't be directly ported to 8.6.  I would recommend using this similar implementation, or at least using it as an example of how to use the .NET interface to NotifyIcon from LabVIEW.

Brian A.
National Instruments
Applications Engineer
Dominic.Walker
Member
Member
on

This is fantastic! Very in-depth and helpful, handy links too.

Dominic Walker
Cardiff University
Electrical and Electronic Engineering Student
Prabhakant_Patil
Member
Member
on

This Is very nice and useful

thanks a lot

Regards
Prabhakant Patil
zsolt.trenyik
Member
Member
on

<p>Thanks for your vi's! I had only one question: how to put the running application completely to the notification are, like a virus killer or something similar? here (http://digital.ni.com/public.nsf/allkb/E277CBA8C9065C1A86256D250021D9FA) the title is right, but it seems to me that this method adds the icon to the notification area with many functionality, but leaves the running application in the system tray like a normal application e.g. word processor. thank you!</p>

// Electrical Engineering, MSc (software engineering) //
Brinoceros
NI Employee (retired)
on

To remove the item in the Task Bar, go to File >> VI Properties >> Window Appearance >> Customize... and uncheck "Allow user to minimize window."  That works for VIs.  For an executable, I think you also need to add the ini key HideRootWindow=True. 

Brian A.
National Instruments
Applications Engineer
zsolt.trenyik
Member
Member
on

Hi all,

(beside HideRootWindow=True there is an another useful possibility in the ini-file to allow multiple instances of the app: allowmultipleinstances = TRUE.)

To this topic: I have only one problem. In the example there is a basic solution to show the balloon tooltip, namely just set a boolean to true. But how could I catch the event that the user is standing with the mouse cursor over the icon of my application in the notification area? There are many possibilities in the Notifiicon User Event: User event structure, but they doesn't seem to work at this level. This would be the final step to the perfection

thaks a lot. zsolt

// Electrical Engineering, MSc (software engineering) //
Brinoceros
NI Employee (retired)
on

Zsolt - This is available from the MouseMove event, which is handled as a LabVIEW User Event with this API in the same manner as the rest of the events.  However, it's tricky to use because as you move your mouse over the icon you will receive a series of events.  There isn't a simple single event (like LabVIEW's "Mouse Enter" event) available from the NotifyIcon .NET class.

If all you want is the pop-up ToolTip text that usually shows when you put your mouse over an icon in the system tray, just write to the Text property of your NotifyIcon instance.

Brian A.
National Instruments
Applications Engineer
zsolt.trenyik
Member
Member
on

Hi Brinoceros,

thanks for the quick reply. Could you please explain your last tip a little more detailed, maybe just with a screenshot? I can't find the right solution. thank you.

// Electrical Engineering, MSc (software engineering) //
Brinoceros
NI Employee (retired)
on

Try this: 

icon_tooltip.png

If that's not what you're after, take a look at "NotifyIcon - Example 2.vi" and add MouseMove to the event registration.

Brian A.
National Instruments
Applications Engineer
NicLinley
Active Participant
Active Participant
on

Fabulous

smithd
Active Participant
Active Participant
on

Hey all,

First, let me say that this is great. I've been using this to update some of our internal tools to take up less screen space, and it has been great so far. However, in my use of the program, I discovered a bug. The set icon VI does not properly clear out the reference to the old icon, resulting in a slow memory leak. I created a set of VIs and exes which demonstrate the problem, below.

http://decibel.ni.com/content/docs/DOC-15493

(warning: I set the loop on the demonstrator VI to run as quickly as possible, so the buggy application.exe will go from 0 to about 300 MB of ram used in ~4 seconds. Be very very careful that you do not crash your machine)

The files attached to that link also contain the fix for that bug.

Thanks again for the great example!

Brinoceros
NI Employee (retired)
on

Thanks DSmith.  It is now fixed, along with a couple of other instances of the same issue.  NotifyIcon.zip has been updated with the changes.

Brian A.
National Instruments
Applications Engineer
David S.
NI Employee (retired)
on

Hmm...I can't get LV to catch the "Disposed" MenuItem event. Has anyone else had this problem? Does anyone have any debugging tips to see whether the event is firing correctly, or whether LV is listening to it properly?

David Staab, CLA
Staff Systems Engineer
National Instruments
Brinoceros
NI Employee (retired)
on

The reason you cannot catch the Disposed event is that in "NotifyIcon - Destroy.vi" all events are destroyed before the Dispose method is called.  I'll change this when I get the chance, but in the mean time you can switch the order of execution of the Dispose method and where the event registration is destroyed in "NotifyIcon - Destroy.vi" (just switch their location along the reference wire).

Brian A.
National Instruments
Applications Engineer
spud1
Member
Member
on

Hello Brinoceros,

Your code works great with XP but having problems with Windows 7.

Any suggestions?

David S.
NI Employee (retired)
on

It works fine in Win7x86 for me. Which bitness are you using? Can you describe the "problems" in more detail? Do you have .NET 2.0 or higher installed on your Win7 machine?

David Staab, CLA
Staff Systems Engineer
National Instruments
Prabhakant_Patil
Member
Member
on

Hi

I have used this notification, It works with the source code nicely.

But when I create EXE / Installer and tried to install on other System, then it wont work properly.

Kindly suggest me on this

Regards

Prabhakant

Regards
Prabhakant Patil
spud1
Member
Member
on

spud1
Member
Member
on

Hi David S,

I am using Windows 7 X86 - 32BIT and the .NET 2.0 is installed. Also, I have tried a Windows 7 X86 64BIT with .NET 2.0 and still the same problem.

What I see is that the program loads but never is allowed to have the ICON in the Tray before the program aborts.

I am planning to install LabView on to Windows 7 32Bit computer and then run the code through the LabView environment Window.

Any other suggestions?

Thanks,

Mike

Brinoceros
NI Employee (retired)
on

Hi All,

Can we move support-type questions to the discussion forums?  I'll try to help out as I can, but the comments on this document aren't really the place for it and you'll get much better support on the forums.  Feel free to drop a link to your forum post as a comment on here to draw some attention, but lets keep the full support discussions on the forums.  Thanks!

For you guys having trouble with executables, I have used this code in executables on XP and Win 7 x86 so it should work fine.  It should really just depend on the .NET API anyway.  Make sure you're properly handling errors from the VIs so you'll get feedback from your executable. And just simplify things: try building one of the supplied examples into an executable and see if that works.

Brian A.
National Instruments
Applications Engineer
Rumcajs
Member
Member
on

I need it in 8.5 version. Can anybody convert Notifyicon.zip to 8.5 and send to me.

roman.nowakowski@trw.com.

Brinoceros
NI Employee (retired)
on

Sorry, it can't be done.  See my first comment on this document (3rd comment overall).

Brian A.
National Instruments
Applications Engineer
VeeJay
Active Participant
Active Participant
on

This is really helpful. THank you! It works great. Any issues when creating an application? And I am assuming all the issues concerning memory leaks have been fixed and this pallete can be safely used now.

I may not be perfect, but I'm all I got!
Randall_Dannemann
Member
Member
on

Seems to work well even in LV8.5, it just takes changing the vi to fit the old style of recursion; a very minor addition of code.

FlamingYawn
Member Member
Member
on

HideRootWindow=True doesn't seem to work for executables in Windows 7.  Am I doing something wrong?  Is there another way to do this?  I want to hide the application window taskbar icon for an executable.

Robert Mortensen
CLA, CLED, LabVIEW Champion, World's Fastest LabVIEW Programmer
Principal Systems Engineer, Endigit
Andre_H
Member
Member
on

Thx for the greate code and help file

Labview 2010
samsharp99
Member
Member
on

Hi, I had some problem with memory leaks when changing the menu items and re-registering the events. I've posted it on the discussion forums here: http://forums.ni.com/t5/LabVIEW/NotifyIcon-Memory-Leak-amp-New-Function/td-p/2702093. As part of implementing a workaround, I also implemented a new function which enables/disables menu items - it is attached to the post.

IanS
NI Employee (retired)
on

ignore, deleted my post

Ian S
Applications Engineer CLA
National Instruments UK&Ireland
DA_Jakob
Member
Member
on

Frist I have to say, Great Work!

I have two questions for customization of the tray icon menu:

Can I customize the checked/radiocheck Icons?

Why it is not possible to check a partent menu-item?

Thanks Jakob

Corey.Rotunno
Member
Member
on

Just got this working, great work! This should be included in LabView out of the box. I am excited to see how I can utilize this in my applications.


Corey Rotunno

vekkuli
Member
Member
on

I recommend adding the GC.Collect() -workaround to unreserve the callback VI and it's children.

 

See: http://www.ni.com/product-documentation/52150/en/#468139_by_Category and https://forums.ni.com/t5/LabVIEW/net-event-callback-VIs-Why-do-they-never-leave-running-state/td-p/3...

 

For my application it was enough to call it in the Destroy VI in the end.

Capture.PNG

instrumento
Active Participant
Active Participant
on

This seems to work fine, but:

- It doesn't display an info box (not sure what the official name is) when the mouse hovers over the tray icon, as in e.g. when I hover over the sound tray icon it displays a little box with "Speakers: xx%"

- Is this because this NotifyIcon implementation uses "ContextMenu" instead of its replacement "ContextMenuStrip"?

-If so, has anyone updated it with the new functionality of "ContextMenuStrip"?

instrumento
Active Participant
Active Participant
on
@Brinoceros

Answering my question (should have spent  a little more time looking at it).

This implementation never sets the NotifyIcon text property.  Seems it could be added as an argument to "NotifyIcon - Create.vi" quite easily.

J.C._Andersen
Member
Member
on

Hi.

Would you consider making a VI Package of this tool? It would make the installation much better and more safe.

Thanks.

Best regards
Jens Christian Andersen.
CLA, CTA, CPI
Contributors