首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 最小化至系統托盤圖示的 Window Application 實作

     編輯:關於C語言

實作步驟

  1. 表單屬性:ShowInTaskbar = false(不在工作列顯示)。
  2. 加入 NotifyIcon:拖入 notifyIcon1,設定 Icon 屬性指定托盤圖示。
  3. 處理最小化事件:
    this.SizeChanged += new EventHandler(Form1_SizeChanged);
    private void Form1_SizeChanged(object sender, EventArgs e) {
        if (this.WindowState == FormWindowState.Minimized) {
            this.Hide();
            this.notifyIcon1.Visible = true;
        }
    }
    
  4. 處理托盤圖示點擊還原:
    private void notifyIcon1_Click(object sender, EventArgs e) {
        this.Visible = true;
        this.WindowState = FormWindowState.Normal;
        this.notifyIcon1.Visible = false;
    }
    
  5. (可選)右鍵選單:拖入 ContextMenu,設定 notifyIcon1.ContextMenu,選單項加入「顯示」、「退出」等動作。

關鍵點

  • Hide() 隱藏表單但進程持續運行。
  • NotifyIcon.Visible 控制托盤圖示顯示/隱藏。
  • 還原時同步設定 WindowState = Normal 並隱藏托盤圖示。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved