先添加notifyicon控件notifyIcon1
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;
namespace notifyIconShow
{
public partial class ColdJoke : Form
{
#region
//創建NotifyIcon對象
NotifyIcon notifyicon = new NotifyIcon();
//創建托盤圖標對象
Icon ico = new Icon("snow.ico");
//創建托盤菜單對象
ContextMenu notifyContextMenu = new ContextMenu();
#endregion
public ColdJoke()
{
InitializeComponent();
}
#region 托盤提示
private void Form1_Load(object sender, EventArgs e)
{
//設置鼠標放在托盤圖標上面的文字
this.notifyIcon1.Text = "笑話";
}
#endregion
#region 隱藏任務欄圖標、顯示托盤圖標
private void Form1_SizeChanged(object sender, EventArgs e)
{
//判斷是否選擇的是最小化按鈕
if (WindowState == FormWindowState.Minimized)
{
//托盤顯示圖標等於托盤圖標對象
//注意notifyIcon1是控件的名字而不是對象的名字
notifyIcon1.Icon = ico;
//隱藏任務欄區圖標
this.ShowInTaskbar = false;
//圖標顯示在托盤區
notifyicon.Visible = true;
}
}
#endregion
#region 還原窗體
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
//判斷是否已經最小化於托盤
if (WindowState == FormWindowState.Minimized)
{
//還原窗體顯示
WindowState = FormWindowState.Normal;
//激活窗體並給予它焦點
this.Activate();
//任務欄區顯示圖標
this.ShowInTaskbar = true;
//托盤區圖標隱藏
notifyicon.Visible = false;
}
}
#endregion
}
}

