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开发者_如何学JAVA for GDI+ also would be grateful.
Thank you.
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;
}
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:
- Turn on the Code C# snippet for adding text to an image
- Code Prject article on creating an .ico file on the fly - very comprehensive, and probably a bit overkill for your needs, but you can pull out the code for your use.
精彩评论