C # winForm program startup and tray display

Source: Internet
Author: User

I have been learning C # For a long time, read books, and then read more blogs on the Internet, then I learned to make a Demo of a small function like the QQ tray icon:

(1) Click the close button in the window or display the tray when minimized;

(2) double-click the tray icon to display the window;

(3) Right-click the tray icon to provide three menu options: "exit", "hide", and "display ";

(4) The program can be set to start upon startup to hide the display of the taskbar. These four small functions.

1. Create a WinForm Program-TestIconForm and change the ShowInTaskbar attribute to false, so that the program will not be displayed in the taskbar; set the MaximizeBox attribute to false to block the maximize button; change the StartPosition attribute to CerternScreen. After the program runs, the window is centered.

2. Drag the NotifyIcon control-testpolicyicon in the public control in the toolbar. This is the display control of the notification area icon on the right side of the task bar where the program runs.

3. Drag ContextMenuStrip-testContextMenuStrip into the menu and toolbar in the toolbar. This control is associated with the menu when you right-click it.

4. Right-click testpolicyicon and add the ContextMenuStrip attribute to testContextMenuStrip. The two controls in steps 1 and 2 are associated to complete the above (3) function.

5. Right-click testContextMenuStrip and select Properties to go to Items. Then, click "add" to add three menu options: exitMenuItem, hideMenuItem, and showMenuItem. Change the Text attribute: exit, hide, and display.

The preparation is just like this. below is the rough code:

 

1) double-click TestIconForm to add the Load event and

Code

 Private void Form1_Load (object sender, EventArgs e)
{
Testpolicyicon. Icon = new Icon ("e: \ MyPicture \ testIcon. ico ");
// Obtain the program path
String startup = Application. ExecutablePath;

// Class Micosoft. Win32.RegistryKey. indicates the entry-level node in the Window registry. This class is installed in the registry.
RegistryKey rKey = Registry. LocalMachine;
RegistryKey autoRun = rKey. CreateSubKey (@ "SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run ");
Try
{
AutoRun. SetValue ("BookServer", startup );
RKey. Close ();
}
Catch (Exception exp)
{
MessageBox. Show (exp. Message. ToString (), "prompt", MessageBoxButtons. OK, MessageBoxIcon. Error );
}
}

Add the Form_Closing and SizeChanged events.

Code

 

Private void Form1_FormClosing (object sender, FormClosingEventArgs e) // closes the button event
{
E. Cancel = true;
This. Hide ();
}

Private void Form1_SizeChanged (object sender, EventArgs e) // click the minimize event button.
{
This. Hide ();
}

 

2) Add a MouseDoubleClick event to testpolicyicon.

Code

 Private void testpolicyicon_mousedoubleclick (object sender, MouseEventArgs e) // double-click the left button to display
{
If (e. Button = MouseButtons. Left)
{
This. Show ();
This. WindowState = FormWindowState. Normal;
This. Activate ();
}
}

3) Go to TestIconForm and click testContextMenuStrip. Then, you can view "exit", "hide", and "show" and double-click them to add corresponding events.

Code

 Private void exitMenuItem_Click (object sender, EventArgs e)
{
If (MessageBox. Show ("are you sure you want to exit the terminal service program? "," OK ", MessageBoxButtons. OKCancel,
MessageBoxIcon. Question, MessageBoxDefaultButton. Button2) = DialogResult. OK)
{
Testpolicyicon. Visible = false;
This. Close ();
This. Dispose ();
Application. Exit ();
}
}

Private void showMenuItem_Click (object sender, EventArgs e)
{
This. Hide ();
}

Private void hideMenuItem_Click (object sender, EventArgs e)
{
This. Show ();
This. WindowState = FormWindowState. Normal;
This. Activate ();
}

 

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.