Display Text over notifyicon icon in windows application
Asked Answered
W

2

5

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???

Whortleberry answered 25/9, 2012 at 7:1 Comment(0)
C
11

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:

Cohlier answered 25/9, 2012 at 7:9 Comment(3)
I'm already using that but the problem is I have to show the balloon every second if I use this. i thought it would be better to show the same (No like 34) over the notification iconWhortleberry
In this line Icon icon = new Icon(); it says , 'System.Drawing.Icon' Does not contain a Constructor that takes 0 argumentsWhortleberry
I added the icon file into Resource folder and done this. Icon icon = ProjectName.Properties.Resources.notifficationicon;Whortleberry
B
3

Try this one. Hopefully this helps you.

http://www.dotnetperls.com/notifyicon

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

And the most you can do some thing like this.

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());
Bailly answered 25/9, 2012 at 7:14 Comment(5)
I have already seen this but executing this total code every second will be a burden to the programWhortleberry
no i'm using windows xp but the final application might be used in win 7Whortleberry
try the code i have put above. replace the DrawString("2".. with your value like 34. it will work on both xp and 7Bailly
msdn.microsoft.com/en-us/library/3bka19x4%28v=vs.80%29.aspx see the add existing resource.Bailly
How to resze the icon file ?? the image file is cropping. i need to autofit the size?Whortleberry

© 2022 - 2024 — McMap. All rights reserved.