Use C # to write an icon program to dock on the taskbar _c# tutorial

Source: Internet
Author: User
Write an icon program that is docked on the taskbar in C #
Author: Cai Shiyu Category: c#/vb Date: 2002-1-30 10:21:46
01-12-6 10:53:11
--------------------------------------------------------------------------------
Introduction
The C # language is a Microsoft company target. NET platform to launch a new language, as. NET platform, which concentrates almost all the latest results on software development and software engineering research. It is the first fully component-oriented language.
Microsoft's. NET platform is a new generation of Internet platform. For technical developers,. NET platform is very remarkable, first of all, it provides common language runtime, common language running platform, on the other hand, it is a large and comprehensive unified programming class.
Let me introduce you to one. NET platform in C # written on the taskbar can be parked in the icon program, similar to SQL Manager, anti-virus software icon and Oicq icon.
First, the basic concept of icon program
The icon program we refer to here is similar to the programs that are frequently contacted in Windows, which are parked on the taskbar, running in the background, and SQL Service Manager. These programs start to form an icon that stops on the taskbar and has the following appearance features:
Double-click the icon to open the Program main window.
Displays a message when the mouse stops on the icon.
Click the right mouse button on the icon to pop-up a shortcut menu, by clicking on the menu item on the shortcut menu can run the corresponding program function.
Introduction to the Application class of the program
. NET provides a very large number of classes, we only need to use the various classes provided in the development process to achieve most of our functionality. NET platform, some of the classes are encapsulated, some virtual, most of them are general public classes. You can construct instances of these public classes directly, or you can use the full functionality of the class through inheritance. In this program, we use a lot. NET class, below, I will be the main class for a brief introduction!
System.Windows.Fomrs.NotifyIcon class
This is the most important class in this program, it inherits from the component class of a package class, not inheritable, the main function is to produce a can be parked in the taskbar rightmost icon program. The following is an introduction to its main members:
Constructor (constructor function)
public NotifyIcon (); Constructs a NotifyIcon object directly without any arguments.
Public NotifyIcon (IContainer container); Takes a IContainer parameter that represents the NotifyIcon container control.
Attributes (properties)
ContextMenu the pop-up menu for an object.
Icons for Icon objects
The ToolTip text of the text object, which is the text that is displayed when the mouse hovers over the object.
Visible Indicates whether the object is visible.
Event (events)
Clcik is raised when the object icon is clicked.
DoubleClick is raised when the object icon is double-clicked.
System.Windows.Forms.Application class
The Windows Forms application launches classes, which provide a way to manage applications, such as starting Windows Forms using Application.Run (Form), and exiting programs using Application.exit ().
System.Windows.Forms.Form class
Windows Forms program Form classes, Windows program forms are typically generated directly or indirectly from the class, such as creating a simple form using new forms (). In a generic application, there are more visual elements and controls, such as buttons, labels, text boxes, and so on, that are added to the user's form, so most of us use the form class to derive the user's own form class from the class.
System.Windows.Forms.Button class
Button control class, which provides a visual button element, which is normally used by setting the display text of the button through the Text property and then adding the Click event Response function to the button.
System.Windows.Forms.Label class
A text label class that displays general text information on a form by setting its Text property.
System.Windows.Forms.MenuItem class
The Windows Forms menu item class, which generates a simple menu item that is the basic constituent element of all menus, allows you to set the appearance properties and event response methods for menu items. This class provides a variety of constructors that generate menu item objects, and in practice, MenuItem (string text) is typically used to generate a menu item that displays text as a literal. The menu item class provides a Click event that allows the user to respond when clicked on the menu.
System.Windows.Forms.ContextMenu class
pop-up menu class to generate a pop-up menu object. It includes a menu item collection property that makes it easy to manage and access the items in the pop-up menu. There are two ways to generate a pop-up menu object using its constructor: one is to use ContextMenu () directly, without any arguments. The other is using ContextMenu (menuitem[] menus) and substituting a menu item array as a parameter.
Third, the program design and main code
Programming
This program uses visual Studio. NET writing, you first build a Windows Forms initial form by creating a new Windows Application project, adding a button with the text "hidden form" on the form, a label for the demo program, and double-clicking the button to add the hidden window break code in its Click event Response method. Then locate the NotifyIcon control in the toolbar panel and drag it onto the form, and then set its Text property and Icon property (the Icon property is a picture, which you can select from the control's property bar). Double-click the form to add the initialization code for the NotifyIcon class to the form's Load event response method, which can be compiled and run directly. As shown in the following illustration:
Program main code and annotation
The main code and detailed comments in this program are listed below for a deeper understanding of the program.
public class Form1:System.Windows.Forms.Form
{
Define a NotifyIcon Object
Private System.Windows.Forms.NotifyIcon NotifyIcon1;
private void InitializeComponent ()//visual Studio. NET automatically generated and executed when the form is initialized
{
Create a NotifyIcon object and set its basic properties such as Icon,text and visible
This.notifyicon1 = new System.Windows.Forms.NotifyIcon (this.components);
This.notifyIcon1.Icon = ((System.Drawing.Icon) (resources. GetObject ("Notifyicon1.icon"));
This.notifyIcon1.Text = "This is my test procedure, also cool bar!" ";
This.notifyIcon1.Visible = true;
}
static void Main ()
{
Application.Run (New Form1 ());//Start Run Program
}
private void Form1_Load (object sender, System.EventArgs e)
{
Generates 4 menu item objects, showing the text as Display window, hidden window, execute program, exit program
MenuItem menuitem1=new MenuItem ("Display window");
MenuItem menuitem2=new MenuItem ("hidden Window");
MenuItem menuitem3=new MenuItem ("Executive procedure");
MenuItem menuitem4=new MenuItem ("Exit procedure");
Add the Click event Response function for 4 menu items respectively
Menuitem1.click+=new System.EventHandler (This.menuitem1_click);
Menuitem2.click+=new System.EventHandler (This.menuitem2_click);
Menuitem3.click+=new System.EventHandler (This.menuitem3_click);
Menuitem4.click+=new System.EventHandler (This.menuitem4_click);
Set the ContextMenu property of the NotifyIcon object to the Dough pop-up menu object
Notifyicon1.contextmenu=new ContextMenu (New menuitem[]{menuitem1,menuitem2,menuitem3,menuitem4});
The corresponding function is executed when the user double-clicks the program icon
Notifyicon1.doubleclick+=new System.EventHandler (This.notifyicon_dbclick);
}
The response method for the private void Menuitem1_click (Object Sender,system.eventargs e)//Display Window menu
{
if (this. Visible==false) this. visible=true;//Display the current window if the current window does not appear
}
The response method for the private void Menuitem2_click (Object Sender,system.eventargs e)//Hidden Window menu
{
if (this. Visible==true) this. visible=false;//Hide a window if the current window is displayed
}
The response method for the private void Menuitem3_click (Object Sender,system.eventargs E)/"Execute Program" menu
{
Displays a message box indicating that the event has been responded to
MessageBox.Show ("demo program has been executed, here is the function is to display a prompt box!") "," Cue Information ", messageboxbuttons.ok,messageboxicon.information);
}
The response method for the private void Menuitem4_click (Object Sender,system.eventargs E)/"Exit Program" menu
{
This. Close ()//closes the current object (that is, form)
Application.exit ()//exit the application by static method exit () of the application class
}
private void Button1_Click (object sender, System.EventArgs e)//Response method when a user clicks a button
{
This. visible=false;//Hide Current main window
}
private void Notifyicon_dbclick (object sender, System.EventArgs e)//user double-click the application icon to enter the response method
{
This. visible=true;//Display the current main window
}
}
Iv. operation of the program
When you run the program, a main window is displayed, and you can see an extra icon on the taskbar. When the mouse hovers over it, a simple hint message is displayed, and clicking the Hide Window button on the window hides the main window, as shown in the following illustration:
Click the right mouse button on the icon will display a pop-up menu, you can select the appropriate menu to perform the corresponding functions such as "Display Window", "hidden Window", "Exit program, etc.", as shown in the following figure:
For example, click "Execute Program" in the pop-up menu to send information to an informational balloon, as shown in the following illustration:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.