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.4) Go to your Application's Property Pages And Change Output Type to Windows Application.
- System
- System.Drawing
- System.Windows.Forms
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.Part 2: Adding Default Functions
- 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)
1) Open your Empty Code File and add the following
This gives us access to all the Windows System Functions we will need to create our application.Code:using System; using System.Drawing; using System.ComponentModel; using System.Windows.Forms;
2) Create our Name space for the Whole Project with the following
All Classes & Functions will be entered inside the Name space.Code:namespace SimpleIcon { }
3) Create the Class that the App will use
So now our total code should look like thisCode:private class MyApp { }
3) Now for the variables. All Code is Entered Inside the Class MyAppCode:using System; using System.Drawing; using System.ComponentModel; using System.Windows.Forms; namespace SimpleIcon { public class MyApp { } }
appIcon is the pointer that will our actual interfaceCode:private NotifyIcon appIcon = new NotifyIcon(); private ContextMenu sysTrayMenu = new ContextMenu(); private MenuItem MenuItem1 = new MenuItem("Say Hello"); private MenuItem MenuItem2 = new MenuItem("Exit");
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
Our Main function will actuall run our command "TrueApplicationStart"Code:public static void Main() { MyApp app = new MyApp(); app.TrueApplicationStart(); Application.Run(); }
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.
The First 3 Lines Identify our Icon to the system & what the user would see when they hold the mouse over the Icon.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 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"
MenuItem1_Click will just show a Simple System.Windows.Forms.Message Box that can show whatever text you wish (AKA Hello World)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(); }
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.
And should compile without any errors.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(); } } }
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.
It says:
Why?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
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks