Tag Archives: Text

How to Display Text in the System Tray/Notify Icon in VB.NET and Higher

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (9 votes, average: 3.56 out of 5)
 

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.


More Info…

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.


Getting Started…

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

 

The code from this article in use!

 


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!

                Jason

Minor Revision: 2014

Increase or Decrease the GDI+ Graphic’s Quality and Speed using VB.NET, VB 2010, and Higher

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
 

Usually by default, the GDI+ Library is set for higher speed rendering and lesser quality output. Thankfully they give control over a few of the quality settings for rendering images, drawing, filling, etc..  But one of the current problems with GDI+ is that it is not GPU hardware accelerated like the original GDI Classic API library. Since many users computers are very powerful this day and age, it may not be to much of a concern. But there are still those users with slower computers and if you set the graphic quality to high it will weigh down (slow down) their computer. So sometimes you may have to make a compromise between quality and performance. Or give the user the option to increase/decrease the quality manually. This little article is basically just going to show some of the the quality based features of GDI+ for either increasing or decreasing the quality and/or performance. Obviously if your not a VB.NET beginner, you may already know of these features and would not benefit from this post. 🙂

Note:

This article is mainly to highlight some features you can use to increase/decrease the quality/performance of your graphics rendering. I will not go in-depth on the features and you can find resources on exactly what each feature means and/or does at Microsoft’s website.

Text Rendering…

Quite often you may need to draw text using GDI+ in your projects. There is a little feature available that can have a pretty big effect on the quality of your text. GDI+ has a feature that has a big effect on Text rendering. Its a property called: “TextRenderingHint” in the Graphics Class. If you check out that property under the ObjectBrowser(Press F2 is view) there are a few enum’s available to set this property. You can specify SystemDefault rendering up to AntiAliasGridFit and even ClearTypeGridFit. In my opinion I like the ClearTypeGridFit rendering effect and usually draw text with it. But AntiAlias is also higher quality setting and will ‘Smooth’ out the edges/corners of your text to make it look better. But as you may already know, the ClearType and AntiAlias settings WILL use more resources. So you may want to use SingleBitPerPixel rendering. Which will be low quality, high speed rendering. You will probably want to tinker with the 6x(Six) TextRenderingHint settings. Below is a simple example…

        '
        'Either of these settings will give you higher quality but at the cost of performance.
        '
        'High quality rendering
        g.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit

        'High quality rendering
        g.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAliasGridFit

        'This will give somewhat lower quality but better performance.
        g.TextRenderingHint = Drawing.Text.TextRenderingHint.SingleBitPerPixel

 

Line Based Rendering…

Usually by Default when you draw lines it is using High Speed rendering and not High Quality. Thankfully GDI+ has that covered. By Default, at least on my computer drawing lines is very jagged and rough. So you very well may want to ‘Smooth” out your drawn lines. Thats where the Graphics.SmoothingMode Property comes into play. There are a few settings available depending on the speed/quality you want. Obviously the “HighQuality” setting should be the best out of the settings, while HighSpeed should be the worst quality setting. There are other settings available like “AntiAlias” and “Default”. If you hate jagged lines and curves or edges, then you will want to use either the “AntiAlias” setting or the “HighQuality” setting. If you want the quickest rendering then use “HighSpeed“. On my computer at least the “Default” setting is very low quality. So you may Not want to use it. 🙂

Just a note – I am not really able to see to much of a difference between using “AntiAlias” and “HighQuality”. But that definitely doesn’t mean they are the same. “HighQuality” will be slower to render and “AntiAlias” should be a little quicker to render.

To set the quality for your line/shape,ect.. drawing just set the SmoothingMode property as seen below…

        'Simply declare a Graphics object.
        Dim g As Graphics

        'This will give the best looks available but also the worst performance.
        g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality

        'This will give the worst looks but the Best performance.
        g.SmoothingMode = Drawing2D.SmoothingMode.HighSpeed

 

Image Based Rendering

The last rendering in this article is for Drawing Images. There are a couple features available for doing “HighSpeed” and/or “HighQuality” Image rendering. The first to go over is: “InterpolationMode”. You would want to use this mode for doing Image Scaling and/or Rotating. There are quite a few settings you can choose in this Property. From “Low” quality rendering up to “HighQualityBiCubic” rendering. As the names suggest, the “Low” setting will give the Best performance but the Worst quality. Likewise, “HighQualityBiCubic” should give the Best quality but the Worst performance. There are many settings available if you want to reach a compromise between quality and speed. Like specifying “Bilinear” only rendering or “BiCubic” rendering. Use the different settings till you get the results your after. “CompositingQuality” Is another feature related to Image rendering quality.

Below is a explanation from Microsoft on Compositing Images.

Compositing is done during rendering when the source pixels are combined with the destination pixels to produce the resultant pixels. The quality of compositing directly relates to the visual quality of the output and is inversely proportional to the render time. The higher the quality, the slower the render time. This is because the higher the quality level, the more surrounding pixels need to be taken into account during the composite.

The “AssumeLinear” setting apparently gives you a ‘balance’ between quality and speed. So use this setting if you want alittle of both. Below is a simple example of setting the Compositing quality…

        'This will give you high quality but also the worst performance.
        g.CompositingQuality = Drawing2D.CompositingQuality.HighQuality

        'This will give lower quality but the Best performance.
        g.CompositingQuality = Drawing2D.CompositingQuality.HighSpeed

That’s about it. Take Care 🙂

Jason

Highlight Textbox Text on Focus in VB 6.0

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 2.00 out of 5)
 

Simple code snippet for Visual Basic 6.0 to highlight all of the text in a textbox control when receiving focus.


'textbox - GotFocus() Event

Private Sub Text1_GotFocus()

Text1.SelStart = 0

Text1 = SelLength = Len(Text1)

End Sub

Read a Text File and Add text to a Textbox Control With Stream Reader Class – VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (10 votes, average: 4.10 out of 5)
 

This will open a textfile and then add the text contents to a textbox control using VB.NET, VB 2008, VB 2010, and Visual Basic 2013.

        '
        'Read from a text file to a textbox with the Stream Reader Class
        'Put a textbox on the form and name txt and set to multi-line
        '
        'This will make the Reader read the entire contents of the textfile and write to the
        'TextBox
        'Create a new stream reader specifying its source to be from the newTextFile.txt file.
        Dim sReader As IO.StreamReader = New IO.StreamReader("c:\newTextFile.txt")
        '
        'Make the textbox keep the current data while adding the new data from the stream.
        txt.AppendText(sReader.ReadToEnd)

Revisted: 2015

Find/Search for a String in a Listbox/Combobox control by using VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
 

This will search for a String value in the Listbox or Combobox items in Visual Basic.NET, VB 2005 and Higher

            '
            'TxtItem.Text would be a textbox control whose text string will be searched. And of course, lstItems
            'is a Listbox control or you can make it a Combobox control. The code below shows searching
            'for a  partial/exact string at either the beginning of the control or at a specified Index value.
            '
            'Search a Listbox Control for a exact string
            lstItems.SelectedIndex = lstItems.FindStringExact(txtItem.Text)
            '
            'Search a Listbox Control for a partial string
            lstItems.SelectedIndex = lstItems.FindString(txtItem.Text)