8

I want to display an icon [a .ico file] in System tray with some text added to it at runtime. Is there any native WPF way to do it? or snippet for GDI+ also would be grateful.

Thank you.

iraSenthil
  • 11,307
  • 6
  • 39
  • 49

2 Answers2

7

Here is the code that worked for me,

public static Icon GetIcon(string text)
{
    //Create bitmap, kind of canvas
    Bitmap bitmap = new Bitmap(32, 32);

    Icon icon = new Icon(@"Images\PomoDomo.ico");
    System.Drawing.Font drawFont = new System.Drawing.Font("Calibri", 16, FontStyle.Bold);
    System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.White);

    System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap);

    graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
    graphics.DrawIcon(icon, 0, 0);            
    graphics.DrawString(text, drawFont, drawBrush, 1, 2);

    //To Save icon to disk
    bitmap.Save("icon.ico", System.Drawing.Imaging.ImageFormat.Icon);

    Icon createdIcon = Icon.FromHandle(bitmap.GetHicon());

    drawFont.Dispose();
    drawBrush.Dispose();
    graphics.Dispose();
    bitmap.Dispose();

    return createdIcon;
}
iraSenthil
  • 11,307
  • 6
  • 39
  • 49
0

Since the System tray icons do not have text accompanying them, you can either overlay the text on the icon itself, or use a tooltip-style pop-up.

Overlaying text (probably by embedding it in a proceduraly generated icon) would be very fiddly, but more importantly is probably a bad idea, since icons are best used to display categorical status information, with more detailed information being available in pop-up text or a pop-up window.

For example, if an icon is representing inbox status, then it is enough to show "new mail" versus "no new mail", without needing to show number of new mails. That allows for a clearer icon to be displayed that does not need to be deciphered in detail by the user.

A greyer area is an icon indicating a process status. You could just have "in progress" versus "complete" icons, but I have seen icons with progress bars embedded in them, that I thought was a neat way to do it. Obviously a progress bar in a tiny icon is not really displaying precise numerical data, and can be closer to expressing categorical data (not done at all, part-way done, complete) depending upon resolution.

If you decide to follow this approach, then this link should help:

  • Code Project article showing how to change an icon, show pop-up text and a pop-up window using WPF.

If you still feel your use case merits overlaying text on an icon, then I think procedurally generating the icon is going to be the way to do it. I think you'll need to add the text to an image first, then convert it to an icon. These links should help you with that:

Ergwun
  • 12,579
  • 7
  • 56
  • 83
  • 1
    Why do you think generating icon on the fly with some text is bad idea? Still you didn't answer my question how to add text on icon. :( – iraSenthil May 11 '11 at 02:24
  • I think it is generally a bad idea because icons are best used to display categorical information rather than precise numerical data. I'll update my answer to clarify this, and add some hints for procedural icon creation if you still want to go ahead with it. – Ergwun May 11 '11 at 02:44
  • 3
    I think nothing is bad if it enhances user experience – iraSenthil May 11 '11 at 03:23
  • My point was that it very well may not enhance user experience, and/or there may be other features that would better enhance user experience. However, since I don't know the specifics of your scenario, maybe it is an appropriate solution for you. The links in my updated answer should help you anyway. – Ergwun May 11 '11 at 03:28
  • 1
    Thanks for the suggestions, IconLib is not helpful. I am going to look how to create Icon in WPF – iraSenthil May 11 '11 at 05:56