对于使.NET程序随系统开机启动,最常用的可能就是向在注册表中注册开机启动项,或是建立Windows服务,使程序随系统启动而启动。
这里以WinForm程序为例,测试Demo分享,同时附上对于程序托盘的简单操作,仿Foxmail的托盘显示效果,好的产品都是细细打磨出来的呀!
1.WinForm程序添加注册表和移除注册表
(1).创建一个WinForm程序,界面如下(很简单):
(2).主要实现代码:(注册表开机启动位置:SOFTWARE\Microsoft\Windows\CurrentVersion\Run)
/// <summary>
/// 根据注册名称和路径判断是否已注册
/// </summary>
/// <param name="regName">注册名称</param>
/// <param name="startUpPath">注册路径</param>
private void IsExistsStartRun(string regName, string startUpPath)
{
string strMessage = "注册已移除!";
RegistryKey loca = Registry.LocalMachine;
RegistryKey run = loca.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");object obj = run.GetValue(regName);
if (obj != null)
{
strMessage = "注册已添加!";
ControlBtnEnable(false);
}
else
{
ControlBtnEnable(true);
}
lblInitMsg.Text += strMessage;
}/// <summary>
/// 根据注册名和注册路径,更新注册表信息
/// </summary>
/// <param name="regName">注册名</param>
/// <param name="startUpPath">注册路径</param>
/// <param name="isAddReg">是否添加</param>
private void RegCompStartRun(string regName, string startUpPath, bool isAddReg)
{
//表示Window注册表中项级节点,读取 Windows 注册表基项HKEY_LOCAL_MACHINE
RegistryKey localMachine = Registry.LocalMachine;
RegistryKey run = localMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");try
{
//SetValue:存储值的名称
if (isAddReg)
{
run.SetValue(regName, startUpPath);//加入注册,参数一为注册节点名称(随意)
}
else
{
if (run.GetValue(regName) != null)//存在则移除
{
run.DeleteValue(regName, false);//删除该注册节点
}
}
localMachine.Close();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
重新将改程序注册到注册表中,重启电脑,应该就可以了。博客最后是完整代码实现。
2.程序托盘显示控制
(1).在WinForm程序控件库找到notifyIcon控件,将其加到WinForm程序上面,仿Foxmail的托盘程序控制。
随系统自动启动,最小化为托盘在底部显示;单击托盘图标,显示主体程序,继续单击显示和隐藏;单击主窗体关闭按钮,放到托盘显示,右键退出系统,关闭主体程序。
(2).主要实现代码:
/// <summary>
/// 初始化消息图标的右键菜单
/// </summary>
private void InitNotifyicon()
{
List<MenuItem> listMenu = new List<MenuItem>();
listMenu.Add(new MenuItem("显示主窗口", new EventHandler(this.notifyIcon_ShowFrm)));
listMenu.Add(new MenuItem("-"));
listMenu.Add(new MenuItem("最小化系统", new EventHandler(this.notifyIcon_Minimzed)));
listMenu.Add(new MenuItem("-"));
listMenu.Add(new MenuItem("退出系统", new EventHandler(this.notifyIcon_ExitShow)));this.notifyIcon1.ContextMenu = new ContextMenu(listMenu.ToArray());//消息托盘菜单
}/// <summary>
/// 根据是否显示窗体和是否最小化,控制窗体显示
/// </summary>
/// <param name="isFrmShow">是否显示主窗体</param>
/// <param name="isMinShow">是否为最小化</param>
private void IsShowMainFrm(bool isFrmShow, bool isMinShow)
{
if (!isMinShow)
{
this.Visible = isFrmShow;//显示在屏幕
this.ShowInTaskbar = isFrmShow;//显示在任务栏
this.WindowState = FormWindowState.Normal;
isShowFrm = !isFrmShow;//相反设置
}
else
{
this.Visible = isMinShow;//主窗体可见
this.ShowInTaskbar = isMinShow;//在任务栏显示
isShowFrm = !isFrmShow;//是否显示主窗体控制
this.WindowState = isFrmShow == true ? FormWindowState.Normal : FormWindowState.Minimized;
}
this.isShowMin = isMinShow;//显示最小化控制
}
3.结合对注册表的操作实现随着系统启动自动启动程序;默认显示为托盘图标显示在底部以及一些显示隐藏控制,仿Foxmail的完成代码实现:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;namespace WinFormAutoStart
{
public partial class Form1 : Form
{
private bool isShowMin = false;//默认为最小化
private bool isShowFrm = false;//默认不显示主窗体
private string regName = "TestWinForm";//注册表对应的Name
private string startUpPath = Application.ExecutablePath;//程序执行路径public Form1()
{
InitializeComponent();
}private void Form1_Load(object sender, EventArgs e)
{
//注册表操作
IsExistsStartRun(regName, startUpPath);//托盘消息操作
this.Visible = false;//不可见
this.ShowInTaskbar = false;//不显示在系统任务S栏
this.notifyIcon1.Visible = true;
InitNotifyicon();//添加消息图标右键菜单
}#region 注册表相关操作
/// <summary>
/// 控制按钮是否可用
/// </summary>
/// <param name="isAdd">标识是否为添加注册操作</param>
private void ControlBtnEnable(bool isAdd)
{
if (isAdd)
{
this.btnAddReg.Enabled = true;
this.btnDeleteReg.Enabled = false;
}
else
{
this.btnAddReg.Enabled = false;
this.btnDeleteReg.Enabled = true;
}
}/// <summary>
/// 根据注册名称和路径判断是否已注册
/// </summary>
/// <param name="regName">注册名称</param>
/// <param name="startUpPath">注册路径</param>
private void IsExistsStartRun(string regName, string startUpPath)
{
string strMessage = "注册已移除!";
RegistryKey loca = Registry.LocalMachine;
RegistryKey run = loca.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");object obj = run.GetValue(regName);
if (obj != null)
{
strMessage = "注册已添加!";
ControlBtnEnable(false);
}
else
{
ControlBtnEnable(true);
}
lblInitMsg.Text += strMessage;
}/// <summary>
/// 根据注册名和注册路径,更新注册表信息
/// </summary>
/// <param name="regName">注册名</param>
/// <param name="startUpPath">注册路径</param>
/// <param name="isAddReg">是否添加</param>
private void RegCompStartRun(string regName, string startUpPath, bool isAddReg)
{
//表示Window注册表中项级节点,读取 Windows 注册表基项HKEY_LOCAL_MACHINE
RegistryKey localMachine = Registry.LocalMachine;
RegistryKey run = localMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");try
{
//SetValue:存储值的名称
if (isAddReg)
{
run.SetValue(regName, startUpPath);//加入注册,参数一为注册节点名称(随意)
}
else
{
if (run.GetValue(regName) != null)//存在则移除
{
run.DeleteValue(regName, false);//删除该注册节点
}
}
localMachine.Close();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}/// <summary>
/// 添加到注册表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnAddReg_Click(object sender, EventArgs e)
{
//注册 WinForm程序开机启动
string strMessage = "添加程序注册成功!";
RegCompStartRun(regName, startUpPath, true);
lblMessage.Text = strMessage;
ControlBtnEnable(false);
MessageBox.Show(strMessage);
}/// <summary>
/// 从注册表移除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDeleteReg_Click(object sender, EventArgs e)
{
//注册 WinForm程序开机启动
string strMessage = "移除程序注册成功!";
RegCompStartRun(regName, startUpPath, false);
lblMessage.Text = strMessage;
ControlBtnEnable(true);
MessageBox.Show(strMessage);
}
#endregion#region 消息图标相关操作
/// <summary>
/// 初始化消息图标的右键菜单
/// </summary>
private void InitNotifyicon()
{
List<MenuItem> listMenu = new List<MenuItem>();
listMenu.Add(new MenuItem("显示主窗口", new EventHandler(this.notifyIcon_ShowFrm)));
listMenu.Add(new MenuItem("-"));
listMenu.Add(new MenuItem("最小化系统", new EventHandler(this.notifyIcon_Minimzed)));
listMenu.Add(new MenuItem("-"));
listMenu.Add(new MenuItem("退出系统", new EventHandler(this.notifyIcon_ExitShow)));this.notifyIcon1.ContextMenu = new ContextMenu(listMenu.ToArray());//消息托盘菜单
}/// <summary>
/// 根据是否显示窗体和是否最小化,控制窗体显示
/// </summary>
/// <param name="isFrmShow">是否显示主窗体</param>
/// <param name="isMinShow">是否为最小化</param>
private void IsShowMainFrm(bool isFrmShow, bool isMinShow)
{
if (!isMinShow)
{
this.Visible = isFrmShow;//显示在屏幕
this.ShowInTaskbar = isFrmShow;//显示在任务栏
this.WindowState = FormWindowState.Normal;
isShowFrm = !isFrmShow;//相反设置
}
else
{
this.Visible = isMinShow;//主窗体可见
this.ShowInTaskbar = isMinShow;//在任务栏显示
isShowFrm = !isFrmShow;//是否显示主窗体控制
this.WindowState = isFrmShow == true ? FormWindowState.Normal : FormWindowState.Minimized;
}
this.isShowMin = isMinShow;//显示最小化控制
}/// <summary>
/// 右键菜单显示主窗体事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void notifyIcon_ShowFrm(object sender, System.EventArgs e)
{
IsShowMainFrm(true, false);
}/// <summary>
/// 右键菜单退出系统事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void notifyIcon_ExitShow(object sender, EventArgs e)
{
notifyIcon1.Visible = false;//图标可不见
notifyIcon1.Dispose();
this.Close();//关闭窗体
this.Dispose();//释放资源
}/// <summary>
/// 右键菜单最小化事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void notifyIcon_Minimzed(object sender, EventArgs e)
{
IsShowMainFrm(false, true);
}/// <summary>
/// 监视窗体大小改变事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
IsShowMainFrm(false, true);
}
}/// <summary>
/// 窗体正在关闭事件
/// 自动放置到托盘
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
IsShowMainFrm(false, false);
e.Cancel = true;//取消关闭操作
}/// <summary>
/// 消息点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
IsShowMainFrm(isShowFrm, isShowMin);//控制主窗体是否显示
}
#endregion
}
}
4.效果图如下:
.Net程序随系统开机启动(仿Foxmail托盘效果控制)