.net form案例大全
WinForm 自动更新程序
自定义组件
跑马灯效果Label
TextBox横线样式及占位提示
监控变量值改变
Visual Studio 2022如何支持低版本.NET框架
Winform 支持高DPI的几种方式
C#实现程序在系统托盘显示图标及开机自启动
VS2022 工具箱中不显示 Devexpress控件的问题
进程间通信
两个exe程序之间通信
管道通信
日志写入到RichTextBox中展示
多线程访问WinForms控件的方法
多线程
线程锁与单多线程简单使用
本文档使用 MrDoc 发布
-
+
首页
C#实现程序在系统托盘显示图标及开机自启动
# 引言 我们在使用桌面应用程序时,经常会看到一些程序在系统托盘中显示图标,且会有一些常用功能。如QQ、钉钉、微信等,同时,这些程序在系统启动后也跟随自动启动。本文将介绍C#实现程序在系统托盘中显示图标及开机自启动的内容。 # 实现 **1、程序在系统托盘中显示图标** NotifyIcon 控件,通常用于在系统托盘中显示图标,通过使用它就可以我们想要的效果。 **常用属性** | 属性 | 描述 | 其他 | | --- | --- | --- | |Icon |在系统托盘中显示的图标 | | |Text | 鼠标悬停在图标时显示的文本 | | |Visible |指定是否可见 | | **常用方法** | 属性 | 描述 | 其他 | | --- | --- | --- | | ShowContextMenu | 在系统托盘上下文菜单中显示指定的菜单 | --- | **实现步骤** 1、创建 NotifyIcon 控件并设置属性; 2、编写 NotifyIcon 响应控制事件; 3、在主窗体的Load事件中将 NotifyIcon 添加到系统托盘; 4、程序退出时,移除系统托盘的 NotifyIcon; ```java 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、系统开机自启动应用程序 通过将应用程序往注册表开机启动项,实现开机自启动。下面通过示例介绍其实现。 ```java 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 } } } ```
孙端己
2024年7月12日 10:58
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
分享
链接
类型
密码
更新密码