1.从工具栏拉一个NotifyIcon控件到Form1;
2.给notifyIcon1的Icon属性指定一个图标,并将Visible设置为False;
3.Form1.cs代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized) //最小化
{
this.ShowInTaskbar = false;
notifyIcon1.Visible = true;
}
else if (this.WindowState == FormWindowState.Normal) //恢复窗体
{
this.ShowInTaskbar = true;
notifyIcon1.Visible = false;
}
}
private void notifyIcon1_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Normal;
}
}
}后记:如果需要给托盘加一个右键菜单,只需在窗体中添加一个ContextMenuStrip菜单(contextMenuStrip1),并将notifyIcon1的ContextMenuStrip属性值设置为contextMenuStrip1即可!同时修改下托盘点击事件的处理程序:
private void notifyIcon1_Click(object sender, EventArgs e)
{
MouseEventArgs e1 = (MouseEventArgs)e;
if (e1.Button == MouseButtons.Left)
{
this.WindowState = FormWindowState.Normal;
}
}
评论(0)
暂无评论。