Search code examples
c#winformswindowsnotifyicon

Display Text over notifyicon icon in windows application


I'm creating a windows application. In this application I'm using notifyicon and minimizing my application to system tray. In my code on button click, I process some thing in background and returns a integer value every 2 seconds. I need to display the Value over the Notifyicon .

Can anyone help me???


Solution

  • Try NotifyIcon.ShowBalloonTip method:

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

    If you want to change the tray icon, create an icon ondemand and set it to NotifyIcon.Icon:

    to create icon you can use this codes (Updated):

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

    see this same project: