码迷,mamicode.com
首页 > Windows程序 > 详细

C# NotifyIcon添加系统托盘

时间:2015-01-09 22:28:33      阅读:252      评论:0      收藏:0      [点我收藏+]

标签: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添加系统托盘

标签:c#   notifyicon   contextmenustrip   系统托盘   

原文地址:http://blog.csdn.net/taoerit/article/details/42560777

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!