You’ve probably seen some applications that are able to display what looks like text in the notify icon system tray. You may have noticed that the NotifyIcon control we use in VB.NET has no feature for displaying actual text in the tray So, how could you show text in the icon tray like other applications do? A simple little trick will display your icon as text.
You actually have to make a bitmap in memory, draw the text you want to display, and use that bitmap you made as the icon to show in the system tray or notify area as some call it. This article has full sample code to accomplish this using Visual Basic.NET, Visual Basic 2008, VB 2010, and Visual Basic 2013. You can also download an example for doing this by clicking here.
Unfortunately there is only about 16×16 pixels of space for your icon text. So it will definitely limit the amount of text you can show in each spot. You should experiment with different Font Types and Font Sizes to get the best results. I also recommend you set the TextRenderingHint property to a higher quality setting like AntiAliasing which could make the text look much better compared to the default rendering hint. Also try to fine tune the position of the text drawn on your Icon to make full use of the icon area.
The first thing you want to do once you open up your project is add a NotifyIcon component to your project from the Toolbox. I named mine “notifyText”. You can also add a context menu to be used during a Right Mouse Click. Go to the Notify Icon Properties and under the ContextMenu option select the context menu if you added one. If you want, under the ‘Text’ property you can set the Tooltip Text for when the Mouse is hovering over the Notify Icon in the System Tray. There are also some events for the Notify Icon if you want to make use of them like _MouseDown, _DoubleClick, etc.
Visual Basic.NET, Visual Basic 2008, VB 2010, and Higher Source Code…
The first code is a “DestroyIcon” API Function I use to completely destroy/remove the icon from existance when i’m done with it. IF you do get a generic gdi+ error (or it may just say a Generic Error has Occured? I don’t remember), then try using this API call to kill the Icon you created in memory. I made an app that will display and update the cpu clockspeed/usage in the tray update every second or so and that Generic Error would popup every once in awhile and not really say much about it except it had something to do with something in the icon code. This function is good to use anyways to release the resources used by the icon.
'I'm using this in the createTextIcon sub to releases all of the resources that the icon/icons would have used. Public Declare Function DestroyIcon Lib "user32.dll" (ByVal hIcon As Int32) As Int32
Remember to optimize your text icon as much as you can so you can take full advantage of the limited space you have available for clarity. Set the “System.Drawing.Text.TextRenderingHint” to “System.Drawing.Text.TextRenderingHint.AntiAlias” to get smoother and easier to read text. You should setup the font and X/Y coordinates to make full use of the small space. And of course select a brush color that the user would be able to see well.
The code here is the declaration based code for variables and such…
'You should fine tune the font you want to use so the user can see the text you want them to see. 'Certain Fonts will obviously display your text better than other fonts might. Dim fontToUse As Font = New Font("Microsoft Sans Serif", 8, FontStyle.Regular, GraphicsUnit.Pixel) 'A basic brush with a Dark Blue Color. This should show up pretty well in the icon tray if the user uses the default tray color. Dim brushToUse As Brush = New SolidBrush(Color.DarkBlue) 'A bitmap used to setup how the icon will display. Dim bitmapText As Bitmap = New Bitmap(16, 16) 'A simply Grahics object to draw the text to a bitmap. Dim g As Graphics = Drawing.Graphics.FromImage(bitmapText) 'Will be used to get the Handle to the bitmap Icon. Dim hIcon As IntPtr
The code below will draw the text onto the bitmap image and set the notify icon to display it by using its handle.
'This code was actually taken from my CPUMonLite Application which displays the processors clockspeed in 'the system tray. But the clockspeed code is removed ' Sub createTextIcon() Try 'Clear any previous ‘stuff’ instead of creating a new bitmap. g.Clear(Color.Transparent) 'Setup the text, font, brush, and position for the system tray icon. For the font type and 'size I used, a good position for the X coordinate is a -1 or -2. And the Y coordinate seems 'to work well at a 5. 'You specify the actual text you want to be displayed in the draw string parameter that you 'want to display in the notify area of the system tray. You will only be able to display a 'few characters, depending on the font, size of the font, and the coordinates you used. g.DrawString("Text!", fontToUse, brushToUse, -2, 5) 'Get a handle to the bitmap as a Icon. hIcon = (bitmapText.GetHicon) 'Display that new usage value image in the system tray. notifyText.Icon = Drawing.Icon.FromHandle(hIcon) 'Added this to try and get away from a rare Generic Error from the code above. Using this API Function seems to have stopped that error from happening. DestroyIcon(hIcon.ToInt32) Catch exc As Exception MessageBox.Show(exc.InnerException.ToString, "Somethings not right?", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub
Now all you have to do is call that Sub above each time you want the text to change/update in the System Tray! Hopefully I made some sense in this article and you were able to follow ok. Have fun!
Minor Revision: 2014