Visual C # tray program production experience

xiaoxiao2021-03-06  113

First, of course, the Notifyicon control is to be introduced.

Private system.windows.forms.notifyicon notifyiconserver;

This.notifyiconserver = new system.windows.forms.notifyicon (this.components);

Next, set the properties of the control:

//

// NotifyiconServer

//

this.notifyiconserver.ContextMenu = this.contextMenutRay; // Specify context menu

THIS.NOTIFYICONSERVER.ICON = ((System.drawing.icon) ("NotifyiconServer.icon"))))); // Specify the icon

this.notifyiconserver.text = "my server"; / / Specify the mouse hover display

This.notifyiconserver.mousedown = new system.windows.Forms.MouseEventHandler (this.notifyiconserver_mousedown);

THIS.NOTIFYICONSERVER.DOUBLECLICK = New System.EventHandler (this.notifyiconserver_doubleclick);

//

// contextMenutray context menu

//

This.ContextMenutRay.Menuitems.Addrange (New System.windows.Forms.MenuItem [] {

This.Menuitem1,

THIS.MENUITEM2});

//

// Menuitem1

//

THIS.MENUITEM1.INDEX = 0;

THIS.MENUITEM1.TEXT = "Open Chat Server";

THIS.MENUITEM1.Click = new system.eventhandler (this.Menuitem1_click);

//

// Menuitem2

//

this.Menuitem2.index = 1;

this.Menuitem2.Text = "Exit Program";

THIS.MENUITEM2.Click = new system.eventhandler (this.Menuitem2_click);

When the user clicks the "Close" small button of the form, the form is not really turned off, but put the program into the system tray.

Private void chatform_closing (object sender, system.componentmodel.canceleventargs e)

{

E.cancel = true; // Cancel the form

THIS.HIDE ();

THIS.SHOWINTASKBAR = FALSE;

this.notifyiconserver.visible = true; // Displays the tray icon

}

Notifyicon Double-click Event, you can recover the program form:

Private void Notifyiconserver_doubleClick (Object Sender, System.Eventargs E)

{

THIS.SHOW ();

IF (this.WindowState == formwindowstate.minimized)

THIS.WINDOWSTATE = FormWindowState.Normal;

THIS.Activate ();

Additional requirements: Click the left mouse button or you can also call up a menu.

The solution is as follows:

First, declare a context menu:

//

// contextMenuleft Left mumgu

//

This.ContextMenuleft.Menuitems.addRange (new system.windows.forms.MenuItem [] {

THIS.MENUITEM3});

//

// Menuitem3

//

this.Menuitem3.index = 0;

THIS.MENUITEM3.TEXT = "About ...";

Since the context menu cannot be displayed directly on Notifyicon, only one Control is created as a container, which is a better way to have a better way.

Private void NotifyiconServer_MouseDown (Object Sender, System.Windows E)

{

IF (E.Button == MouseButtons.Left)

{

Control Control = New Control (null, control.mouseposition.x, control.mouseposition.y, 1, 1);

Control.visible = true;

Control.createControl ();

Point Pos = New Point (0, 0); // The two numbers here should be appropriately adjusted according to your context menu.

This.ContextMenuleft.show (Control, POS);

}

}

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

New Post(0)