Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Create Icons at Runtime and Show Them in the System Tray

, 10 Jun 2008
Create dynamic icons at runtime and show them in the system tray
dynicon_demo.zip
DynIcon
DynIcon
DynIcon.suo
res
DynIcon.ico
icon1.ico
dynicon_demo_cs.zip
DynIconCS
DynIconCS
DynIconCS.suo
bin
Release
DynIconCS.exe
DynIconCS.pdb
DynIconCS.vshost.exe
DynIconCS.csproj.user
Properties
Settings.settings
Resources
Icon1.ico
Icon1.ico
dynicon_demo_mfc.zip
DynIcon.suo
DynIcon.ico
icon1.ico
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DynIconCS
{
    public partial class Form1 : Form
    {
        Icon m_Icon1;
        Icon m_Icon2;
        Icon m_IconDefault;

        String m_strTooltip;
        String m_strTooltipDefault;

        int m_nValue1;
        int m_nValue2;

       
        public Form1()
        {
            InitializeComponent();

            BnStart.Enabled = true;
            BnStop.Enabled = false;

            m_nValue1 = -25;
            m_nValue2 = 8;

            m_strTooltip = "";
            m_strTooltipDefault = "Temperature-Monitor (inactive)";

            //the default icon is a precreated icon from the resources
            m_IconDefault = DynIconCS.Properties.Resources.Icon1;

            //at first just show the default icon in the system tray
            notifyIcon1.Icon = m_IconDefault;
            notifyIcon1.Text = m_strTooltipDefault;
            notifyIcon1.Visible = true;

        }

        private void BnStart_Click(object sender, EventArgs e)
        {
            //start the timer to change the values
            timer1.Start();

            BnStart.Enabled = false;
            BnStop.Enabled = true;

            //clear the default icon
            notifyIcon1.Visible = false;

            //create new icons
            CreateIcons();
            PushIcons();
        }

        private void BnStop_Click(object sender, EventArgs e)
        {
            //stop the timer
            timer1.Stop();

            BnStart.Enabled = true;
            BnStop.Enabled = false;

            //in the stopmode the default icon is shown
            notifyIcon1.Icon = m_IconDefault;
            notifyIcon1.Text = m_strTooltipDefault;

            //clear the second icon
            notifyIcon2.Visible = false;

        }

        private void BnEnd_Click(object sender, EventArgs e)
        {
            if (timer1.Enabled) timer1.Stop();

            this.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            ChangeValues();
            CreateIcons();
            PushIcons();
        }


        private void ChangeValues()
        {
            m_nValue1++;
            if (m_nValue1 > 50)
                m_nValue1 = -65;
            
            m_nValue2++;
            if (m_nValue2 > 10)
                m_nValue2 = -15;
            
            //show the new values in the windows form
            Value1.Text = m_nValue1.ToString();
            Value2.Text = m_nValue2.ToString();
        }


        private void CreateIcons()
        {
            CreateIcon(1);
            CreateIcon(2);
        }

        private void CreateIcon(int nIcon)
        {
            String str;

            if (nIcon == 1)
                str = String.Format("{0,3}", m_nValue1);
            else
                str = String.Format("{0,3}", m_nValue2);
            

            //white pen to draw some lines in the icon
            Pen pen = new Pen(Color.White);

            //white brush for the text
            SolidBrush brush = new SolidBrush(Color.White);

            //create a small font to write the values in the bitmap
            Font font = new Font("Tahoma", 8);

            //origin for the string
            PointF origin = new PointF(-1, 1);

            //create a new bitmap with 16x16 pixel
            Bitmap bitmap = new Bitmap(16, 16);

            //use the bitmap to draw
            Graphics graph = Graphics.FromImage(bitmap);

            //draw two horizontale lines
            graph.DrawLine(pen, 0, 15, 15, 15);
            graph.DrawLine(pen, 0, 0, 15, 0);

            //draw the string including the value
            graph.DrawString(str, font, brush, origin);


            //get an Hicon from the bitmap
            IntPtr Hicon = bitmap.GetHicon();

            //create a new icon from the handle
            if (nIcon == 1)
                m_Icon1 = Icon.FromHandle(Hicon);
            else
                m_Icon2 = Icon.FromHandle(Hicon);

            //clean up
            pen.Dispose();
            brush.Dispose();
            font.Dispose();
            bitmap.Dispose();
            graph.Dispose();
        }


        private void PushIcons()
        {
            //create a new tooltip with the recent values
            m_strTooltip = String.Format("Value 1:{0,3}\nValue 2:{1,3}", m_nValue1, m_nValue2);

            //push the icons to the system tray
            notifyIcon2.Icon = m_Icon2;
            notifyIcon2.Text = m_strTooltip;
            if (!notifyIcon2.Visible) notifyIcon2.Visible = true;

            notifyIcon1.Icon = m_Icon1;
            notifyIcon1.Text = m_strTooltip;
            if (!notifyIcon1.Visible) notifyIcon1.Visible = true;
        }



        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            //doubleclicking the icon will change the windowstate
            ChangeWindowState();
        }

        private void notifyIcon2_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            //doubleclicking the icon will change the windowstate
            ChangeWindowState();
        }

        private void ChangeWindowState()
        {
            //if the window is visible it is going to be hidden and vice versa
            if (this.Visible == true)
            {
                this.Hide();
            }
            else
            {
                this.Show();
                this.WindowState = FormWindowState.Normal;
            }

        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            //if the window is minimized, it should be removed from the taskbar
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
            }
        }



    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Holger Lange

Germany Germany
No Biography provided

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 10 Jun 2008
Article Copyright 2007 by Holger Lange
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid