C # develop the system tray Program

Source: Internet
Author: User

The so-called pallet program, as its name implies, is a program like a tray. The so-called tray is the icon displayed in the program running, and the position of the tray is the toolbar of the Windows system. The pallet program is intuitive, occupies a small amount of screen space, and can define Multiple Functional menus for it, which brings convenience to the operator, so more and more programmers are designing programs as pallets. We have seen examples of designing pallets in other languages. Most of them, the entire design process is still relatively cumbersome. However, Visual C #, the next-generation programming language highly recommended by Microsoft, can easily design a program with a tray. This article describes the specific process of designing a pallet program in Visual C.

First, we will introduce the environment required for designing the pallet program in this article:

(1) Microsoft Windows 2000 Server Edition

(2) Net Framework SDK beta 2

I. Main steps and Solutions of the pallet program:

The main reason why Visual C # Can Be Used to easily create a pallet program is.. NET Framework software development kit (.. NET Framework SDK. The following describes the specific usage of this component and the main skills in programming.

(1) how to hide the form after running the program:

We know that the main form cannot be seen after the pallet program runs. It will only be displayed on the toolbar. When you use Visual C # to design such a program, you can use either of the following methods to prevent the main form from being displayed after the program runs. One of the methods is to reload the onactivated () event in the main form. The onactivated () event is triggered only when the form is activated. By reloading this event, you can hide the main form. The specific program code is as follows:

Protected override void onactivated (eventargs E)
{
This. Hide ();
}

Another method is to initialize the main form, and do not display it by setting the attributes of the main form. The specific program code is as follows:

This. maximizebox = false;
This. minimizebox = false;
This. windowstate = system. Windows. Forms. formwindowstate. minimized;

The second method is used in the program described in this article.
(2) how to set the display icon for the tray program:

One attribute icon in the categoryicon component is used to set the tray icon. Because Visual C # Is A Complete OOP (Object-Oriented) language, anything in Visual C # can be processed as an object. Of course, corresponding to an icon, you can also use the object method to process it. The following statement is used to obtain an icon object:

Private icon mnettrayicon = new icon ("tray. ICO ");

Note: In compiled programs, a tray. ICO icon file must exist in the same directory; otherwise, errors may occur during program running.

Use the following statement to pay the icon object to the icon attribute in the notifyicon component. If the program is correctly compiled, the icon is displayed in the toolbar.

Trayicon. Icon = mnettrayicon;

(3) set the text content displayed when the mouse stays on the tray program:

The yyicon component has an attribute text. Set the content of this attribute, that is, the content displayed by hovering the cursor over the tray icon. The statement is as follows:

Trayicon. Text = "using Visual C # As a pallet program" + "" + "Author: XXX ";

(4) how to add a menu in the pallet program:

The categoryicon component has an object called contextmenu. The menu displayed in the pallet program is implemented by setting this object. The following program code adds a menu item to the pallet program:

Policyiconmnu = new contextmenu (mnuitms );
Trayicon. contextmenu = policyiconmnu;
// Set the menu for the pallet Program

(5) how to set the content of the contextmenu object:

The contextmenu object is the structure of the menu of the pallet program. Therefore, it is critical to set this object. In a program, you define a menu item array and set different values for this array (which includes some menu attributes and events ), then assign this array to the contextmenu object at the same time to set the contextmenu object. The specific code in the program is as follows:

// Define a menuitem array and assign the array to the contextmenu object at the same time
Menuitem [] mnuitms = new menuitem [3];
Mnuitms [0] = new menuitem ();
Mnuitms [0]. Text = "Use Visual C # As the pallet program! ";
Mnuitms [0]. Click + = new system. eventhandler (this. showmessage );

Mnuitms [1] = new menuitem ("-");
Mnuitms [2] = new menuitem ();
Mnuitms [2]. Text = "exit system ";
Mnuitms [2]. Click + = new system. eventhandler (this. exitselect );
Mnuitms [2]. defaultitem = true;

Policyiconmnu = new contextmenu (mnuitms );
Trayicon. contextmenu = policyiconmnu;
// Add the set contextmenu object to the tray Program

After the contextmenu object is successfully added, when the program compilation is complete, When you right-click the tray icon, the program automatically displays the contextmenu object encapsulated menu.

2. The program source code (tray. CS) introduced in this article ):
Source code of tray. CS:

Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
// The namespace used for import in the program
Public class Tray: Form
{
Private system. componentmodel. Container components = NULL;
Private icon mnettrayicon = new icon ("tray. ICO ");
Private policyicon trayicon;
Private contextmenu policyiconmnu;

Public tray ()
{
// Initialize the components used in the form
Initializecomponent ();
// Initialize all elements of the pallet Program
Initializenotifyicon ();
}

Private void initializenotifyicon ()
{
// Set attributes of the pallet Program
Trayicon = new notifyicon ();
Trayicon. Icon = mnettrayicon;
Trayicon. Text = "using Visual C # As a pallet program" + "" + "Author: XXX ";
Trayicon. Visible = true;
Trayicon. Click + = new system. eventhandler (this. Click );

// Define a menuitem array and assign the array to the contextmenu object at the same time
Menuitem [] mnuitms = new menuitem [3];
Mnuitms [0] = new menuitem ();
Mnuitms [0]. Text = "Use Visual C # As the pallet program! ";
Mnuitms [0]. Click + = new system. eventhandler (this. showmessage );

Mnuitms [1] = new menuitem ("-");

Mnuitms [2] = new menuitem ();
Mnuitms [2]. Text = "exit system ";
Mnuitms [2]. Click + = new system. eventhandler (this. exitselect );
Mnuitms [2]. defaultitem = true;

Policyiconmnu = new contextmenu (mnuitms );
Trayicon. contextmenu = policyiconmnu;
// Add the set contextmenu object to the tray Program
}
Public void click (Object sender, system. eventargs E)
{
MessageBox. Show ("Visual C # compile the Event Response in the pallet program ");
}

Public void showmessage (Object sender, system. eventargs E)
{
MessageBox. Show ("You clicked the first option in the menu ");
}

Public void exitselect (Object sender, system. eventargs E)
{
// Hide the icon in the pallet Program
Trayicon. Visible = false;
// Shut down the system
This. Close ();
}
// Clear resources used in the program
Public override void dispose ()
{
Base. Dispose ();
If (components! = NULL)
Components. Dispose ();
}

Private void initializecomponent ()
{
This. suspendlayout ();
This. autoscalebasesize = new system. Drawing. Size (5, 13 );
This. clientsize = new system. Drawing. Size (320, 56 );
This. controlbox = false;
This. maximizebox = false;
This. minimizebox = false;
This. windowstate = system. Windows. Forms. formwindowstate. minimized;

This. Name = "Tray ";
This. showintaskbar = false;
This. Text = "Use Visual C # As the pallet program! ";
This. resumelayout (false );

}
Static void main ()
{
Application. Run (new tray ());
}
}

Iii. Summary:

Through the above introduction, we can see that using Visual C # As a pallet program is not a complicated task, but a relatively easy task. We can also understand that although Visual C # is a powerful programming language, it is just an open.. NET Framework SDK is the key of this rich-content software package. NET programming language has a broader stage to display its own functions.

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.