1.在form中添加一个NotifyIcon控件
2.把1.ico这个图标放在binDebug目录下
先看看效果图:
3.代码的实现
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Text = "实现最小托盘";
this.notifyIcon1.Icon = new Icon("1.ico");//设置当前图标
}
private void button1_Click(object sender, EventArgs e)
{
WindowState = FormWindowState.Minimized;//最小化时隐藏窗体
//this.Visible = false; //是否显示该控件
this.notifyIcon1.Visible = true; //图标在任务栏区域中可见
this.notifyIcon1.ShowBalloonTip(300, "哈哈", "窗体成功隐藏了", ToolTipIcon.Info); //设置气球状工具提示显示的时间为10秒
this.ShowInTaskbar =false;//windows任务栏中不显示窗体
}
private void button2_Click(object sender, EventArgs e)
{
WindowState = FormWindowState.Maximized; //窗体最大化
}
private void button3_Click(object sender, EventArgs e)
{
WindowState = FormWindowState.Normal; //窗体常规
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)//判断是否窗体最小化
{
this.WindowState = FormWindowState.Normal;
}
this.Activate();//激活窗体
this.notifyIcon1.Visible = false;//在任务栏区域中不显示图标
this.ShowInTaskbar = true; //窗体在任务栏区域中可见
}
}
}