C# winForm启动最小化到任务栏右侧通知栏并交互操作

原文链接:http://blog.sina.com.cn/s/blog_45eaa01a01013u36.html

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();
            }

        }
    }
}

posted @ 2016-06-21 16:14  Dukezhou  阅读(8740)  评论(1编辑  收藏  举报