Use C# to write an icon program parked on the taskbar
Author: Cai Shiyou Category: C#/VB Date: 2002-1-30 10:21:46
01-12-6 10:53:11 am
--------------------------------------------------------------------------------
Introduction
C# is a new language that Microsoft has launched for the .Net platform. As the first language of the .Net platform, it focuses almost all the latest results on software development and software engineering research. It is the first fully component-oriented language at present.
Microsoft's .Net platform can be called a new generation of Internet platforms. For technical developers, the two major features of the .Net platform are very outstanding. First, it is the public language runtime it provides, that is, the common language run platform, and on the other hand, it is its huge and comprehensive unified programming class.
Next, I will introduce to you an icon program written in C# on the .Net platform that can be parked on the taskbar, similar to SQL manager, antivirus software icons, and OICQ icons.
1. Basic concepts of icon program
The icon program we refer to here is similar to the virus monitoring program parked on the taskbar, running in the background, and other programs that are often exposed in Windows. After these programs are started, they form an icon to park on the taskbar, with the following appearance characteristics:
Double-click the icon to open the main program window.
The prompt message is displayed when the mouse stops on the icon.
Right-click the icon to pop up a shortcut menu. By clicking the menu item on the shortcut menu, you can run the corresponding program functions.
2. Introduction to program usage
.Net provides a lot of classes. During the development process, we only need to use the various classes it provides to implement most of our functions. Some classes in the .Net platform are encapsulated, some are virtual, and most of them are general public classes. You can directly construct instances of these public classes, or you can use all the functions of the class through inheritance. In this program, we use a lot of .Net classes. Below, I will briefly introduce the main classes!
kind
This is the most important class in this program. It inherits from the Component class and is not inheritable. Its main function is to generate an icon program that can be parked on the far right of the taskbar. Below is the introduction of its main members:
Constructor Function
public NotifyIcon(); Construct a NotifyIcon object directly without any parameters.
Public NotifyIcon(IContainer container); with an Icontainer parameter, representing the NotifyIcon container control.
Properties
The pop-up menu of the ContextMenu object.
Icon object icon
Text object ToolTip text, that is, the text displayed when the mouse stops on the object.
Visible indicates whether the object is visible.
Events
Click Raised when the object icon is clicked.
DoubleClick Raised when double-clicking the object icon.
kind
The Windows Forms application startup class provides a method to manage applications, such as starting Windows Forms use (Form), and exiting the program using (), etc.
kind
For Windows Forms program form class, the windows of the window program are generally generated directly or indirectly from this class. For example, if a simple form is generated, you can use new Forms(). In general applications, since the user form needs to add more visual elements and controls, such as buttons, labels, text boxes, etc., according to their needs, most of us derive the user's own form class from this class when using the Form class.
kind
The button control class provides a visual button element. Generally, the button display text is set through the Text property, and then the Click event response function is added to the button.
kind
Text tag class can be set to display general text information on the form by setting its Text property.
kind
The Windows Forms menu item class can generate a simple menu item. The menu item is the basic component of all menus. Through this class, you can set the appearance display properties and event response methods of the menu item. This class provides a variety of constructors for generating menu item objects. In actual applications, the MenuItem(string Text) method is generally used to generate a menu item with the display text Text. The menu item class provides a Click event so that the user can respond when clicking on a menu.
kind
Pop-up menu class, generating a pop-up menu object. It includes a menu item collection attribute, which allows it to easily manage and access pop-up menu items. There are two ways to generate pop-up menu objects: one is to use ContextMenu() directly without any parameters. The other is to use ContextMenu(MenuItem[] menuus), which has an array of menu items as parameters.
3. Programming and main code
Programming
This program is written using Visual Studio .Net. First, by creating a new Windows application project, add a button with the text "Hide Form" on the form, and a label explaining the demo program, and double-click the button to add hidden code to its Click event response method. Then find the NotifyIcon control in the toolbar panel, drag it onto the form, and then set its Text property and Icon properties (the Icon property is an image, you can select settings through the control property bar). Double-click the form to add the initialization code of the NotifyIcon class to the form's Load event response method, and then it can be compiled and run directly after completion. As shown in the figure below:
Program main code and comments
Below, some of the main codes and detailed comments in this program will be listed for everyone to deepen their understanding of the program.
public class Form1 :
{
//Define a NotifyIcon object
private notifyIcon1;
private void InitializeComponent()//Visual Studio .Net is automatically generated and executed when the form is initialized
{
//Create NotifyIcon object and set its basic properties such as Icon, Text and Visible, etc.
this.notifyIcon1 = new ();
this. = (()(("")));
this. = "This is my test program, please cool it!";
this. = true;
}
static void Main()
{
(new Form1());//Start and run the program
}
private void Form1_Load(object sender, e)
{
// Generate 4 menu item objects, the display text is "Show window", "Hide window", "Execute program", and "Exit program" respectively.
MenuItem menuItem1=new MenuItem("Show window");
MenuItem menuItem2=new MenuItem("Hide Window");
MenuItem menuItem3=new MenuItem("Execute Program");
MenuItem menuItem4=new MenuItem("Exit program");
//Add Click event response function for 4 menu items respectively
+=new (this.menuItem1_Click);
+=new (this.menuItem2_Click);
+=new (this.menuItem3_Click);
+=new (this.menuItem4_Click);
//Set the ContextMenu property of the NotifyIcon object to the pop-up menu object of the fresh face.
=new ContextMenu(new MenuItem[]{menuItem1,menuItem2,menuItem3,menuItem4});
// When the user double-clicks the program icon, the corresponding function will be executed.
+=new (this.notifyIcon_DBClick);
}
private void menuItem1_Click(object sender, e)//Response method of the "Show Window" menu
{
if(==false)=true;//If the current window is not displayed, the current window will be displayed.
}
private void menuItem2_Click(object sender, e)//Response method of the "Hide Window" menu
{
if(==true)=false;//If the current window is displayed, hide the window.
}
private void menuItem3_Click(object sender, e)//Response method of the "Execute Program" menu
{
// Show a prompt message box, indicating that the event has been responded.
("The demo program has been executed, and the function here is to display a prompt box!", "Prompt message",,);
}
private void menuItem4_Click(object sender, e)//Response method of the "Exit Program" menu
{
();//Close the current object (i.e. the form)
();//Exit the application through the static method Exit() of the Application class.
}
private void button1_Click(object sender, e)//Response method when a user clicks a button
{
=false;//Hide the current main window
}
private void notifyIcon_DBClick(object sender, e)//The response method of the user double-clicking the application icon
{
=true;//Show the current main window
}
}
4. Program operation
Running the program will display a main window, and you can also see an icon on the taskbar. When the mouse moves to it for a while, a simple prompt message will be displayed. Clicking the "Hide Window" button on the window will hide the main window, as shown in the figure below:
Right-clicking the icon will display a pop-up menu. At this time, you can select the corresponding menu to perform the corresponding functions such as "Show window", "Hide window", "Exit program, etc.", as shown in the figure below:
For example, click "Execute Program" in the pop-up menu to send a message prompt box, as shown in the following figure:
Author: Cai Shiyou Category: C#/VB Date: 2002-1-30 10:21:46
01-12-6 10:53:11 am
--------------------------------------------------------------------------------
Introduction
C# is a new language that Microsoft has launched for the .Net platform. As the first language of the .Net platform, it focuses almost all the latest results on software development and software engineering research. It is the first fully component-oriented language at present.
Microsoft's .Net platform can be called a new generation of Internet platforms. For technical developers, the two major features of the .Net platform are very outstanding. First, it is the public language runtime it provides, that is, the common language run platform, and on the other hand, it is its huge and comprehensive unified programming class.
Next, I will introduce to you an icon program written in C# on the .Net platform that can be parked on the taskbar, similar to SQL manager, antivirus software icons, and OICQ icons.
1. Basic concepts of icon program
The icon program we refer to here is similar to the virus monitoring program parked on the taskbar, running in the background, and other programs that are often exposed in Windows. After these programs are started, they form an icon to park on the taskbar, with the following appearance characteristics:
Double-click the icon to open the main program window.
The prompt message is displayed when the mouse stops on the icon.
Right-click the icon to pop up a shortcut menu. By clicking the menu item on the shortcut menu, you can run the corresponding program functions.
2. Introduction to program usage
.Net provides a lot of classes. During the development process, we only need to use the various classes it provides to implement most of our functions. Some classes in the .Net platform are encapsulated, some are virtual, and most of them are general public classes. You can directly construct instances of these public classes, or you can use all the functions of the class through inheritance. In this program, we use a lot of .Net classes. Below, I will briefly introduce the main classes!
kind
This is the most important class in this program. It inherits from the Component class and is not inheritable. Its main function is to generate an icon program that can be parked on the far right of the taskbar. Below is the introduction of its main members:
Constructor Function
public NotifyIcon(); Construct a NotifyIcon object directly without any parameters.
Public NotifyIcon(IContainer container); with an Icontainer parameter, representing the NotifyIcon container control.
Properties
The pop-up menu of the ContextMenu object.
Icon object icon
Text object ToolTip text, that is, the text displayed when the mouse stops on the object.
Visible indicates whether the object is visible.
Events
Click Raised when the object icon is clicked.
DoubleClick Raised when double-clicking the object icon.
kind
The Windows Forms application startup class provides a method to manage applications, such as starting Windows Forms use (Form), and exiting the program using (), etc.
kind
For Windows Forms program form class, the windows of the window program are generally generated directly or indirectly from this class. For example, if a simple form is generated, you can use new Forms(). In general applications, since the user form needs to add more visual elements and controls, such as buttons, labels, text boxes, etc., according to their needs, most of us derive the user's own form class from this class when using the Form class.
kind
The button control class provides a visual button element. Generally, the button display text is set through the Text property, and then the Click event response function is added to the button.
kind
Text tag class can be set to display general text information on the form by setting its Text property.
kind
The Windows Forms menu item class can generate a simple menu item. The menu item is the basic component of all menus. Through this class, you can set the appearance display properties and event response methods of the menu item. This class provides a variety of constructors for generating menu item objects. In actual applications, the MenuItem(string Text) method is generally used to generate a menu item with the display text Text. The menu item class provides a Click event so that the user can respond when clicking on a menu.
kind
Pop-up menu class, generating a pop-up menu object. It includes a menu item collection attribute, which allows it to easily manage and access pop-up menu items. There are two ways to generate pop-up menu objects: one is to use ContextMenu() directly without any parameters. The other is to use ContextMenu(MenuItem[] menuus), which has an array of menu items as parameters.
3. Programming and main code
Programming
This program is written using Visual Studio .Net. First, by creating a new Windows application project, add a button with the text "Hide Form" on the form, and a label explaining the demo program, and double-click the button to add hidden code to its Click event response method. Then find the NotifyIcon control in the toolbar panel, drag it onto the form, and then set its Text property and Icon properties (the Icon property is an image, you can select settings through the control property bar). Double-click the form to add the initialization code of the NotifyIcon class to the form's Load event response method, and then it can be compiled and run directly after completion. As shown in the figure below:
Program main code and comments
Below, some of the main codes and detailed comments in this program will be listed for everyone to deepen their understanding of the program.
public class Form1 :
{
//Define a NotifyIcon object
private notifyIcon1;
private void InitializeComponent()//Visual Studio .Net is automatically generated and executed when the form is initialized
{
//Create NotifyIcon object and set its basic properties such as Icon, Text and Visible, etc.
this.notifyIcon1 = new ();
this. = (()(("")));
this. = "This is my test program, please cool it!";
this. = true;
}
static void Main()
{
(new Form1());//Start and run the program
}
private void Form1_Load(object sender, e)
{
// Generate 4 menu item objects, the display text is "Show window", "Hide window", "Execute program", and "Exit program" respectively.
MenuItem menuItem1=new MenuItem("Show window");
MenuItem menuItem2=new MenuItem("Hide Window");
MenuItem menuItem3=new MenuItem("Execute Program");
MenuItem menuItem4=new MenuItem("Exit program");
//Add Click event response function for 4 menu items respectively
+=new (this.menuItem1_Click);
+=new (this.menuItem2_Click);
+=new (this.menuItem3_Click);
+=new (this.menuItem4_Click);
//Set the ContextMenu property of the NotifyIcon object to the pop-up menu object of the fresh face.
=new ContextMenu(new MenuItem[]{menuItem1,menuItem2,menuItem3,menuItem4});
// When the user double-clicks the program icon, the corresponding function will be executed.
+=new (this.notifyIcon_DBClick);
}
private void menuItem1_Click(object sender, e)//Response method of the "Show Window" menu
{
if(==false)=true;//If the current window is not displayed, the current window will be displayed.
}
private void menuItem2_Click(object sender, e)//Response method of the "Hide Window" menu
{
if(==true)=false;//If the current window is displayed, hide the window.
}
private void menuItem3_Click(object sender, e)//Response method of the "Execute Program" menu
{
// Show a prompt message box, indicating that the event has been responded.
("The demo program has been executed, and the function here is to display a prompt box!", "Prompt message",,);
}
private void menuItem4_Click(object sender, e)//Response method of the "Exit Program" menu
{
();//Close the current object (i.e. the form)
();//Exit the application through the static method Exit() of the Application class.
}
private void button1_Click(object sender, e)//Response method when a user clicks a button
{
=false;//Hide the current main window
}
private void notifyIcon_DBClick(object sender, e)//The response method of the user double-clicking the application icon
{
=true;//Show the current main window
}
}
4. Program operation
Running the program will display a main window, and you can also see an icon on the taskbar. When the mouse moves to it for a while, a simple prompt message will be displayed. Clicking the "Hide Window" button on the window will hide the main window, as shown in the figure below:
Right-clicking the icon will display a pop-up menu. At this time, you can select the corresponding menu to perform the corresponding functions such as "Show window", "Hide window", "Exit program, etc.", as shown in the figure below:
For example, click "Execute Program" in the pop-up menu to send a message prompt box, as shown in the following figure: