using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace RichTextBoxUse
{
public partial class Form1 : Form
{
NotifyIcon notifyIcon = new NotifyIcon();//创建托盘对象
Icon icon =new Icon("exit.ico");//创建托D盘图标对象
ContextMenu notifyContextMenu = new ContextMenu();//创建托盘菜单对象
public Form1()
{
InitializeComponent();
notifyIcon.Text = "托盘名称";//鼠标放在托盘图标上显示的文字
notifyIcon.DoubleClick += new EventHandler(notifyIcon_DoubleClick);
notifyIcon.MouseClick += new MouseEventHandler(notifyIcon_MouseClick);
MenuItem mi = new MenuItem("Exit");
notifyContextMenu.MenuItems.Add(mi);
notifyIcon.ContextMenu = notifyContextMenu;
mi.Click += new EventHandler(mi_Click);
}
private void Form1_SizeChanged(object sender,EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
notifyIcon.Icon = icon;
this.ShowInTaskbar = false;//任务栏上不显示窗体图标
notifyIcon.Visible = true;//托盘中显示托盘图标
}
}
private void notifyIcon_DoubleClick(object sender,EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
WindowState = FormWindowState.Normal;//还原窗体
this.Activate();//激活窗体并给予它焦点
this.ShowInTaskbar =true;//在任务栏上显示窗体图标
notifyIcon.Visible = false;//隐藏托盘图标
}
}
private void notifyIcon_MouseClick(object sender,MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
this.notifyIcon.ContextMenu.Show(this,new Point(e.X, e.Y));
}
}
private void mi_Click(object sender,EventArgs e)
{
notifyIcon.Visible = false;
this.Close();
}
}
}