在Windows系统托盘通知图标上显示数字

3
我正在尝试创建一个基于通知图标的应用程序,想要在其中显示从1到999的一些数字。
我查看了这个视频,它与我的需求类似,但是在这里,系统托盘图标只显示图标,并弹出一个框,而不是在系统托盘图标上显示数字或任何文本。
除了弹出项之外,我想做的就是读取一个数字(从某个地方输入),并将该数字显示在通知图标部分。
我愿意尝试任何技术(QT、.net)来完成这个任务。基本上,我正在寻找一些示例。

这真的可能吗? - Ankush Ved Mitra Pandit
3个回答

3
虽然你的问题有点含糊,但这是完全可能的,甚至可以说非常简单。由于你提到你愿意尝试任何技术,C# 可能会为你简化事情。
  • 生成一个新的 16 x 16 的 Bitmap 并使用 Graphics 类将数字绘制到其中。
  • Image 实例转换为 Icon 实例,在释放你的 Graphics 对象后进行。
  • 将你刚创建的图标设置为你的 NotifyIconIcon 属性。
这些是基本步骤。如果你对使用的类不熟悉,可能需要进行一些研究。

2
感谢您回答我的问题。以下是我想到的内容。不确定这是否是您所说的内容。
Bitmap bmp = new Bitmap(WindowsFormsApplication2.Properties.Resources._16by16BitmapIcon); RectangleF rectf = new RectangleF(2, 2, 16, 16); Graphics g = Graphics.FromImage(bmp); g.DrawString("99", new Font("Tahoma", 7), Brushes.Blue, rectf); pictureBox1.Image = bmp; pictureBox1.Height = bmp.Height; pictureBox1.Width = bmp.Width; g.Dispose(); var thumb = (Bitmap)bmp.GetThumbnailImage(64, 64, null, IntPtr.Zero); thumb.MakeTransparent(); notifyIcon1.Icon = Icon.FromHandle(thumb.GetHicon());
现在,我的下一个问题是,是否有更好的方法来完成这项任务?这是我第一个C Sharp应用程序,因此欢迎任何建议!

1
你的代码看起来很好,但是除非它们真正是答案,否则你不应该在问题中发布答案。 - Luke Joshua Park

0
public void ShowText(string text, Font font, Color col)
{
    Brush brush = new SolidBrush(col);

    // Create a bitmap and draw text on it
    Bitmap bitmap = new Bitmap(16, 16);
    Graphics graphics = Graphics.FromImage(bitmap);
    graphics.DrawString(text, font, brush, 0, 0);

    // Convert the bitmap with text to an Icon
    Icon icon = Icon.FromHandle(bitmap.GetHicon());

    m_notifyIcon.Icon = icon;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接