c# - 在 Windows 应用程序中的 notifyicon 图标上显示文本

标签 c# windows winforms windows-applications notifyicon

我正在创建一个 Windows 应用程序。在此应用程序中,我使用 notifyicon 并将我的应用程序最小化到系统托盘。在我点击按钮的代码中,我在后台处理一些东西并每 2 秒返回一个整数值。我需要在 Notifyicon 上显示值。

谁能帮帮我???

最佳答案

尝试 NotifyIcon.ShowBalloonTip方法:

Displays a balloon tip with the specified title, text, and icon in the taskbar for the specified time period.

void Form1_DoubleClick(object sender, EventArgs e)
{
    notifyIcon1.Visible = true;
    notifyIcon1.ShowBalloonTip(20000, "Information", "This is the text",
        ToolTipIcon.Info );
}

如果要更改托盘图标,请按需创建一个图标并将其设置为 NotifyIcon.Icon :

要创建图标,您可以使用以下代码(更新):

public static Icon GetIcon(string text)
{
    Bitmap bitmap = new Bitmap(32, 32);

    Icon icon = SmsSender.Properties.Resources.notifficationicon;
    System.Drawing.Font drawFont = new System.Drawing.Font("Calibri", 16, FontStyle.Bold);
    System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.White);

    System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap);

    graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
    graphics.DrawIcon(icon, 0, 0);            
    graphics.DrawString(text, drawFont, drawBrush, 1, 2);        
    Icon createdIcon = Icon.FromHandle(bitmap.GetHicon());

    drawFont.Dispose();
    drawBrush.Dispose();
    graphics.Dispose();
    bitmap.Dispose();

    return createdIcon;
} 

查看同一个项目:

关于c# - 在 Windows 应用程序中的 notifyicon 图标上显示文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12577749/

相关文章:

c# - Azure 是否有用于应用服务域和 SSL 测试的沙箱?

windows - 在Windows上运行docker镜像,结果为 “oci runtime error: exec: ” bash“:$ PATH中找不到可执行文件。”

python - 如何访问在我的 Windows 操作系统上的 Docker 工具箱 Linux 容器中创建的项目目录?

windows - 在没有安装 ActivePerl 的情况下在 Windows 上使用 perl 脚本?

.net - 复制/粘贴 Treeview 节点标签

.net - DataGridView、BindingList<T>、DataGridViewComboBoxColumn

c# - WinForms:如何检查 C# 文本框中的最少字符数?

c# - 屏幕传输

c# - 通过转换 (ChildClass)parentObject 调用子构造函数;跟踪修订

c# - 如何在 C# 中获取文件夹大小?