1. 程式人生 > >[C#]C# 最小化 托盤

[C#]C# 最小化 托盤

先新增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
}

}
複製程式碼