C#實現窗口最小化到系統托盤


關鍵字:C# 最小化 托盤
原文:http://www.cnblogs.com/txw1958/archive/2012/12/17/csharp-minimize-tray.html

先添加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
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM