【问题标题】:Display Text over notifyicon icon in windows application在 Windows 应用程序中的 notifyicon 图标上显示文本
【发布时间】:2012-09-25 07:01:32
【问题描述】:

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

谁能帮帮我???

【问题讨论】:

    标签: c# windows winforms windows-applications notifyicon


    【解决方案1】:

    试试NotifyIcon.ShowBalloonTip方法:

    在指定时间段的任务栏中显示带有指定标题、文本和图标的气球提示。

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

    查看同一个项目:

    【讨论】:

    • 我已经在使用它,但问题是如果我使用它,我必须每秒显示一次气球。我认为最好在通知图标上显示相同(不喜欢 34)
    • 在这一行 Icon icon = new Icon(); 它说,'System.Drawing.Icon' Does not contain a Constructor that takes 0 arguments
    • 我将图标文件添加到资源文件夹并完成了此操作。 Icon icon = ProjectName.Properties.Resources.notifficationicon;
    【解决方案2】:

    试试这个。希望这对您有所帮助。

    http://www.dotnetperls.com/notifyicon

    http://www.codeproject.com/Articles/37451/Display-Progress-and-Overlay-Icons-for-Multiple-Vi

    你最多可以做这样的事情。

    Graphics canvas;
    Bitmap iconBitmap = new Bitmap(16, 16);
    canvas = Graphics.FromImage(iconBitmap);
    
    canvas.DrawIcon(YourProject.Resources.YourIcon, 0, 0);
    
    StringFormat format = new StringFormat();
    format.Alignment = StringAlignment.Center;
    
    canvas.DrawString(
        "2",
        new Font("Calibri", 8, FontStyle.Bold),
        new SolidBrush(Color.FromArgb(40, 40, 40)),
        new RectangleF(0, 3, 16, 13),
        format
    );
    
    notifyIcon.Icon = Icon.FromHandle(iconBitmap.GetHicon());
    

    【讨论】:

    • 我已经看到了,但是每秒执行这个总代码将是程序的负担
    • 不,我使用的是 windows xp,但最终的应用程序可能会在 win 7 中使用
    • 试试我上面的代码。将 DrawString("2".. 替换为您的值,例如 34。它将适用于 xp 和 7
    • 如何调整图标文件的大小??图像文件正在裁剪。我需要自动调整大小吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    • 2016-05-25
    • 2014-02-13
    • 2022-01-17
    • 2023-04-10
    • 2012-05-14
    相关资源
    最近更新 更多