C# NotifyIcon添加系統托盤

來源:互聯網
上載者:User

標籤:c#   notifyicon   contextmenustrip   系統托盤   

要求:

1 程式啟動時,無系統托盤

2 程式最小化時,顯示托盤,且程式隱藏

3 雙擊系統托盤,顯示主介面,托盤隱藏

4 系統托盤右鍵,點擊顯示和退出按鈕,主程式顯示和退出

代碼;

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 SystemIcon{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e)        {        }        private void Form1_Resize(object sender, EventArgs e)        {            if (this.WindowState == FormWindowState.Minimized)            {//最小化時 顯示系統托盤,主介面隱藏                notifyIcon1.Visible = true;                this.Visible = false;            }            else            {                //隱藏系統托盤                notifyIcon1.Visible = false;            }        }        private void notifyIcon1_DoubleClick(object sender, EventArgs e)        {            this.Visible = true; //顯示主視窗            this.WindowState = FormWindowState.Normal;            this.Activate();//獲得焦點            notifyIcon1.Visible = false;//隱藏托盤        }        private void 顯示ToolStripMenuItem_Click(object sender, EventArgs e)        {            // 顯示主程式            notifyIcon1_DoubleClick(sender, e);        }        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)        {            //退出 this.Close();            Application.Exit();        }    }}

說明;

1 添加NotifyIcon 和 ContextMenuStrip

2 使用NotifyIcon的ContextMenuStrip屬性關聯ContextMenuStrip

 

C# NotifyIcon添加系統托盤

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.