系統托盤編程

來源:互聯網
上載者:User

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

namespace TrayIcon
{
    public partial class Form1 : Form
    {
        private Icon ico = new Icon("..\\..\\qq.ico");
        private NotifyIcon trayIcon;
        private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
        private System.Windows.Forms.ToolStripMenuItem showMainFrameToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem;

        public Form1()
        {
            InitializeComponent();

            InitContextMenu();

            this.ShowInTaskbar = false;   //以下兩句用來使表單在啟動時不顯示
            this.WindowState = FormWindowState.Minimized;
            InitTrayIcon();
        }

        //private void Form1_Activated(object sender, EventArgs e)
        //{
        //    //this.Show();  //也可以用這句使表單在啟動時不顯示
        //}

        private void InitTrayIcon()
        {
            trayIcon = new NotifyIcon();
            trayIcon.Icon = ico;
            trayIcon.Text = "托盤程式";
            trayIcon.Visible = true;
            //trayIcon.Click += new EventHandler(trayIcon_Click);
            trayIcon.DoubleClick += new EventHandler(trayIcon_DoubleClick);
            trayIcon.ContextMenuStrip= contextMenuStrip1;
        }

        void trayIcon_DoubleClick(object sender, EventArgs e)
        {
            // Show the form when the user double clicks on the notify icon.

            // Set the WindowState to normal if the form is minimized.
            if (this.WindowState == FormWindowState.Minimized)
                this.WindowState = FormWindowState.Normal;

            // Activate the form.
            this.Activate();
            //throw new Exception("The method or operation is not implemented.");
        }

        //void trayIcon_Click(object sender, EventArgs e)
        //{
        //    MessageBox.Show("托盤程式");
        //    //throw new Exception("The method or operation is not implemented.");
        //}

        private void showMainFrameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Activate();
            this.Show();
            MessageBox.Show("!!!!");
        }

        private void exitToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            trayIcon.Visible = false;
            this.Close();
            Application.Exit();
        }

        private void InitContextMenu()
        {
            //
            // contextMenuStrip1
            //
            this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.showMainFrameToolStripMenuItem,
            this.exitToolStripMenuItem});
            this.contextMenuStrip1.Name = "contextMenuStrip1";
            this.contextMenuStrip1.Size = new System.Drawing.Size(153, 70);
            //
            // showMainFrameToolStripMenuItem
            //
            this.showMainFrameToolStripMenuItem.Name = "showMainFrameToolStripMenuItem";
            this.showMainFrameToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.showMainFrameToolStripMenuItem.Text = "ShowMainFrame";
            this.showMainFrameToolStripMenuItem.Click += new System.EventHandler(this.showMainFrameToolStripMenuItem_Click);
            //
            // exitToolStripMenuItem
            //
            this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
            this.exitToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.exitToolStripMenuItem.Text = "Exit";
            this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click_1);
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            //if (this.WindowState == FormWindowState.Minimized)
            //{
            //    this.Hide();           //不知道為什麼不能加這個,加這個以後,雙擊時視窗便不能得新顯示。

            //}
        }

    }
}

聯繫我們

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