将文本而不是图标写入系统托盘
Writing text to the system tray instead of an icon
我正在尝试在系统托盘中显示 2-3 个可更新字符,而不是显示 .ico 文件 - 类似于 CoreTemp 在系统中显示温度时所做的尝试:
我在我的 WinForms 应用程序中使用 NotifyIcon 以及以下代码:
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);
}
遗憾的是,这产生了一个糟糕的结果,与 CoreTemp 得到的结果完全不同:
您可能认为解决方案是增加字体大小,但超过 8 号的任何内容都不适合图像。将位图从 16x16 增加到 32x32 也无济于事 - 它会缩小尺寸。
然后我想显示“8.55”而不是“55”的问题 - 图标周围有足够的 space 但它似乎无法使用。
有更好的方法吗?为什么windows可以做下面的事而我做不到?
更新:
感谢@NineBerry 提供了很好的解决方案。另外,我发现 Tahoma
是最好用的字体。
这给了我一个非常漂亮的两位数字符串显示:
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);
}
我改变了什么:
使用更大的字体大小,但将 x 和 y 偏移进一步向左和顶部移动 (-4, -2)。
在 Graphics 对象上设置 TextRenderingHint 以禁用抗锯齿。
似乎无法绘制超过 2 个数字或字符。图标采用正方形格式。任何超过两个字符的文本都意味着大大降低文本的高度。
您 select 键盘布局 (ENG) 的示例实际上不是托盘区域中的通知图标,而是它自己的 shell 工具栏。
显示 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);
}
具有以下更改:
- 使用非常窄的 Trebuchet MS 字体。
- 使用单引号而不是点,因为它两边的 space 较少。
- 使用 10 号字体并适当调整偏移量。
我正在尝试在系统托盘中显示 2-3 个可更新字符,而不是显示 .ico 文件 - 类似于 CoreTemp 在系统中显示温度时所做的尝试:
我在我的 WinForms 应用程序中使用 NotifyIcon 以及以下代码:
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);
}
遗憾的是,这产生了一个糟糕的结果,与 CoreTemp 得到的结果完全不同:
您可能认为解决方案是增加字体大小,但超过 8 号的任何内容都不适合图像。将位图从 16x16 增加到 32x32 也无济于事 - 它会缩小尺寸。
然后我想显示“8.55”而不是“55”的问题 - 图标周围有足够的 space 但它似乎无法使用。
有更好的方法吗?为什么windows可以做下面的事而我做不到?
更新:
感谢@NineBerry 提供了很好的解决方案。另外,我发现 Tahoma
是最好用的字体。
这给了我一个非常漂亮的两位数字符串显示:
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);
}
我改变了什么:
使用更大的字体大小,但将 x 和 y 偏移进一步向左和顶部移动 (-4, -2)。
在 Graphics 对象上设置 TextRenderingHint 以禁用抗锯齿。
似乎无法绘制超过 2 个数字或字符。图标采用正方形格式。任何超过两个字符的文本都意味着大大降低文本的高度。
您 select 键盘布局 (ENG) 的示例实际上不是托盘区域中的通知图标,而是它自己的 shell 工具栏。
显示 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);
}
具有以下更改:
- 使用非常窄的 Trebuchet MS 字体。
- 使用单引号而不是点,因为它两边的 space 较少。
- 使用 10 号字体并适当调整偏移量。