用 C# 编写一个停放在任务栏上的图标程序

来源:互联网 发布:三星手机官方软件 编辑:程序博客网 时间:2024/04/18 19:40

很简单,就不用说明了。

 

Form1.Designer.cs

 

namespace NotifyIconApplication
{
    partial class Form1
    {
        /// <summary>
        /// 設計工具所需的變數。
        /// </summary>
        private System.ComponentModel.IContainer components = null;
        private bool IsShutDown = false;

        /// <summary>
        /// 清除任何使用中的資源。
        /// </summary>
        /// <param name="disposing">如果應該公開 Managed 資源則為 true,否則為 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (IsShutDown)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
        }

        #region Windows Form 設計工具產生的程式碼

        /// <summary>
        /// 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器修改這個方法的內容。
        ///
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.button1 = new System.Windows.Forms.Button();
            this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(244, 172);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "隐藏窗体";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // notifyIcon1
            //
            this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
            this.notifyIcon1.Text = "这是我的测试程序,还Cool吧!";
            this.notifyIcon1.Visible = true;
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(198, 115);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(175, 13);
            this.label1.TabIndex = 1;
            this.label1.Text = "本程序使用Visual Studio .Net编写";
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(566, 377);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.NotifyIcon notifyIcon1;
        private System.Windows.Forms.Label label1;
    }
}

 

 

后台代码页 Form1.cs

 

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

namespace NotifyIconApplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //生成4个菜单项对象,显示文本分别为"显示窗口"、"隐藏窗口"、"执行程序"、"退出程序"
            MenuItem menuItem1 = new MenuItem("显示窗口");

            MenuItem menuItem2 = new MenuItem("隐藏窗口");

            MenuItem menuItem3 = new MenuItem("执行程序");

            MenuItem menuItem4 = new MenuItem("退出程序");

            //分别为4个菜单项添加Click事件响应函数
            menuItem1.Click += new System.EventHandler(this.menuItem1_Click);

            menuItem2.Click += new System.EventHandler(this.menuItem2_Click);

            menuItem3.Click += new System.EventHandler(this.menuItem3_Click);

            menuItem4.Click += new System.EventHandler(this.menuItem4_Click);

            //设置NotifyIcon对象的ContextMenu属性为生面的弹出菜单对象
            notifyIcon1.ContextMenu = new ContextMenu(new MenuItem[] { menuItem1, menuItem2, menuItem3, menuItem4 });

            //当用户双击程序图标时将执行相应的函数
            notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon_DBClick);

            this.FormClosing+=new FormClosingEventHandler(Form1_FormClosing);

            this.Activated+=new EventHandler(Form1_Activated);
        }

        private void menuItem1_Click(object sender, System.EventArgs e)//“显示窗口”菜单的响应方法
        {

            this.Activate();//将窗口设定为当前活动窗口

            //假如当前窗口没显示则显示当前窗口
            if (this.Visible == false) this.Visible = true;

            // //如果当前主窗口为最小化状态,将状态调整为Normal状态
            if (this.WindowState == FormWindowState.Minimized) this.WindowState = FormWindowState.Normal;

        }

        private void menuItem2_Click(object sender, System.EventArgs e)//"隐藏窗口"菜单的响应方法
        {

            if (this.Visible == true) this.Visible = false;//假如当前窗口为显示的则隐藏窗口

        }

        private void menuItem3_Click(object sender, System.EventArgs e)//"执行程序"菜单的响应方法
        {

            //显示一个提示信息框,表示事件已经得到响应

            MessageBox.Show("演示程序已经执行,此处功能就是显示一个提示框!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

        private void menuItem4_Click(object sender, System.EventArgs e)//“退出程序”菜单的响应方法
        {

            this.Close();//关闭当前对象(即窗体)

            this.IsShutDown = true;

            Application.Exit();//通过Application类的静态方法Exit()退出应用程序

        }

        private void button1_Click(object sender, System.EventArgs e)//用户单击按钮时的响应方法
        {

            this.Visible = false;//隐藏当前主窗口

        }

        private void notifyIcon_DBClick(object sender, System.EventArgs e)//用户双击应用程序图标进的响应方法
        {

            this.Activate();//将窗口设定为当前活动窗口

           this.Visible = true;//显示当前主窗口

            //如果当前主窗口为最小化状态,将状态调整为Normal状态
            if (this.WindowState == FormWindowState.Minimized) this.WindowState = FormWindowState.Normal;

        }

        private void Form1_FormClosing(object sender, System.EventArgs e)
        {
            this.IsShutDown = false;
            this.Visible = false;
        }

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

 

 

Program.cs

 

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace NotifyIconApplication
{
    static class Program
    {
        /// <summary>
        /// 應用程式的主要進入點。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

 

 

原创粉丝点击