• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

VS2017-Winform简单托盘小程序

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 界面比较简单,主要两个button 一个NotifyIcon 和 右键菜单 控件,

NotifyIcon 属性,如下:

并为NotifyIcon指定了DoubleClick事件:

 

主窗体增加两个事件:

 

 

整体代码如下:

using Pallet_Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Pallet_01
{
    public partial class MainForm : Form
    {

        public MainForm()
        {
            InitializeComponent();
            this.btnEnd.Enabled = false;
            this.停止ToolStripMenuItem.Enabled = false;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.btnEnd.Enabled = false;
            this.Resize += MainForm_Resize;

        }


        //单击窗体最小化时窗体隐藏 
        void MainForm_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
            }
        }

        private void NotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Show(); // 窗体显现
            this.WindowState = FormWindowState.Normal; //窗体回复正常大小
        }

        private void btnStart_Click(object sender, EventArgs e)
        {

            Scheduler.StartUp();
            this.btnStart.Enabled = false;
            this.btnEnd.Enabled = true;
            this.启动ToolStripMenuItem.Enabled = false;
            this.停止ToolStripMenuItem.Enabled = true;

            string pageSize = ConfigurationManager.AppSettings["PageSize"];
            this.lblCount.Text = pageSize + "";

            this.lblCount.Update();

        }

        private void btnEnd_Click(object sender, EventArgs e)
        {

            Scheduler.Stop();
            this.btnStart.Enabled = true;
            this.btnEnd.Enabled = false;
            this.启动ToolStripMenuItem.Enabled = true;
            this.停止ToolStripMenuItem.Enabled = false;

            this.lblCount.Text = "----";
            this.lblCount.Update();
        }


        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("是否确认退出程序?", "退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                //关闭所有的线程,释放占用内存
                this.Dispose();
                this.Close();
            }
            else
            {
                e.Cancel = true;
            }
        }

        #region 右键菜单事件
        private void 启动ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Scheduler.StartUp();
            this.btnStart.Enabled = false;
            this.btnEnd.Enabled = true;
            this.启动ToolStripMenuItem.Enabled = false;
            this.停止ToolStripMenuItem.Enabled = true;
        }

        private void 停止ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Scheduler.Stop();
            this.btnStart.Enabled = true;
            this.btnEnd.Enabled = false;
            this.启动ToolStripMenuItem.Enabled = true;
            this.停止ToolStripMenuItem.Enabled = false;
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否确认退出程序?", "退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                // 关闭所有的线程,释放占用内存
                this.Dispose();
                this.Close();
            }
        }
        #endregion
    }
}

 

其中打开一个程序后,不允许再次打开,也就是运行后,只能运行一个,如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Pallet_01
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            bool bCreateNew;
            Mutex m = new Mutex(false, "Pallent_01", out bCreateNew);
            if (bCreateNew)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new MainForm());
            }
        }
    }
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap