【问题标题】:Writing text to the system tray instead of an icon将文本写入系统托盘而不是图标
【发布时间】: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


【解决方案1】:

这给了我一个很好看的两位数字符串的显示:

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);
}

我改变了什么:

  1. 使用更大的字体,但将 x 和 y 偏移量进一步向左和向上移动(-4、-2)。

  2. 在 Graphics 对象上设置 TextRenderingHint 以禁用抗锯齿。

绘制超过 2 个数字或字符似乎是不可能的。图标采用方形格式。任何超过两个字符的文本都意味着大大降低文本的高度。

您选择键盘布局 (ENG) 的示例实际上不是托盘区域中的通知图标,而是它自己的外壳工具栏。


展示 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);
}

进行以下更改:

  1. 使用非常窄的字体 Trebuchet MS。
  2. 使用单引号而不是点,因为它的两侧空间较小。
  3. 使用字体大小 10 并充分调整偏移量。

【讨论】:

  • 另外,我觉得“Tahoma”是这里最好的字体。
  • 您需要这行:DestroyIcon(hIcon) 以防止应用程序在大约 50 分钟后因内存泄漏而退出。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-11
  • 1970-01-01
  • 2016-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多