C# 将文本而不是图标写入系统托盘
我尝试在系统托盘中显示2-3个可更新字符,而不是显示.ico文件-类似于CoreTemp在系统中显示温度时所做的尝试: 我正在WinForms应用程序中使用Notify图标以及以下代码:C# 将文本而不是图标写入系统托盘,c#,.net,system-tray,notifyicon,C#,.net,System Tray,Notifyicon,我尝试在系统托盘中显示2-3个可更新字符,而不是显示.ico文件-类似于CoreTemp在系统中显示温度时所做的尝试: 我正在WinForms应用程序中使用Notify图标以及以下代码: Font fontToUse = new Font("Microsoft Sans Serif", 8, FontStyle.Regular, GraphicsUnit.Pixel); Brush brushToUse = new SolidBrush(Color.White); Bitmap bitmapT
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
是最适合使用的字体。这给了我一个非常好看的两位数字符串显示:
我改变的是:
我所能达到的最佳效果是显示8.55: 作出以下更改:
我希望其他应用程序只使用一组内置图标,而不是尝试在FlyAlway上生成它们,只是说我发现“Tahoma”是这里使用的最佳字体。你需要一行:DestroyIcon(hIcon),以防止应用程序在大约50分钟后由于内存泄漏而退出。
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);
}
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);
}