C # implement the taskbar icon Program

Source: Internet
Author: User
This is an icon written in C # On the. NET platform that can be parked on the taskbar. Program , Similar to the SQL manager, anti-virus software Icon, and OICQ icon.

I. Basic concepts of the icon Program

The icon program we refer to here is similar to the virus monitoring and SQL Service Manager programs that are often used in windows and parked on the taskbar. After these programs are started, an icon is displayed on the taskbar, which has the following characteristics:
Double-click the icon to open the main window of the program.
A prompt is displayed when you move your cursor over the icon.
Right-click the icon to bring up a shortcut menu. You can click the menu item on the shortcut menu to run the corresponding program functions.

II. Introduction to program usage

. NET provides a lot of classes. In the development process, we only need to use the various types provided by it to implement most of our functions ,. some of the classes on the net platform are encapsulated, some are virtual, and most of them are general public classes. You can directly construct the instances of these public classes, or use all the functions of the classes through inheritance. In this program, we use a lot of. Net classes. below, I will give a brief introduction to the main classes!

* System. Windows. fomrs. policyicon class

This is the most important class in this program. It is an encapsulation class inherited from the component class and cannot be inherited. Its main function is to generate an icon program that can be parked on the rightmost side of the taskbar. The following is an introduction to its main members:
Constructor Function)
Public policyicon (); directly constructs a policyicon object without any parameters.
Public yyicon (icontainer container); contains an icontainer parameter, which represents the notifyicon container control.

Properties)
The pop-up menu of the contextmenu object.
Icon of the icon object
The tooltip Text of the Text object, that is, the text displayed when the mouse stops on the object.
Visible indicates whether the object is visible.

Events)
Clcik is triggered when an object icon is clicked.
DoubleClick is triggered when you double-click an object icon.

* System. Windows. Forms. Application class

The Windows Forms application startup class provides methods to manage applications, such as enabling Windows Forms to use application. Run (form), and exiting applications to use application. Exit.

* System. Windows. Forms. Form class

Windows Forms Program form class, Windows program forms are generally generated directly or indirectly from this class, such as generating a simple form using new forms. In general applications, as you need to add more visual elements and controls, such as buttons, labels, and text boxes, to your form, when using the form class, most of us derive the user's own form class from this class.

* System. Windows. Forms. Button class

The button control class provides a visual button element. Generally, you can set the text to be displayed by setting the text attribute, and then add the Click Event Response Function for the button.

* System. Windows. Forms. Label class

Text label class. You can set its text attribute to display general text information on the form.
System. Windows. Forms. menuitem class

Windows Forms menu items can generate a simple menu item, which is the basic element of all menus. You can set the Display Properties and Event Response methods of menu items. This class provides a variety of constructors for generating menu item objects. in practical applications, menuitem (string text) is generally used to generate a menu item whose displayed text is text. The menu item class provides click events so that users can respond when clicking the menu.

* System. Windows. Forms. contextmenu class

The pop-up menu class generates a pop-up menu object. It includes a menu item set attribute so that it can easily manage and access the items in the pop-up menu. You can use contextmenu () to generate a pop-up menu object without any parameters. The other is to use contextmenu (menuitem [] menus), with an array of menu items as the parameter.

Iii. program design and mainCode
Program Design
This program uses Visual Studio. net, first generate a Windows Forms initial form by creating a Windows application project, add a button with the text as "hide form" on the form, and a label that describes the demo program, double-click the button to add the code for hiding the window break in the Click Event Response Method. Find the yyicon control in the toolbar, drag it to the form, and set its text attribute and Icon attribute (the icon attribute is an image, you can select settings through the control attribute bar ). Double-click the form to add the initialization code of the policyicon class for the load Event Response Method of the form, and then compile and run the Code directly. As shown in:

Http://www.ccw.com.cn/htm/app/aprog/01_12_6_3a.gif

Main program code and comments

The main code and detailed comments in the middle of the program will be listed below for your understanding of the program.

Public class form1: system. Windows. Forms. Form

{

// Define a policyicon object

Private system. Windows. Forms. policyicon policyicon1;

Private void initializecomponent () // automatically generated by Visual Studio. NET and executed during form Initialization

{

// Create a policyicon object and set its basic attributes, such as icon, text, and visible.

This. policyicon1 = new system. Windows. Forms. policyicon (this. components );

This. policyicon1.icon = (system. Drawing. Icon) (resources. GetObject ("policyicon1.icon ")));

This. policyicon1.text = "this is my test program, and cool! ";

This. policyicon1.visible = true;

}

Static void main ()

{

Application. Run (New form1 (); // start the running program

}

Private void form1_load (Object sender, system. eventargs E)

{

// Four menu item objects are generated. The displayed text is "display window", "hidden window", "execution program", and "Exit program"

Menuitem menuitem1 = new menuitem ("display window ");

Menuitem menuitem2 = new menuitem ("Hide window ");

Menuitem menuitem3 = new menuitem ("executable program ");

Menuitem menuitem4 = new menuitem ("Exit program ");

// Add the Click Event Response Function for four menu items respectively

Menuitem1.click + = new system. eventhandler (this. menuitem#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 attribute of the categoryicon object to the pop-up menu object

Policyicon1.contextmenu = new contextmenu (New menuitem [] {menuitem1, menuitem2, menuitem3, menuitem4 });

// When you double-click the program icon, the corresponding function is executed.

Policyicon1.doubleclick + = new system. eventhandler (this. policyicon_dbclick );

}

Private void menuitem1_click (Object sender, system. eventargs e) // response method of the "display window" menu

{

If (this. Visible = false) This. Visible = true; // if the current window is not displayed, the current window is displayed.

}

Private void menuitem2_click (Object sender, system. eventargs e) // response method of the "Hide window" menu

{

If (this. Visible = true) This. Visible = false; // hide the window if the current window is displayed.

}

Private void menuitem3_click (Object sender, system. eventargs e) // response method of the "execution program" menu

{

// A prompt box is displayed, indicating that the event has been responded.
MessageBox. Show ("the demo program has been executed. This function is to display a prompt box! "," Message ", messageboxbuttons. OK, messageboxicon. information );

}

Private void menuitem4_click (Object sender, system. eventargs e) // response method of the "Exit program" menu

{

This. Close (); // close the current object (that is, the form)

Application. Exit (); // exit the application by using the static method exit () of the application class.

}

Private void button#click (Object sender, system. eventargs e) // response method when the user clicks the button

{

This. Visible = false; // hide the current main window

}

Private void policyicon_dbclick (Object sender, system. eventargs e) // The response method that the user double-click the application icon.

{

This. Visible = true; // display the current main window

}

}

4. program running

Run the program. A main window is displayed. An icon is displayed on the taskbar. When you move the cursor over it, a simple prompt is displayed. clicking "Hide window" on the window will hide the main window, as shown in:
Http://www.ccw.com.cn/htm/app/aprog/01_12_6_3b.gif
Right-click the icon to display a pop-up menu. You can select a menu to execute the corresponding functions, such as "display window", "Hide window", and "Exit program ", as shown in:
Http://www.ccw.com.cn/htm/app/aprog/01_12_6_3c.gif
For example, click "program execution" in the pop-up menu to display an information prompt box, as shown in:
Http://www.ccw.com.cn/htm/app/aprog/01_12_6_3d.gif

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.