Use Visual C # to do pallet programs

Source: Internet
Author: User

The so-called tray program as the name implies is like a tray of the same program. The so-called tray is the operation of the display of the icon, and hold up the position is the Windows System's toolbar. The tray program has the intuitive, occupies the screen space to be small and may define many function menus for it, this causes the operator to bring the convenience, therefore more and more program designers all put the design into the pallet this way. We have seen the use of other languages to design the tray program examples, most of which, the entire design process is relatively cumbersome. And for Microsoft's highly recommended next-generation program development language--visual C #, it can be very convenient to design a tray program. This article is about the Visual C # design of the tray program specific process.

Let's start by introducing the environment you need to design the pallet program in this article:

(1) Microsoft company Windows 2000 Server Edition

(2). Net FrameWork SDK Beta 2

A The main steps and solutions of the pallet program:

Why is the use of Visual C # can be very convenient to do a tray program, the main reason is in. NET Framework, the WinForm component in the Software Development Kit (. NET Framework SDK) defines a component--notifyicon component that is specifically designed to develop a pallet program. Here is a brief introduction to the specific usage of this component and the key techniques in the design of the program.

(1). How to hide a form after a program is run:

We know that the tray program can not see the main form after running, he will only appear on the toolbar. When designing such a program in Visual C #, there are two ways to make the program run without displaying the main form. One method is to overload the Onactivated () event in the main form, and the onactivated () event is triggered when the form is active. By overloading this event, you can achieve the purpose of hiding the main form. The specific program code is as follows:

protected override void OnActivated ( EventArgs e )
{
this.Hide ( ) ;
}

Another approach is to do this when initializing the main form, by setting the properties of the main form to not be displayed. The specific program code is as follows:

this.MaximizeBox = false ;
this.MinimizeBox = false ;
this.WindowState = System.Windows.Forms.FormWindowState.Minimized ;

In the program described in this article, the second method is used.

(2). How to set the display icon for the tray program:

In the NotifyIcon component there is a property icon to set the tray icon, because Visual C # is a complete oop (object-oriented) language, anything in Visual C # can be handled as an object. Of course, corresponding to an icon, you can also use the object method to deal with him. We get an icon object with the following statement:

private Icon mNetTrayIcon = new Icon ( "Tray.ico" ) ;

Please note: In a compiled program, you must have a Tray.ico icon file in the same directory, otherwise the program will run wrong.

This icon object is paid to the Icon property in the NotifyIcon component by using the following statement, which is displayed in the toolbar if the program compiles correctly.

TrayIcon.Icon = mNetTrayIcon ;

(3). Set the text content displayed when the mouse rests on the tray program:

There is an attribute text in the NotifyIcon component. Set the content of this property, is the mouse resting on the tray icon displayed on the content. The specific statements are as follows:

TrayIcon.Text = "用Visual C#做托盘程序" + "\n" + "作者:马金虎于2001.12.08" ;

(4). How to add a menu to the tray program:

In the NotifyIcon component there is an object called ContextMenu, and the menu displayed in the tray program is implemented by setting this object. The following program code is to add a menu item for a pallet program:

notifyiconMnu = new ContextMenu ( mnuItms ) ;
TrayIcon.ContextMenu = notifyiconMnu ;
//为托盘程序设定菜单

(5). How to set the contents of the ContextMenu object:

The ContextMenu object is the structure of the tray program's menu, so how to set this object is more critical in this program. In a program, you can implement the setting of a ContextMenu object by defining a menu item array and setting a different value for the array (which includes some of the menu's properties and events) and assigning the array to the ContextMenu object at the same time. Here is the specific code in the program:

//定义一个MenuItem数组,并把此数组同时赋值给ContextMenu对象
MenuItem [ ] mnuItms = new MenuItem [ 3 ] ;
mnuItms [ 0 ] = new MenuItem ( ) ;
mnuItms [ 0 ] .Text = "用Visual C#做托盘程序!" ;
mnuItms [ 0 ] .Click += new System.EventHandler ( this.showmessage ) ;
mnuItms [ 1 ] = new MenuItem ( "-" ) ;
mnuItms [ 2 ] = new MenuItem ( ) ;
mnuItms [ 2 ] .Text = "退出系统" ;
mnuItms [ 2 ] .Click += new System.EventHandler ( this.ExitSelect ) ;
mnuItms [ 2 ] .DefaultItem = true ;
notifyiconMnu = new ContextMenu ( mnuItms ) ;
TrayIcon.ContextMenu = notifyiconMnu ;
//为托盘程序加入设定好的ContextMenu对象

When the ContextMenu object is successfully joined, when the program compiles to run, when the right mouse button clicks on the tray icon, the program will automatically pop up the ContextMenu object packaged menu.

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.