Tuesday, 2 July 2013

How to Create a Notify Icon App using C#


All C# Developers will try to create a Application  like Notification in taskbar, This can be done easily Using the following Two Class.

  1.  NotifyIcon
  2.  ContextMenu
This Two class present in the Namespace  System.Windows.Forms;

NotifyIcon : Component that Creates a Icon in the Notification Area, This class cannot be inherited.

ContextMenu : This class will create a Context Menu in Notify Icon.

Let we Start the simple program with Steps.
  1. Create a Class Named "NotificationApp"
  2. Declare a Two Variables  one for "NotifyIcon" ,Other for "ContextMenu".
  3. Initialize the values for "Property" of NotifyIcon class and ContextMenu Class,and also Bind the 'Eventhandlers" for Events for that two class in the Constructor.
  4. Call the class in the Application Main Method.

/****************************************************************************/
Step 1 : Create a Class
             public sealed class NotificationApp


/****************************************************************************/
Step 2: Declare the variables

            private NotifyIcon notifyIcon;
            private ContextMenu notificationMenu;


/****************************************************************************/
Step 3: Initialize Values in Constructor of that Class 

             public NotificationApp()
            {
                  notifyIcon = new NotifyIcon();
                  notificationMenu = new ContextMenu(InitializeMenu());         
                  notifyIcon.DoubleClick += IconDoubleClick;           

                  /* Assign the Icon from System Icon  */
          notifyIcon.Icon = SystemIcons.Exclamation;
                 notifyIcon.ContextMenu = notificationMenu;
           }
         

           /* Add Menu item in ContextMenu */
           private MenuItem[] InitializeMenu()
           {
               MenuItem[] menu = new MenuItem[] {
                    new MenuItem("About", menuAboutClick),
                    new MenuItem("Exit", menuExitClick)
                                                                      };
               return menu;
           }
         

    /* Method that are bind in Events  , Add reference System.Windows.Forms */
        private void menuAboutClick(object sender, EventArgs e)
        {
            MessageBox.Show("About was Clicked");
        }
     
        private void menuExitClick(object sender, EventArgs e)
        {
            Application.Exit();
        }
     
        private void IconDoubleClick(object sender, EventArgs e)
        {
            MessageBox.Show("The icon was double clicked");
        }



/****************************************************************************/
Step 4: Call the class inside Main Method

         public static void Main(string[] args)
        {
             Application.EnableVisualStyles();
             Application.SetCompatibleTextRenderingDefault(false);
         
            bool isFirstInstance;

            // Please use a unique name for the mutex to prevent conflicts with other programs
            using (Mutex mtx = new Mutex(true"sampleNotify"out isFirstInstance)) 
            {
                if (isFirstInstance) 
                {
                    NotificationApp notificationIcon = new NotificationApp();
                    notificationIcon.notifyIcon.Visible = true;
                    Application.Run();
                    notificationIcon.notifyIcon.Dispose();
                } 
                else
                {
                    // The application is already running
                    // invoke the existing running instance
                }
            } // releases the Mutex
        }


/****************************************************************************/
Whole Class NotificationApp.Cs

 public sealed class NotificationApp
{
       private NotifyIcon notifyIcon;
       private ContextMenu notificationMenu;

            public NotificationApp()
            {
                  notifyIcon = new NotifyIcon();
                  notificationMenu = new ContextMenu(InitializeMenu());         
                  notifyIcon.DoubleClick += IconDoubleClick;           

                  /* Assign the Icon from System Icon  */
          notifyIcon.Icon = SystemIcons.Exclamation;
                 notifyIcon.ContextMenu = notificationMenu;
           }
         

           /* Add Menu item in ContextMenu */
           private MenuItem[] InitializeMenu()
           {
               MenuItem[] menu = new MenuItem[] {
                    new MenuItem("About", menuAboutClick),
                    new MenuItem("Exit", menuExitClick)
                                                                      };
               return menu;
           }
         
         private void menuAboutClick(object sender, EventArgs e)
        {
            MessageBox.Show("About was Clicked");
        }
     
        private void menuExitClick(object sender, EventArgs e)
        {
            Application.Exit();
        }
     
        private void IconDoubleClick(object sender, EventArgs e)
        {
            MessageBox.Show("The icon was double clicked");
        }

         public static void Main(string[] args)
        {
             Application.EnableVisualStyles();
             Application.SetCompatibleTextRenderingDefault(false);
         
            bool isFirstInstance;

            // Please use a unique name for the mutex to prevent conflicts with other programs
            using (Mutex mtx = new Mutex(true"sampleNotify"out isFirstInstance)) 
            {
                if (isFirstInstance) 
                {
                    NotificationApp notificationIcon = new NotificationApp();
                    notificationIcon.notifyIcon.Visible = true;
                    Application.Run();
                    notificationIcon.notifyIcon.Dispose();
                } 
                else
                {
                    // The application is already running
                    // invoke the existing running instance
                }
            } // releases the Mutex
        }
}


Enjoy, With this Basic Notification App. 

3 comments:

  1. hi Author i need small favour from u. i.e can we use the same class into the asp.net website means creating object of the class and do same with website.

    ReplyDelete
  2. can you elaborate the requirement, because it is designed for winforms Notify, for web Notify see the following link and use this library to notify the web datas http://dotnetvisio.blogspot.in/2014/11/create-angular-notification-service.html

    ReplyDelete
  3. Hi Rajesh,

    This above tray application is implemented for a WPF application. But could you help me find an example to create a tray application from a web application?

    It would be a great help.

    Thanks

    ReplyDelete