以下是 添加到托盘、添加到开机启动,以及全局热键(快捷键)的实现方法:
目录
添加到托盘:
//只需要把NotifyIcon控件拖入到窗体中即可。
/// <summary>
/// 关闭
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ActInfoForm_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
ActInfo.Visible = true;
this.Hide();
this.ShowInTaskbar = false;
ActInfo.Dispose();//释放资源
}
/// <summary>
/// 双击托盘继续显示
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void notifyIcon_DoubleClick(object sender, EventArgs e)
{
if(this.ShowInTaskbar==false)
{
ActInfo.Visible = false;
this.Show();
this.Activate();
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = true;
//InitializeComponent();//继续显示
}
}
/// <summary>
/// 最小化
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ActInfoForm_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.ShowInTaskbar = false;
this.Hide();
ActInfo.Visible = true;
}
}
添加到开机启动:
第一种方法:开始菜单启动:
//需要添加引用,从 Com 中搜索:Windows Script Host Object Model 添加即可。
public static bool Create(string directory, string shortcutName, string targetPath,
string description = null, string iconLocation = null)
{
try
{
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
//添加引用 Com 中搜索 Windows Script Host Object Model
string shortcutPath = Path.Combine(directory, string.Format("{0}.lnk", shortcutName));
IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell();
IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutPath);//创建快捷方式对象
shortcut.TargetPath = targetPath;//指定目标路径
shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath);//设置起始位置
shortcut.WindowStyle = 1;//设置运行方式,默认为常规窗口
shortcut.Description = description;//设置备注
shortcut.IconLocation = string.IsNullOrWhiteSpace(iconLocation) ? targetPath : iconLocation;//设置图标路径
shortcut.Save();//保存快捷方式
return true;
}
catch
{ }
return false;
}
第二种方法:注册表开机启动项:
// 添加到 当前登陆用户的 注册表启动项
RegistryKey RKey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
RKey.SetValue("AppName", @"C:\AppName.exe");
// 添加到 所有用户的 注册表启动项
RegistryKey RKey = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
RKey.SetValue("AppName", @"C:\AppName.exe");
// @ 符号中的双引号需要用两次来转义
RKey.SetValue("AppName", @"""C:\AppName.exe""");
第三种方法:Windows计划任务方式启动:
//新建任务
TaskSchedulerClass scheduler = new TaskSchedulerClass();
//连接
scheduler.Connect(null, null, null, null);
//获取创建任务的目录
ITaskFolder folder = scheduler.GetFolder("\\");
//设置参数
ITaskDefinition task = scheduler.NewTask(0);
task.RegistrationInfo.Author = author;//创建者
task.RegistrationInfo.Description = desc;//描述
//设置触发机制(此处是 登陆后)
task.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON);
//设置动作(此处为运行exe程序)
IExecAction action = (IExecAction)task.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC);
action.Path = file;//设置文件目录
task.Settings.ExecutionTimeLimit = "PT0S"; //运行任务时间超时停止任务吗? PTOS 不开启超时
task.Settings.DisallowStartIfOnBatteries = false;//只有在交流电源下才执行
task.Settings.RunOnlyIfIdle = false;//仅当计算机空闲下才执行
IRegisteredTask regTask =
folder.RegisterTaskDefinition(name, task,//此处需要设置任务的名称(name)
(int)_TASK_CREATION.TASK_CREATE, null, //user
null, // password
_TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN,
"");
IRunningTask runTask = regTask.Run(null);
全局热键:
using System;
using System.Runtime.InteropServices;
namespace enter
{
class HotKey
{
//如果函数执行成功,返回值不为0。
//如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(
IntPtr hWnd, //要定义热键的窗口的句柄
int id, //定义热键ID(不能与其它ID重复)
KeyModifiers fsModifiers, //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效
System.Windows.Forms.Keys vk //定义热键的内容
);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd, //要取消热键的窗口的句柄
int id //要取消热键的ID
);
//定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值)
[Flags()]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Ctrl = 2,
Shift = 4,
WindowsKey = 8
}
}
}
原文链接:
C# 关于托盘的应用_蘑菇色的小象的博客-CSDN博客_c# 托盘