c# 托盘程序

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

namespace test
{
    public partial class Form1 : Form
    {
        public Form1()  //过早函数
        {
            InitializeComponent();


            /// 初始化推盘程序参数
            this.notifyIconServer.ContextMenuStrip = this.txtMenu;  //托盘关联的菜单
            this.notifyIconServer.Text = "测试托盘程序";
            this.notifyIconServer.Icon = new Icon(Application.StartupPath + @"/tray.ico");
            this.notifyIconServer.Visible = false;  //隐藏托盘

            this.ShowInTaskbar = false; //设置不再任务栏中显示图标
        }
        /// <summary>
        /// 是否关闭窗口
        /// </summary>
        private bool IsClose = false;
        /// <summary>
        /// 托盘图像
        /// </summary>
        private NotifyIcon notifyIconServer = new NotifyIcon();

       
        /// <summary>
        /// 获取或设置是否关闭主窗体
        /// </summary>
        private bool getIsColse
        {
            set { this.IsClose = value; }
            get { return this.IsClose; }
        }
        /// <summary>
        /// 快捷菜单打开事件
        /// </summary>
        private void menuOpen_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized; //指定窗体最大化
        }
        /// <summary>
        /// 快捷菜单关闭事件
        /// </summary>
        private void mentClose_Click(object sender, EventArgs e)
        {
            this.getIsColse = true;
            this.Close();
        }

        /// <summary>
        /// 窗体大小改变时激发的事件
        /// </summary>
        private void Form1_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Maximized)
            {
                this.notifyIconServer.Visible = false; //隐藏托盘
            }
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.notifyIconServer.Visible = true;  //显示托盘
            }
        }

        /// <summary>
        /// 窗体关闭前激发的事件
        /// </summary>
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.getIsColse)
            {
                this.notifyIconServer.Visible = false;
            }
            else
            {
                e.Cancel = true;
                this.WindowState = FormWindowState.Minimized;
                this.notifyIconServer.Visible = true;
            }
                  
        }

    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值