I am trying to display 2-3 updatable characters in the system tray rather than display an .ico file - similar to what CoreTemp does when they display the temperature in the system try:
I am using a NotifyIcon in my WinForms application along with the following code:
Font fontToUse = new Font("Microsoft Sans Serif", 8, FontStyle.Regular, GraphicsUnit.Pixel);
Brush brushToUse = new SolidBrush(Color.White);
Bitmap bitmapText = new Bitmap(16, 16);
Graphics g = Drawing.Graphics.FromImage(bitmapText);
IntPtr hIcon;
public void CreateTextIcon(string str)
{
g.Clear(Color.Transparent);
g.DrawString(str, fontToUse, brushToUse, -2, 5);
hIcon = (bitmapText.GetHicon);
NotifyIcon1.Icon = Drawing.Icon.FromHandle(hIcon);
DestroyIcon(hIcon.ToInt32);
}
Sadly this produces a poor result nothing like what CoreTemp gets:
You'd think the solution would be to increase the font size, but anything over size 8 doesn't fit inside the image. Increasing the bitmap from 16x16 to 32x32 does nothing either - it gets resized down.
Then there's the problem of me wanting to display "8.55" instead of just "55" - there's enough space around the icon but it appears unusable.
Is there a better way to do this? Why can windows do the following but I cannot?
Update:
Thanks for @NineBerry for a good solution. To add, I find Tahoma
to be the best font to use.
Click and hold an icon from the expanded System Tray. Drag the icon into the standard System Tray. Release the left mouse button. If you don't like the positioning of the icon, you can click and drag it left or right to position it where you want it in the tray.
To pin apps to the taskbarSelect Start , scroll to the app you want to pin, then press and hold (or right-click) the app. Select More > Pin to taskbar. If the app is already open on the desktop, press and hold (or right click) the app's taskbar icon, and then select Pin to taskbar.
This gives me a quite good looking display of a two digit string:
private void button1_Click(object sender, EventArgs e)
{
CreateTextIcon("89");
}
public void CreateTextIcon(string str)
{
Font fontToUse = new Font("Microsoft Sans Serif", 16, FontStyle.Regular, GraphicsUnit.Pixel);
Brush brushToUse = new SolidBrush(Color.White);
Bitmap bitmapText = new Bitmap(16, 16);
Graphics g = System.Drawing.Graphics.FromImage(bitmapText);
IntPtr hIcon;
g.Clear(Color.Transparent);
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
g.DrawString(str, fontToUse, brushToUse, -4, -2);
hIcon = (bitmapText.GetHicon());
notifyIcon1.Icon = System.Drawing.Icon.FromHandle(hIcon);
//DestroyIcon(hIcon.ToInt32);
}
What I changed:
Use a larger font size, but move the x and y offset further to the left and top (-4, -2).
Set TextRenderingHint on the Graphics object to disable anti-aliasing.
It seems impossible to draw more than 2 digits or characters. The icons have a square format. Any text longer than two characters would mean reducing the height of the text a lot.
The sample where you select the keyboard layout (ENG) is actually not a notification icon in the tray area but its very own shell toolbar.
The best I could achieve to display 8.55:
private void button1_Click(object sender, EventArgs e)
{
CreateTextIcon("8'55");
}
public void CreateTextIcon(string str)
{
Font fontToUse = new Font("Trebuchet MS", 10, FontStyle.Regular, GraphicsUnit.Pixel);
Brush brushToUse = new SolidBrush(Color.White);
Bitmap bitmapText = new Bitmap(16, 16);
Graphics g = System.Drawing.Graphics.FromImage(bitmapText);
IntPtr hIcon;
g.Clear(Color.Transparent);
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
g.DrawString(str, fontToUse, brushToUse, -2, 0);
hIcon = (bitmapText.GetHicon());
notifyIcon1.Icon = System.Drawing.Icon.FromHandle(hIcon);
//DestroyIcon(hIcon.ToInt32);
}
with the following changes:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With