C # program for pallets

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 program Tray:

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: Ma Jinhu on 2001.12.08 ";

(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 co

Related Article

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.