【发布时间】:2016-07-22 15:08:18
【问题描述】:
我试图在系统托盘中显示 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”的问题 - 图标周围有足够的空间,但它似乎无法使用。
有没有更好的方法来做到这一点?为什么windows可以做以下事情而我做不到?
更新:
感谢@NineBerry 提供了一个很好的解决方案。补充一点,我发现Tahoma 是最适合使用的字体。
【问题讨论】:
-
我希望其他应用程序只使用一组内置图标,而不是尝试即时生成它们
标签: c# .net system-tray notifyicon