C# 托盤程式(自己的)

來源:互聯網
上載者:User

製作托盤程式的關鍵是VS裡面的NotifyIcon控制項

這個小例子使表單最大化和最小化按鈕隱藏,關閉按鈕失效成為隱藏按鈕,雙擊托盤表徵圖顯示主表單,右鍵點擊快顯功能表

1、

在C#介面拖出一個notifyIcon1,設定相關屬性(我是初學者),或者在代碼裡面寫

3個Balloon*屬性分別是托盤氣泡的表徵圖、內容、標題(例如用在下面的FormClosing事件)

ContextMenuStrip屬性是下面將要用到的右鍵托盤表徵圖時關聯的彈窗

Icon屬性就是給托盤添加表徵圖

Text屬性是當滑鼠停留在托盤表徵圖時的提示資訊。

2、

在介面拖出一個contextMenuStrip1並添加“嘿嘿”和“退出”

全部代碼:

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 test
{
    public partial class Form16托盤 : Form
    {
        public Form16托盤()
        {
            InitializeComponent();
        }

        private void Form16托盤_Load(object sender, EventArgs e)
        {            
            //this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
        }

        private void 嘿嘿ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //this.WindowState = System.Windows.Forms.FormWindowState.Normal;
            this.Show();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            notifyIcon1.Dispose();//釋放托盤資源
            Application.Exit();//釋放所有資源
            
        }

        private void Form16托盤_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                this.Hide();
                notifyIcon1.BalloonTipTitle = "Title";
                notifyIcon1.BalloonTipText = "程式還在繼續運行......" + "\r\n" + "單擊托盤表徵圖還原視窗.";
                notifyIcon1.ShowBalloonTip(2000);
            }
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            this.Show();
        }
    }

}

轉載e.CloseReason屬性

if (e.CloseReason != System.Windows.Forms.CloseReason.WindowsShutDown)
{
                e.Cancel = true;
                this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
}

e.CloseReason屬性,其值為CloseReason枚舉類型

成員名稱 說明
ApplicationExitCall      Application 類的 Exit 方法被調用。  
FormOwnerClosing         所有者表單正在關閉。
MdiFormClosing           此多重文件介面 (MDI) 表單的父表單正在關閉。
None                     關閉原因未定義或者無法確定。
TaskManagerClosing Microsoft Windows
                          工作管理員正在關閉應用程式。
UserClosing              使用者正在通過使用者介面 (UI) 關閉該表單,例如通過單擊表單視窗上的“關閉”按鈕,通過選擇視窗控制功能表上的“關閉”按鈕,或者通過按 Alt+F4 等方式關閉。
WindowsShutDown          作業系統正在關閉所有應用程式以便準備關機。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.