Visual C # tray program production experience

xiaoxiao2021-03-05  26

First, of course, the Notifyicon control item is to be introduced. Private system.windows.forms.notifyicon notifyiconserver; this.notifyiconserver = new system.windows.Forms.notifyicon (this.components);

Next, the various properties of the control item are set: // // NotifyiconServer /// this.notifyiconserver.contextMenu = this.contextMenutMenu = this.contextMenutRay; // Specify context menu this.notifyiconserver.icon = (system.drawing.icon) (resources.drawing.icon) ("NotifyiConserver.icon"))))))))))))); // Specify this.notifyiconserver.text = "my server"; // Specify the hull 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.contextMenut.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, it does not really turn off the form, but put the program in the system tray.

private void ChatForm_Closing (object sender, System.ComponentModel.CancelEventArgs e) {e.Cancel = true; // Close Cancel form this.Hide (); this.ShowInTaskbar = false; this.notifyIconServer.Visible = true; // show tray }

NOTIFYICON Double-click Event, you can restore 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 button to adjust the function table.

The solution is as follows:

First, declare a context menu: // // contextMenuleft Left button // this.contextMenuleft.Menuitems.Addrange (new system.windows.Forms.Menuitem [] {this.MenuItem3}); /// Menuitem3 // THIS .menuitem3.index = 0; this.Menuitem3.text = "About ...";

Because the context function table cannot be displayed directly on Notifyicon, only one Control is created as a container, which is the right way, should have a better way. private void notifyIconServer_MouseDown (object sender, System.Windows.Forms.MouseEventArgs 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); // The two digits here To properly adjust this.contextMenuleft.show according to your context function Control, POS);}}

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

New Post(0)