LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

C#实现Winform程序在系统托盘显示图标及开机自启动

admin
2024年7月12日 17:0 本文热度 540

引言

我们在使用桌面应用程序时,经常会看到一些程序在系统托盘中显示图标,且会有一些常用功能。如QQ、钉钉、微信等,同时,这些程序在系统启动后也跟随自动启动。本文将介绍C#实现程序在系统托盘中显示图标及开机自启动的内容。

实现

1、程序在系统托盘中显示图标

NotifyIcon 控件,通常用于在系统托盘中显示图标,通过使用它就可以我们想要的效果。

常用属性

属性描述
Icon在系统托盘中显示的图标
Text鼠标悬停在图标时显示的文本
Visible指定是否可见

常用方法

方法描述
ShowContextMenu在系统托盘上下文菜单中显示指定的菜单

实现步骤

1、创建 NotifyIcon 控件并设置属性;
2、编写 NotifyIcon 响应控制事件;
3、在主窗体的Load事件中将 NotifyIcon 添加到系统托盘;
4、程序退出时,移除系统托盘的 NotifyIcon;

示例代码

using System;

using System.Windows.Forms;


namespace Fountain.WinForm.NotifyDemo

{

    public partial class FormMain : Form

    {

        /// <summary>

        /// 通知控件

        /// </summary>

        private NotifyIcon notifyIcon = new NotifyIcon();

        /// <summary>

        /// 通知控件显示菜单

        /// </summary>

        private ContextMenuStrip contextMenuStrip = new ContextMenuStrip();

        /// <summary>

        /// 构造方法

        /// </summary>

        public FormMain()

        {

            InitializeComponent();

        }

        /// <summary>

        /// 窗体加载

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void FormMain_Load(object sender, EventArgs e)

        {

            this.InitializeNotifyMenu();

            this.notifyIcon.Text = this.Text;

            this.notifyIcon.Visible = true;

            this.notifyIcon.Icon = this.Icon;

            this.notifyIcon.ContextMenuStrip = this.contextMenuStrip;

            this.notifyIcon.DoubleClick += notifyIcon_DoubleClick;

        }

        /// <summary>

        /// 托盘菜单

        /// </summary>

        private void InitializeNotifyMenu()

        {

            try

            {

                contextMenuStrip.Items.Clear();

                ToolStripMenuItem showMenuItem = new ToolStripMenuItem("显示界面");

                showMenuItem.Tag = "显示";

                showMenuItem.Click += new EventHandler(ShowMenuItem_Click);

                contextMenuStrip.Items.Add(showMenuItem);


                ToolStripMenuItem sboutMenuItem = new ToolStripMenuItem("关于");

                sboutMenuItem.Tag = "关于";

                sboutMenuItem.Click += new EventHandler(AboutMenuItem_Click);

                contextMenuStrip.Items.Add(sboutMenuItem);


                ToolStripMenuItem exitMenuItem = new ToolStripMenuItem("退出");

                exitMenuItem.Tag = "退出";

                exitMenuItem.Click += new EventHandler(ExistMenuItem_Click);

                contextMenuStrip.Items.Add(exitMenuItem);

            }

            catch(Exception exception) 

            {

                throw new Exception(exception.Message);

            }

        }

        /// <summary>

        /// 右击任务栏图标

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void notifyIcon_DoubleClick(object sender, EventArgs e)

        {

            try

            {

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

                }

            }

            catch (Exception objException)

            {

                throw new Exception(objException.Message);

            }

        }

        /// <summary>

        /// 显示

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void ShowMenuItem_Click(object sender, EventArgs e)

        {

            try

            {

                this.Show();

                this.WindowState = FormWindowState.Normal;

                this.Activate();

            }

            catch (Exception objException)

            {

                throw new Exception(objException.Message);

            }

        }

        /// <summary>

        /// 关于

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void AboutMenuItem_Click(object sender, EventArgs e)

        {

            try

            {

            }

            catch (Exception objException)

            {

                MessageBox.Show(objException.Message);

            }

        }

        /// <summary>

        /// 退出

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void ExistMenuItem_Click(object sender, EventArgs e)

        {

            try

            {

                if (MessageBox.Show("你确定要退出程序吗?","提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)

                {

                    this.notifyIcon.Visible = false;

                    this.notifyIcon.Dispose();

                    this.Dispose();

                    Application.Exit();

                }

            }

            catch (Exception objException)

            {

                MessageBox.Show(objException.Message);

            }

        }

        /// <summary>

        /// 主窗体关闭

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)

        {

            try

            {

                e.Cancel = true;

                this.Hide();

                this.notifyIcon.Dispose();

            }

            catch (Exception objException)

            {

                MessageBox.Show(objException.Message);

            }

        }

        /// <summary>

        /// 窗体大小变化

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void FormMain_Resize(object sender, EventArgs e)

        {

            if (this.WindowState == FormWindowState.Minimized)

            {

                this.ShowInTaskbar = false;

                this.Hide();

                this.notifyIcon.Visible = true;

            }

        }

    }

}

2、系统开机自启动应用程序

通过将应用程序往注册表开机启动项,实现开机自启动。下面通过示例介绍其实现。

using Microsoft.Win32;

using System;

using System.Diagnostics;

using System.Windows.Forms;


namespace Fountain.WinForm.NotifyDemo

{

    public partial class FormMain : Form

    {

        /// <summary>

        /// 构造方法

        /// </summary>

        public FormMain()

        {

            InitializeComponent();

        }

        /// <summary>

        /// 窗体加载

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void FormMain_Load(object sender, EventArgs e)

        { 

            string applictionName = Process.GetCurrentProcess().MainModule.ModuleName;

            string applictionPath = Process.GetCurrentProcess().MainModule.FileName;

            #region 当前登陆用户的注册表启动项

            RegistryKey registryKey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");

            registryKey.SetValue(applictionName, applictionPath);

            #endregion

            #region 所有用户的注册表启动项

            //RegistryKey registryKey = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");

            //registryKey.SetValue(applictionName, applictionPath);

            #endregion

        }

    }

}

小结

以上是C#实现系统托盘显示应用程序图标与应用程序开机自启动的实现案例。


该文章在 2024/7/12 17:04:08 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved