using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace TrayIcon
{
public partial class Form1 : Form
{
private Icon ico = new Icon("..\\..\\qq.ico");
private NotifyIcon trayIcon;
private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
private System.Windows.Forms.ToolStripMenuItem showMainFrameToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem;
public Form1()
{
InitializeComponent();
InitContextMenu();
this.ShowInTaskbar = false; //以下两句用来使窗体在启动时不显示
this.WindowState = FormWindowState.Minimized;
InitTrayIcon();
}
//private void Form1_Activated(object sender, EventArgs e)
//{
// //this.Show(); //也可以用这句使窗体在启动时不显示
//}
private void InitTrayIcon()
{
trayIcon = new NotifyIcon();
trayIcon.Icon = ico;
trayIcon.Text = "托盘程序";
trayIcon.Visible = true;
//trayIcon.Click += new EventHandler(trayIcon_Click);
trayIcon.DoubleClick += new EventHandler(trayIcon_DoubleClick);
trayIcon.ContextMenuStrip= contextMenuStrip1;
}
void trayIcon_DoubleClick(object sender, EventArgs e)
{
// Show the form when the user double clicks on the notify icon.
// Set the WindowState to normal if the form is minimized.
if (this.WindowState == FormWindowState.Minimized)
this.WindowState = FormWindowState.Normal;
// Activate the form.
this.Activate();
//throw new Exception("The method or operation is not implemented.");
}
//void trayIcon_Click(object sender, EventArgs e)
//{
// MessageBox.Show("托盘程序");
// //throw new Exception("The method or operation is not implemented.");
//}
private void showMainFrameToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Activate();
this.Show();
MessageBox.Show("!!!!");
}
private void exitToolStripMenuItem_Click_1(object sender, EventArgs e)
{
trayIcon.Visible = false;
this.Close();
Application.Exit();
}
private void InitContextMenu()
{
//
// contextMenuStrip1
//
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.showMainFrameToolStripMenuItem,
this.exitToolStripMenuItem});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.Size = new System.Drawing.Size(153, 70);
//
// showMainFrameToolStripMenuItem
//
this.showMainFrameToolStripMenuItem.Name = "showMainFrameToolStripMenuItem";
this.showMainFrameToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.showMainFrameToolStripMenuItem.Text = "ShowMainFrame";
this.showMainFrameToolStripMenuItem.Click += new System.EventHandler(this.showMainFrameToolStripMenuItem_Click);
//
// exitToolStripMenuItem
//
this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
this.exitToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.exitToolStripMenuItem.Text = "Exit";
this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click_1);
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
//if (this.WindowState == FormWindowState.Minimized)
//{
// this.Hide(); //不知道为什么不能加这个,加这个以后,双击时窗口便不能得新显示。
//}
}
}
}