C# winForm啓動最小化到任務欄右側通知欄並交互操作




一。主要功能:
(1)、程序啓動自動隱藏到任務欄右側通知欄顯示。(與系統托盤同義)
(2)、雙擊系統托盤圖標顯示、隱藏窗口;
(3)、右擊系統托盤圖標提供三個菜單選項,“退出”、“隱藏”、“顯示”;

二。相關控件:
1、建一個WinForm程序—IconForm,將Form屬性ShowInTaskbar改爲false,這樣程序將不會在任務欄中顯示。
2、將Form屬性WindowState選擇爲 Minimized,以便起來自動最小化隱藏
3、在工具欄中的“公共控件”裏,拖入NotifyIcon控件—notifyIcon1,這個是程序運行任務欄右側通知區域圖標顯示控件,爲控件notifyIcon的屬性Icon添加一個icon圖標,或從代碼中加入。
4、在工具欄中的“菜單和工具欄”裏,拖入ContextMenuStrip—contextMenuStrip1,這個控件是右擊時關聯菜單。
5、右鍵notifyIcon1選擇屬性,將其屬性ContextMenuStrip改加爲contextMenuStrip1,這個時候notifyIcon1和contextMenuStrip1兩個控件就關聯了。
6、右鍵contextMenuStrip1,選擇屬性,進入Items,然後點擊“添加”,這裏添加三個菜單選項:exitMenuItem、hideMenuItem、showMenuItem,同時分別將其Text屬性改爲:退出、隱藏和顯示。


三。主要代碼:
1、雙擊IconForm,即添加Load事件然後

//一 右擊窗體,選擇屬性,轉到事件頁面,雙擊 Load,SizeChanged事件,給窗體添加代碼
        private void Form1_Load(object sender, EventArgs e)
        {
         //1.將Form屬性ShowInTaskbar改爲false,這樣程序將不會在任務欄中顯示。
         //2.將Form屬性WindowState選擇爲 Minimized,以便起來自動最小化隱藏。
            string startup = Application.ExecutablePath;       //取得程序路徑   
            int pp = startup.LastIndexOf("\\");
            startup = startup.Substring(0, pp);
            string icon = startup + "\\testIcon.ico";
         //3.一定爲notifyIcon1其設置圖標,否則無法顯示在通知欄。或者在其屬性中設置
            notifyIcon1.Icon = new Icon(icon);

        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide(); //或者是this.Visible = false;
                this.notifyIcon1.Visible = true;
            }

        }

 

//二 雙擊窗體上的菜單項,添加相關代碼
        private void exitMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("你確定要退出程序嗎?", "確認", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                notifyIcon1.Visible = false;
                this.Close();
                this.Dispose();
                Application.Exit();
            }

        }

        private void hideMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();

        }

        private void showMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
            this.Activate();

        }

//三 轉到窗體設計模式,右擊notifyIcon1 ,選擇屬性,雙擊其中DoubleClick,添加相關代碼
        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Normal)
            {
                this.WindowState = FormWindowState.Minimized;
                this.Hide();
            }
            else if (this.WindowState == FormWindowState.Minimized)
            {
                this.Show();
                this.WindowState = FormWindowState.Normal;
                this.Activate();
            }

        }

 

四。完整的代碼如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace IconForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
//說明,程序運行後自動隱藏到任務欄右側的通知欄裏, 
//1        右擊選擇退出,隱藏,顯示
//2        雙擊可以隱藏和顯示切換

 //一 右擊窗體,選擇屬性,轉到事件頁面,雙擊 Load,SizeChanged事件,給窗體添加代碼
        private void Form1_Load(object sender, EventArgs e)
        {
         //1.將Form屬性ShowInTaskbar改爲false,這樣程序將不會在任務欄中顯示。
         //2.將Form屬性WindowState選擇爲 Minimized,以便起來自動最小化隱藏。
            string startup = Application.ExecutablePath;       //取得程序路徑   
            int pp = startup.LastIndexOf("\\");
            startup = startup.Substring(0, pp);
            string icon = startup + "\\testIcon.ico";
         //3.一定爲notifyIcon1其設置圖標,否則無法顯示在通知欄。或者在其屬性中設置
            notifyIcon1.Icon = new Icon(icon);

        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide(); //或者是this.Visible = false;
                this.notifyIcon1.Visible = true;
            }

        }

//二 雙擊窗體上的菜單項,添加相關代碼
        private void exitMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("你確定要退出程序嗎?", "確認", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                notifyIcon1.Visible = false;
                this.Close();
                this.Dispose();
                Application.Exit();
            }

        }

        private void hideMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void showMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
            this.Activate();

        }

//三 轉到窗體設計模式,右擊notifyIcon1 ,選擇屬性,雙擊其中DoubleClick,添加相關代碼
        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Normal)
            {
                this.WindowState = FormWindowState.Minimized;

                this.Hide();
            }
            else if (this.WindowState == FormWindowState.Minimized)
            {
                this.Show();
                this.WindowState = FormWindowState.Normal;
                this.Activate();
            }

        }
    }
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章