+ Reply to Thread
Results 1 to 3 of 3

Thread: Tutorial: Simple Notification Tray Icon Part 1

  1. #1
    PGP_Protector's Avatar
    PGP_Protector is offline Programming Professional
    Join Date
    Jun 2009
    Posts
    253
    Rep Power
    11

    Tutorial: Simple Notification Tray Icon Part 1

    This Short Tutorial Will Come in Two Parts.
    Part One will show how to create a Simple Application that

    1) Runs without a Master Form
    2) Runs in the Notify Icon Area of the Task Bar
    3) Allows you To Exit The Application

    Part Two will Show how to Add More Custom Forms To the Icon Menu & Expand it.


    How To Create A Notify Icon Application
    Part 1: Preparing The Project.

    1) We are going to start by Creating a New Empty Project Called "SimpleIcon"
    2) Add A CodeFile Called OurApp.CS
    3) Add The Following References To The Empty Project.
    • System
    • System.Drawing
    • System.Windows.Forms
    4) Go to your Application's Property Pages And Change Output Type to Windows Application.
    5) Click on Resources and Create a Default resources File
    6) Add Resource -> Add New Icon called "MyIcon"

    At This Point We Should Now Have an Project With the Following In Our Solution Explorer.
    • OurApp.cs (or Whatever Name You Gave your CodeFile in Step #2)
    • References (System, System.Drawing, And System.Windows.Forms) (Added On Step #3)
    • Properties -> Resources.resx -> Resource.Designer.cs (Auto Created on Step #5)
    • Resources MyIcon.ico (Or What Ever Name You Gave the Icon File in Step #6)
    Part 2: Adding Default Functions

    1) Open your Empty Code File and add the following
    Code:
    using System;
    using System.Drawing;
    using System.ComponentModel;
    using System.Windows.Forms;
    This gives us access to all the Windows System Functions we will need to create our application.

    2) Create our Name space for the Whole Project with the following
    Code:
    namespace SimpleIcon
    {
    }
    All Classes & Functions will be entered inside the Name space.

    3) Create the Class that the App will use
    Code:
    private class MyApp
    {
    }
    So now our total code should look like this
    Code:
    using System;
    using System.Drawing;
    using System.ComponentModel;
    using System.Windows.Forms;
    
    namespace SimpleIcon
    {
        public class MyApp
        {
        }
    }
    3) Now for the variables. All Code is Entered Inside the Class MyApp
    Code:
     
    private NotifyIcon appIcon = new NotifyIcon();
    private ContextMenu sysTrayMenu = new ContextMenu();
    private MenuItem MenuItem1 = new MenuItem("Say Hello");
    private MenuItem MenuItem2 = new MenuItem("Exit");
    appIcon is the pointer that will our actual interface
    ssTrayMenu is used to add multiple pop up menus for the user interface
    MenuItem1,MenuItem2 Will Show The Text that is Displayed on the Icon when the user Right Clicks on the Icon in the System Tray.

    4) Our Main Function as without this, nothing will ever happen
    Code:
           public static void Main()
            {
                MyApp app = new MyApp();
                app.TrueApplicationStart();
                Application.Run();
            }
    Our Main function will actuall run our command "TrueApplicationStart"
    This is the simplest way to allow the application to stay running, otherwise when the application creates the icon & finishes it, it would automatically exit right after if created it.

    5) Now The True Application Start up Code.
    Code:
            public void TrueApplicationStart()
            {
                Icon ico = SimpleIcon.Properties.Resources.MyIcon;
                appIcon.Icon = ico;
                appIcon.Text = "System Tray Application";
    
                sysTrayMenu.MenuItems.Add(MenuItem1);
                sysTrayMenu.MenuItems.Add(MenuItem2);
                appIcon.ContextMenu = sysTrayMenu;
    
                appIcon.Visible = true;
    
                MenuItem1.Click += new EventHandler(MenuItem1_Click);
                MenuItem2.Click += new EventHandler(MenuItem2_Click);
            }
    The First 3 Lines Identify our Icon to the system & what the user would see when they hold the mouse over the Icon.

    The Following 3 Lines Add our Menu Items Created in Step 3 To the ICON and create the Context Menu.

    After That we Enable The Icon By Turning it visible

    And the Final Two Lines Create the Event Handlers for Each Menu Item You Add to The Project.
    (If you have auto complete turn on in your settings, after typing "MenuItem1.Click +=" you can press the tab key twice to create the new Event Handler & the base code for it.

    6) Code for the Event Handlers for our Menu Items "Say Hello" and "Exit"
    Code:
            private void MenuItem1_Click(object sender, System.EventArgs e)
            {
                MessageBox.Show("Hello ColdCall");
            }
    
            private void MenuItem2_Click(object sender, System.EventArgs e)
            {
                appIcon.Visible = false;
                Application.Exit();
            }
    MenuItem1_Click will just show a Simple System.Windows.Forms.Message Box that can show whatever text you wish (AKA Hello World)
    MenuItem2_Click allows us to exit & close the Thread allowing the application to actually Exit.

    We Turn the appIcon.Visible = false before exiting as sometimes windows will not clean up the TaskBar tray if you just exit the application untill you move the mouse over the now defunct icon.

    Summery

    If you followed it thus far your total code should be as follows.
    Code:
    using System;
    using System.Drawing;
    using System.ComponentModel;
    using System.Windows.Forms;
    
    namespace SimpleIcon
    {
        public class MyApp
        {
            private NotifyIcon appIcon = new NotifyIcon();
            private ContextMenu sysTrayMenu = new ContextMenu();
            private MenuItem MenuItem1 = new MenuItem("Say Hello");
            private MenuItem MenuItem2 = new MenuItem("Exit");
    
            public static void Main()
            {
                MyApp app = new MyApp();
                app.TrueApplicationStart();
                Application.Run();
            }
    
            public void TrueApplicationStart()
            {
                Icon ico = SimpleIcon.Properties.Resources.MyIcon;
                appIcon.Icon = ico;
                appIcon.Text = "System Tray Application";
    
                sysTrayMenu.MenuItems.Add(MenuItem1);
                sysTrayMenu.MenuItems.Add(MenuItem2);
                appIcon.ContextMenu = sysTrayMenu;
    
                appIcon.Visible = true;
    
                MenuItem1.Click += new EventHandler(MenuItem1_Click);
                MenuItem2.Click += new EventHandler(MenuItem2_Click);
                MenuItem1.Click +=new EventHandler(MenuItem1_Click);
            }
    
            private void MenuItem1_Click(object sender, System.EventArgs e)
            {
                MessageBox.Show("Hello ColdCall");
            }
    
            private void MenuItem2_Click(object sender, System.EventArgs e)
            {
                appIcon.Visible = false;
                Application.Exit();
            }
    
        }
    }
    And should compile without any errors.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Nov 2009
    Location
    I seem to be lost...
    Posts
    1,566
    Blog Entries
    1
    Rep Power
    22

    Re: Tutorial: Simple Notification Tray Icon Part 1

    Thanks I've been wondering how I could do something like this. +rep
    Knowledge: Intermediate C#, Beginner AS3, HTML, CSS, Binary, Hex, Octal.
    Science is only an educated theory, which we cannot disprove.

  4. #3
    fyhring4 is offline Newbie
    Join Date
    Jan 2009
    Location
    Denmark
    Posts
    13
    Rep Power
    0

    Re: Tutorial: Simple Notification Tray Icon Part 1

    It says:
    Code:
    Error	1	The type or namespace name 'Properties' does not exist in the namespace 'SimpleIcon' (are you missing an assembly reference?)	C:\Users\Fyhring4\AppData\Local\Temporary Projects\Project1\OurApp.cs	24	35	Project1
    Why?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. The XNA Tutorial - Part 1
    By semprance in forum CSharp Tutorials
    Replies: 5
    Last Post: 09-25-2010, 06:22 AM
  2. Replies: 3
    Last Post: 08-24-2010, 07:43 PM
  3. The XNA Tutorial - Part 2
    By semprance in forum Game Design Tutorials
    Replies: 0
    Last Post: 04-07-2010, 07:52 AM
  4. The XNA Tutorial - Part 1
    By semprance in forum Game Design Tutorials
    Replies: 0
    Last Post: 03-30-2010, 08:29 AM
  5. simple discussion forum part 2 - tutorial
    By omar_a_k in forum PHP Tutorials
    Replies: 2
    Last Post: 08-11-2009, 01:47 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts