托盘程序
namespace 托盘程序
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
// 双击托盘打开主菜单
this.Visible = true;
if (this.WindowState == FormWindowState.Minimized)
{
this.WindowState = FormWindowState.Normal;
}
} private void Form1_Load(object sender, EventArgs e)
{
myIcon.ContextMenuStrip = myMenu; //右键托盘弹出myMenu菜单
} //窗体关闭时,将程序最小化到系统托盘上
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)//当用户点击窗体右上角X按钮或(Alt F4)时 发生
{
//MessageBox.Show("程序将最小化到系统托盘区");
e.Cancel = true; // 取消关闭窗体
this.WindowState = FormWindowState.Minimized; //使关闭时窗口向右下角缩小的效果
this.ShowInTaskbar = false;//取消窗体在任务栏的显示
this.myIcon.Visible = true;//显示托盘图标
this.Hide();
}
} private void button1_Click(object sender, EventArgs e)
{
myIcon.ShowBalloonTip(500,"我是一个小气泡","没事,我就秀一下存在感。",ToolTipIcon.Info);
} private void myMenu_Opening(object sender, CancelEventArgs e)
{
}
//左键单击托盘图标时,显示主窗体,右击时当然是弹出上面设置的菜单
private void myIcon_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
myMenu.Show();
}
if (e.Button == MouseButtons.Left)
{
this.Visible = true;
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = true;
}
} private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
//放一个上下文菜单,添加几个基本项,"显示主窗体","退出" ,将这个菜单挂到myIcon上
private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
this.Show();
this.ShowInTaskbar = true;
this.myIcon.Visible = true;
}
private void menuExit_Click(object sender, EventArgs e)
{
this.Dispose(true);
Application.ExitThread();
} private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit(); } private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{ } private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
{ }
}
}
评论