Text overlay on system tray instead of icon

I am trying to display 2-3 updated symbols in the system tray, and not display the .ico file, similar to what CoreTemp does when displaying the temperature in the system:

enter image description here

I am using 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); } 

Unfortunately, this leads to a bad result, which is not like what CoreTemp gets:

enter image description here

You think that the solution will be to increase the font size, but something of size 8 does not fit inside the image. Increasing a raster image from 16x16 to 32x32 does nothing - it is reduced.

Then the problem arises when I want to display "8.55" instead of "55" - there is enough space around the icon, but it looks unusable.

enter image description here

Is there a better way to do this? Why windows can do the following, but I can’t?

enter image description here

Update:

Thanks for @NineBerry for the good solution. To add, I find Tahoma best font to use.

+8
c # system-tray notifyicon
source share
1 answer

This gives me a nice two-digit string display:

enter image description here

 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 left and up (-4, -2).

  • Set TextRenderingHint to the Graphics object to disable anti-aliasing.

It seems impossible to draw more than two numbers or characters. The icons are in square format. Any text longer than two characters means a significant reduction in text height.

The sample in which you select the keyboard layout (ENG) is not really a notification icon in the tray area, but its own shell toolbar.


The best I could achieve was to display 8.55:

enter image description here

 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:

  • Use Trebuchet MS, which is a very narrow font.
  • Use a single quotation mark instead of a dot because it has less space on the sides.
  • Use a font size of 10 and adapt the offsets accordingly.
+10
source share

All Articles