C # make tray programs

xiaoxiao2021-03-06  56

The so-called tray program is the same as the same program like the tray. The so-called tray is the icon shown in the program run, and the position of the paid is the toolbar of the window system. The tray program has intuitive, occupying the screen space and can define multiple function menus for it, which brings convenience to the operator, so more and more programmers are designed into a tray. We have seen examples of design tray programs in other languages, most of which, the entire design process is still relatively cumbersome. For Microsoft's highly recommended next-generation procedures development language - Visual C #, it is very convenient to design a tray program. This article is to introduce the specific process of design tray programs for Visual C #. First introduce the environment you need to design the tray program in this article: (1). Microsoft Window 2000 Server Edition (2). Net Framework SDK Beta 2. The main steps and solutions for tray programs: Why do Visual C # can be very convenient to make a tray program, the main reason is to define a specialized in WinForm components in the .NET Framework Software Development Cap (.NET Framework SDK) Components for developing tray programs - Notifyicon components. Let's introduce the specific usage of this component and the main skills in programming. (1). How to hide the form after the program: We know that the tray program is unable to see the main form, he will only be displayed on the toolbar. When you design such a program with Visual C #, you can use two ways to make the program run without displaying the main form. One method is to overrun the onactivated () event in the main form, and onActiVated () event is triggered when the form is activated. The purpose of hidden the main form is reached by overloading this event. The specific program code is as follows: protected override void onactivated (Eventargs e) {this.hide ();} The other method is to achieve the target of the main form when the primary form is initialized, and the properties of the main form are not displayed. . The specific program code is as follows: this.maximizebox = false; this.minimizebox = false; this.windowState = system.windows.forms.formwindowsTate.minimized; in the program introduced in this article, the second method is used. (2). How to set the display icon for the tray program: There is a property icon in the Notifyicon component to set the tray icon, because Visual C # is a complete OOP (object-oriented) language, anything in Visual C # Can be treated as an object. Of course, the corresponding ICON can also be handled by the object's approach. We get an icon object by the following statements: private icon mnettrayicon = new icon ("tray.ico"); please note: In the compiled program, you must have a TRAY.ICO icon file in the same directory, otherwise The program will be wrong. This icon is displayed in the toolbar by paying this Icon object to the iCON property in the Notifyicon component. At this time, this icon is displayed in the toolbar. TRAYICON.ICON = MNETTRAYICON; (3). Setting the text content displayed on the tray program in the mouse: NOTIFYICON component has an attribute Text. Setting the content of this property is the content that the mouse is displayed on the tray icon.

Specific statement is as follows: TrayIcon.Text = "Visual C # to do with the tray program" "/ n" "Author: Ma Jinhu on 2001.12.08"; (4) How to join the program in the tray menu: there is an object in the NotifyIcon component Call ContextMenu, the menu displayed in the tray program is implemented by setting this object. The following program code is joined for the tray program: notifyiconmnu = new contextMenu (mnuitms); trayicon.contextMenu = notifyiconmnu; // For the tray program setup menu (5). How to set the contents of the ContextMenu object: ContextMenu object is a tray The structure of the menu of the program, so how to set this object and is more critical in this program. In the program, it is to define a menu item array and set different values ​​for this array (this includes some attributes and events of the menu), and then assign this array to the ContextMenu object to implement the settings to the ContextMenu object. Process. The following is a specific code in the program: // Defines a MenuItem array, assigns this array to the ContextMenu object menuitem [] mnuitms = new menuItem [3]; mnuitms [0] = new menuItem (); mnuitms [0] .text = "Use Visual C # to do tray programs!"; 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.exitslect); mnuitms [2] .defaultItem = true; notifyiconmnu = new contextMenu (mnuitms); TRAYICON .ContextMenu = notifyiconmnu; // To add a set of setup for the tray program When successfully joined the ContextMenu object, when the program compiles to complete the run, the program will automatically pop up the contextMenu object package.

two. The program source code introduced herein: TRAY.CS source code: use system; use system.drawing; use system.collection; using system.ComponentModel; use system.windows.forms; using system.data; / / introduced in the program to use the namespace public class Tray: Form {private System.ComponentModel.Container components = null; private Icon mNetTrayIcon = new Icon ( "tray.ico"); private NotifyIcon TrayIcon; private ContextMenu notifyiconMnu; public Tray () {// InitializeComponent () {// InitializeComponent (); // Initializenotifyicon ();} private void initializenotifyicon () {// Set the properties of the tray program TRAYICON = New Notifyicon ); TrayIcon.Icon = mNetTrayIcon; TrayIcon.Text = "do tray program with Visual C #" "/ n" "author: Ma Jinhu on 2001.12.08"; TrayIcon.Visible = true; TrayIcon.Click = new System. EventHandler (this.click); // Defines a MenuItem array and assigns this array to the ContextMenu object MenuItem [] mnuitms = new menuItem [3]; mnuitms [0] = new menuItem (); mnuitms [0] .text = "Use Visual C # to do tray programs!"; MNUITMS [0] .click = new system.eventhandler (this.showMessage); Mnuitms [1] = new meneuitem ("-"); mnuitms [2] = new menuItem (); mnuitms [2] .Text = "Exit System"; Mnuitms [2] .click = new system.EventHandler (this. ExitSelect); mnuItms [2] .DefaultItem = true; notifyiconMnu = new ContextMenu (mnuItms); TrayIcon.ContextMenu = notifyiconMnu; // Add good ContextMenu object set} public void click (object sender, System.EventArgs e program the tray ) {MessageBox.show ("Visual C # Event Response in the Tray Program");} Public Void ShowMessage (Object Sender, System.EventArgs E) {MessageBox.show ("You Click on the first option");

转载请注明原文地址:https://www.9cbs.com/read-115395.html

New Post(0)