1、窗体界面拖拽一个notifyIcon控件:

  

Winform-任务栏图标_最小化

2、窗体启动时加载图标:

/// <summary>
/// 初次启动
/// </summary>
private void Form1_Load(object sender, EventArgs e)
{
    //显示托盘图标
    notifyIcon1.Visible = true;
}

3、窗体最小化/关闭时缩小到任务栏图标:

/// <summary>
/// 关闭时阻止窗口关闭,变为隐藏到任务栏
/// </summary>
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason == CloseReason.UserClosing)
    {
        e.Cancel = true;  // 阻止关闭
        notifyIcon1.Visible = true;  //显示托盘图标
        this.WindowState = FormWindowState.Minimized;  // 最小化事件
        this.ShowInTaskbar = false;  // 隐藏任务栏最小化图标
    }
}

/// <summary>
/// 最小化隐藏图标(窗体状态更改事件)
/// </summary>
private void Form1_SizeChanged(object sender, EventArgs e)
{
    //最小化
    if (this.WindowState == FormWindowState.Minimized)
    {
        notifyIcon1.Visible = true;  //显示托盘图标
        this.ShowInTaskbar = false;  //将程序从任务栏移除显示
    }
    else
    {
        this.ShowInTaskbar = true;  // 将程序从任务栏显示
    }
}

4、双击图标打开窗体:

/// <summary>
/// 双击托盘图标后,要执行的操作
/// </summary>
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
    //设置程序允许显示在任务栏
    this.ShowInTaskbar = true;
    //设置窗口可见
    this.Visible = true;
    //设置窗口状态
    this.WindowState = FormWindowState.Normal;
    //设置窗口为活动状态,防止被其他窗口遮挡。
    this.Activate();
}

5、任务栏图标绑定右键菜单:

  ① 新建一个ContextMenuStrip右键菜单;

  ② 将notifyIcon1的ContextMenuStrip属性选择为“新建的ContextMenuStrip”控件;

  

Winform-任务栏图标_任务栏_02

6、窗口关闭时通知任务栏移除notifyIcon小图标

/// <summary>
/// 隐藏任务栏小图标(窗体关闭事件)
/// </summary>
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
    notifyIcon1.Visible = false;
}

作者:꧁执笔小白꧂