8 Commits

Author SHA1 Message Date
ddc54d9a97 1. 发布 版本v1.0.0.0
2. 取消 管理员权限请求
3. 修改 自启动实现方法,现在使用快捷方式发布到启动目录的方式实现
4. 增加 快捷方式自启动实现方法帮助类
2021-04-29 14:11:26 +08:00
筱傑
c7ac1341cc 清理代码 2021-04-29 13:17:26 +08:00
077b8b1e13 1. 实现 开机自启动功能
2. 修改 启动时请求管理员权限(为了修改注册表设置开机自启动)
3. 修改 运行截图
4. 修改 版本到v0.1.2
2021-04-29 11:40:40 +08:00
7acb1d483d 1. 增加 全局异常捕获 2021-04-29 11:04:03 +08:00
a0ac2e988e 1. 增加 应用配置帮助类
2. 修复 唤出KeyGo本身无效问题
3. 修复 重复启动无反应问题
4. 实现 切换关闭动作功能
5. 修改 版本到v0.1.1.0
2021-04-29 10:28:30 +08:00
筱傑
d63d344e7b Merge branch 'master' of https://github.com/jie65535/KeyGo 2021-04-28 22:39:30 +08:00
筱傑
2ea8cbf725 1. 增加 删除热键前询问 2021-04-28 22:39:09 +08:00
筱傑
01e218559a Update README.md
1. 修复 图片路径中的斜杠错误
2021-04-28 22:36:12 +08:00
15 changed files with 564 additions and 101 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 15 KiB

178
KeyGo/AppAutoStart.cs Normal file
View File

@@ -0,0 +1,178 @@
using System;
using System.Collections.Generic;
using System.IO;
using IWshRuntimeLibrary;
using System.Diagnostics;
namespace KeyGo
{
/// <summary>
/// 摘自https://blog.csdn.net/liyu3519/article/details/81257839
/// 应用开机自启动帮助类
/// </summary>
static class AppAutoStart
{
/// <summary>
/// 快捷方式名称-任意自定义
/// </summary>
private static string QuickName { get; }
/// <summary>
/// 自动获取系统自动启动目录
/// </summary>
private static string SystemStartPath { get; }
/// <summary>
/// 自动获取程序完整路径
/// </summary>
private static string AppAllPath { get; }
static AppAutoStart()
{
var thisProcess = Process.GetCurrentProcess();
QuickName = thisProcess.ProcessName;
AppAllPath = thisProcess.MainModule.FileName;
SystemStartPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
Console.WriteLine(SystemStartPath);
}
/// <summary>
/// 设置开机自动启动-只需要调用改方法就可以了参数里面的bool变量是控制开机启动的开关的默认为开启自启启动
/// </summary>
/// <param name="onOff">自启开关</param>
public static void SetMeAutoStart(bool onOff = true)
{
if (onOff)//开机启动
{
//获取启动路径应用程序快捷方式的路径集合
List<string> shortcutPaths = GetQuickFromFolder(SystemStartPath, AppAllPath);
//存在2个以快捷方式则保留一个快捷方式-避免重复多于
if (shortcutPaths.Count >= 2)
{
for (int i = 1; i < shortcutPaths.Count; i++)
{
DeleteFile(shortcutPaths[i]);
}
}
else if (shortcutPaths.Count < 1)//不存在则创建快捷方式
{
CreateShortcut(SystemStartPath, QuickName, AppAllPath);
}
}
else//开机不启动
{
//获取启动路径应用程序快捷方式的路径集合
List<string> shortcutPaths = GetQuickFromFolder(SystemStartPath, AppAllPath);
//存在快捷方式则遍历全部删除
if (shortcutPaths.Count > 0)
{
for (int i = 0; i < shortcutPaths.Count; i++)
{
DeleteFile(shortcutPaths[i]);
}
}
}
}
/// <summary>
/// 向目标路径创建指定文件的快捷方式
/// </summary>
/// <param name="directory">目标目录</param>
/// <param name="shortcutName">快捷方式名字</param>
/// <param name="targetPath">文件完全路径</param>
/// <param name="description">描述</param>
/// <param name="iconLocation">图标地址</param>
/// <returns>成功或失败</returns>
private static bool CreateShortcut(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, $"{shortcutName}.lnk"); //合成路径
WshShell shell = new WshShell();
IWshShortcut shortcut = (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 (Exception ex)
{
string temp = ex.Message;
temp = "";
}
return false;
}
/// <summary>
/// 获取指定文件夹下指定应用程序的快捷方式路径集合
/// </summary>
/// <param name="directory">文件夹</param>
/// <param name="targetPath">目标应用程序路径</param>
/// <returns>目标应用程序的快捷方式</returns>
private static List<string> GetQuickFromFolder(string directory, string targetPath)
{
List<string> tempStrs = new List<string>();
tempStrs.Clear();
string tempStr = null;
string[] files = Directory.GetFiles(directory, "*.lnk");
if (files == null || files.Length < 1)
{
return tempStrs;
}
for (int i = 0; i < files.Length; i++)
{
//files[i] = string.Format("{0}\\{1}", directory, files[i]);
tempStr = GetAppPathFromQuick(files[i]);
if (tempStr == targetPath)
{
tempStrs.Add(files[i]);
}
}
return tempStrs;
}
/// <summary>
/// 获取快捷方式的目标文件路径-用于判断是否已经开启了自动启动
/// </summary>
/// <param name="shortcutPath"></param>
/// <returns></returns>
private static string GetAppPathFromQuick(string shortcutPath)
{
//快捷方式文件的路径 = @"d:\Test.lnk";
if (System.IO.File.Exists(shortcutPath))
{
WshShell shell = new WshShell();
IWshShortcut shortct = (IWshShortcut)shell.CreateShortcut(shortcutPath);
//快捷方式文件指向的路径.Text = 当前快捷方式文件IWshShortcut类.TargetPath;
//快捷方式文件指向的目标目录.Text = 当前快捷方式文件IWshShortcut类.WorkingDirectory;
return shortct.TargetPath;
}
else
{
return "";
}
}
/// <summary>
/// 根据路径删除文件-用于取消自启时从计算机自启目录删除程序的快捷方式
/// </summary>
/// <param name="path">路径</param>
private static void DeleteFile(string path)
{
FileAttributes attr = System.IO. File.GetAttributes(path);
if (attr == FileAttributes.Directory)
{
Directory.Delete(path, true);
}
else
{
System.IO.File.Delete(path);
}
}
}
}

65
KeyGo/AppConfig.cs Normal file
View File

@@ -0,0 +1,65 @@
using System.IO;
using System.Xml.Serialization;
namespace KeyGo
{
/// <summary>
/// 应用配置帮助类
/// </summary>
public class AppConfig
{
/// <summary>
/// Gets or sets a value indicating whether [power boot].
/// </summary>
/// <value>
/// <c>true</c> if [power boot]; otherwise, <c>false</c>.
/// </value>
public bool PowerBoot { get; set; } = true;
/// <summary>
/// Gets or sets a value indicating whether [close to hide].
/// </summary>
/// <value>
/// <c>true</c> if [close to hide]; otherwise, <c>false</c>.
/// </value>
public bool CloseToHide { get; set; } = true;
/// <summary>
/// Loads the XML.
/// </summary>
/// <param name="filePath">The file path.</param>
/// <returns></returns>
public static AppConfig LoadXml(string filePath)
{
AppConfig data = null;
if (File.Exists(filePath))
{
XmlSerializer formatter = new XmlSerializer(typeof(AppConfig));
using (var stream = File.OpenRead(filePath))
{
if (stream.Length > 0)
{
data = formatter.Deserialize(stream) as AppConfig;
}
}
}
return data;
}
/// <summary>
/// Saves the XML.
/// </summary>
/// <param name="filePath">The file path.</param>
public void SaveXml(string filePath)
{
if (!File.Exists(filePath))
Directory.CreateDirectory(Path.GetDirectoryName(filePath));
XmlSerializer formatter = new XmlSerializer(typeof(AppConfig));
using (var stream = File.Create(filePath))
{
formatter.Serialize(stream, this);
}
}
}
}

View File

@@ -41,7 +41,7 @@ namespace KeyGo
if (instance != null && instance.MainWindowHandle != IntPtr.Zero)
{
if (IsIconic(instance.MainWindowHandle))
ShowWindowAsync(instance.MainWindowHandle, (int)CmdShow.Restore);
ShowWindow(instance.MainWindowHandle, (int)CmdShow.Restore);
SetForegroundWindow(instance.MainWindowHandle);
}
}
@@ -53,9 +53,16 @@ namespace KeyGo
public static void MinimizeWindow(Process instance)
{
if (instance != null && instance.MainWindowHandle != IntPtr.Zero)
ShowWindowAsync(instance.MainWindowHandle, (int)CmdShow.Minimize);
ShowWindow(instance.MainWindowHandle, (int)CmdShow.Minimize);
}
/// <summary>
/// Determines whether [is foreground window] [the specified instance].
/// </summary>
/// <param name="instance">The instance.</param>
/// <returns>
/// <c>true</c> if [is foreground window] [the specified instance]; otherwise, <c>false</c>.
/// </returns>
public static bool IsForegroundWindow(Process instance)
{
return GetForegroundWindow() == instance.MainWindowHandle;
@@ -143,10 +150,13 @@ namespace KeyGo
}
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(System.IntPtr hWnd, int cmdShow);
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(System.IntPtr hWnd);
private static extern bool ShowWindow(IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("User32.dll")]
private static extern IntPtr GetForegroundWindow();

View File

@@ -5,7 +5,7 @@ using System.Windows.Forms;
namespace KeyGo
{
public class AppHotKey
public static class AppHotKey
{
/// <summary>
/// 注册热键
@@ -17,14 +17,7 @@ namespace KeyGo
public static void RegKey(IntPtr hwnd, int hotKey_id, KeyModifiers keyModifiers, Keys key)
{
if (!RegisterHotKey(hwnd, hotKey_id, keyModifiers, key))
{
throw new Win32Exception();
//int code = Marshal.GetLastWin32Error();
//if (code == 1409)
// MessageBox.Show("热键被占用!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
//else
// MessageBox.Show("注册热键失败!错误代码:" + code.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
@@ -38,7 +31,6 @@ namespace KeyGo
UnregisterHotKey(hwnd, hotKey_id);
}
//如果函数执行成功返回值不为0。
//如果函数执行失败返回值为0。要得到扩展错误信息调用GetLastError。
[DllImport("user32.dll", SetLastError = true)]

View File

@@ -12,6 +12,7 @@ namespace KeyGo
}
public HotKeyItem HotKeyItem { get; set; }
private void FormHotKey_Load(object sender, EventArgs e)
{
if (HotKeyItem != null)
@@ -110,6 +111,5 @@ namespace KeyGo
TxtStartupPath.Text = frm.FileName;
}
}
}
}

View File

@@ -35,9 +35,9 @@ namespace KeyGo
this.BtnAdd = new System.Windows.Forms.Button();
this.NotifyIcon = new System.Windows.Forms.NotifyIcon(this.components);
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
this.TSMICloseToHide = new System.Windows.Forms.ToolStripMenuItem();
this.TSMIPowerBoot = new System.Windows.Forms.ToolStripMenuItem();
this.TSMIExit = new System.Windows.Forms.ToolStripMenuItem();
this.TSMIPowerOnStartup = new System.Windows.Forms.ToolStripMenuItem();
this.TSMICloseToMin = new System.Windows.Forms.ToolStripMenuItem();
this.FLPHotKeys.SuspendLayout();
this.contextMenuStrip1.SuspendLayout();
this.SuspendLayout();
@@ -75,35 +75,35 @@ namespace KeyGo
// contextMenuStrip1
//
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.TSMICloseToMin,
this.TSMIPowerOnStartup,
this.TSMICloseToHide,
this.TSMIPowerBoot,
this.TSMIExit});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.Size = new System.Drawing.Size(149, 70);
//
// TSMICloseToHide
//
this.TSMICloseToHide.CheckOnClick = true;
this.TSMICloseToHide.Name = "TSMICloseToHide";
this.TSMICloseToHide.Size = new System.Drawing.Size(148, 22);
this.TSMICloseToHide.Text = "关闭为最小化";
this.TSMICloseToHide.CheckedChanged += new System.EventHandler(this.TSMICloseToHide_CheckedChanged);
//
// TSMIPowerBoot
//
this.TSMIPowerBoot.CheckOnClick = true;
this.TSMIPowerBoot.Name = "TSMIPowerBoot";
this.TSMIPowerBoot.Size = new System.Drawing.Size(148, 22);
this.TSMIPowerBoot.Text = "开机自启动";
this.TSMIPowerBoot.CheckedChanged += new System.EventHandler(this.TSMIPowerBoot_CheckedChanged);
//
// TSMIExit
//
this.TSMIExit.Name = "TSMIExit";
this.TSMIExit.Size = new System.Drawing.Size(180, 22);
this.TSMIExit.Size = new System.Drawing.Size(148, 22);
this.TSMIExit.Text = "退出";
this.TSMIExit.Click += new System.EventHandler(this.TSMIExit_Click);
//
// TSMIPowerOnStartup
//
this.TSMIPowerOnStartup.CheckOnClick = true;
this.TSMIPowerOnStartup.Name = "TSMIPowerOnStartup";
this.TSMIPowerOnStartup.Size = new System.Drawing.Size(180, 22);
this.TSMIPowerOnStartup.Text = "开机自启动";
this.TSMIPowerOnStartup.CheckedChanged += new System.EventHandler(this.TSMIPowerOnStartup_CheckedChanged);
//
// TSMICloseToMin
//
this.TSMICloseToMin.CheckOnClick = true;
this.TSMICloseToMin.Name = "TSMICloseToMin";
this.TSMICloseToMin.Size = new System.Drawing.Size(180, 22);
this.TSMICloseToMin.Text = "关闭为最小化";
this.TSMICloseToMin.CheckedChanged += new System.EventHandler(this.TSMICloseToMin_CheckedChanged);
//
// FormMain
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F);
@@ -135,8 +135,8 @@ namespace KeyGo
private System.Windows.Forms.NotifyIcon NotifyIcon;
private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
private System.Windows.Forms.ToolStripMenuItem TSMIExit;
private System.Windows.Forms.ToolStripMenuItem TSMIPowerOnStartup;
private System.Windows.Forms.ToolStripMenuItem TSMICloseToMin;
private System.Windows.Forms.ToolStripMenuItem TSMIPowerBoot;
private System.Windows.Forms.ToolStripMenuItem TSMICloseToHide;
}
}

View File

@@ -8,35 +8,73 @@ namespace KeyGo
{
public partial class FormMain : Form
{
private static readonly string _DataFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "KeyGo", "HotKey.xml");
#region
private static readonly string _DataFolderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "KeyGo");
private static readonly string _DataFilePath = Path.Combine(_DataFolderPath, "HotKey.xml");
private static readonly string _AppConfigFilePath = Path.Combine(_DataFolderPath, "AppConfig.xml");
private readonly KeyGo _KeyGo;
private readonly AppConfig _AppConfig;
private readonly string _CurrentProcessName;
private bool _Initializing;
#endregion
#region
public FormMain()
{
InitializeComponent();
_Initializing = true;
// 读取程序集版本,显示到标题栏
Assembly assembly = Assembly.GetExecutingAssembly();
AssemblyName thisAssemName = assembly.GetName();
Text += $" - {thisAssemName.Version}";
Text += $" - {thisAssemName.Version} - github.com/jie65535/KeyGo";
// 载入并初始化配置
_AppConfig = LoadAppConfig(_AppConfigFilePath);
TSMICloseToHide.Checked = _AppConfig.CloseToHide;
TSMIPowerBoot.Checked = _AppConfig.PowerBoot;
SetPowerBoot(_AppConfig.PowerBoot);
// 载入热键数据
_KeyGo = LoadHotKeyItems(_DataFilePath);
_KeyGo.HotKeyTriggerEvent += KeyGo_HotKeyTriggerEvent;
_KeyGo.FormHandle = Handle;
var p = Process.GetCurrentProcess();
_CurrentProcessName = p.ProcessName;
if (_KeyGo.Items.Count == 0)
{
_KeyGo.Items.Add(new HotKeyItem
{
ProcessName = p.ProcessName,
ProcessName = _CurrentProcessName,
StartupPath = p.MainModule.FileName,
HotKey = "Ctrl+G",
});
}
_KeyGo.RegAllKey();
// 初始化UI
FLPHotKeys.SuspendLayout();
foreach (var item in _KeyGo.Items)
FLP_AddItem(item);
FLPHotKeys.ResumeLayout();
_Initializing = false;
}
#endregion
#region
private void KeyGo_HotKeyTriggerEvent(object sender, HotKeyTriggerEventArgs e)
{
if (e.HotKeyItem.ProcessName == _CurrentProcessName)
{
ChangeVisible();
e.Handle = true;
}
}
private void FormMain_Load(object sender, EventArgs e)
@@ -44,22 +82,32 @@ namespace KeyGo
Console.WriteLine(_DataFilePath);
}
bool isExit;
private bool isExit;
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
if (!isExit)
if (!isExit && _AppConfig.CloseToHide)
{
Hide();
e.Cancel = true;
}
}
private void FormMain_Deactivate(object sender, EventArgs e)
{
// 如果最小化,则隐藏窗体
if (WindowState == FormWindowState.Minimized)
Hide();
}
private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
{
_KeyGo.UnRegAllKey();
SaveHotKeyItems(_KeyGo);
}
#endregion
#region IO
private KeyGo LoadHotKeyItems(string xmlFilePath)
@@ -90,6 +138,36 @@ namespace KeyGo
#endregion IO
#region IO
private AppConfig LoadAppConfig(string xmlFilePath)
{
AppConfig instance = null;
try
{
instance = AppConfig.LoadXml(xmlFilePath);
}
catch (Exception ex)
{
MessageBox.Show("载入配置文件异常:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return instance ?? new AppConfig();
}
private void SaveAppConfig(AppConfig config)
{
try
{
config.SaveXml(_AppConfigFilePath);
}
catch (Exception ex)
{
MessageBox.Show("保存配置文件异常:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#endregion IO
#region
private const int WM_HOTKEY = 0x312;
@@ -107,21 +185,7 @@ namespace KeyGo
#endregion
private void BtnTest_Click(object sender, EventArgs e)
{
//_KeyGo.Items.Add(new HotKeyItem
//{
// ProcessName = "QQ",
// StartupPath = "",
// HotKey = "Ctrl+Q",
// Enabled = true,
// TriggerCounter = 0,
// CreationTime = DateTime.Now,
// LastModifiedTime = DateTime.Now,
//});
//SaveHotKeyItems(_KeyGo);
//new FormHotKey().ShowDialog();
}
#region
private void BtnAdd_Click(object sender, EventArgs e)
{
@@ -162,12 +226,21 @@ namespace KeyGo
FLPHotKeys.Controls.SetChildIndex(BtnAdd, i1);
}
#endregion
#region
private void NotifyIcon_MouseClick(object sender, MouseEventArgs e)
{
// 鼠标左键点击托盘图标才触发
if (e.Button != MouseButtons.Left)
return;
ChangeVisible();
}
private void ChangeVisible()
{
// 当前在前台则隐藏
if (Visible)
{
@@ -192,27 +265,49 @@ namespace KeyGo
}
}
private void FormMain_Deactivate(object sender, EventArgs e)
{
// 如果最小化,则隐藏窗体
if (WindowState == FormWindowState.Minimized)
Hide();
}
private void TSMIExit_Click(object sender, EventArgs e)
{
isExit = true;
Close();
}
private void TSMIPowerOnStartup_CheckedChanged(object sender, EventArgs e)
private void TSMIPowerBoot_CheckedChanged(object sender, EventArgs e)
{
MessageBox.Show("暂未完成", "TODO");
if (_Initializing)
return;
_AppConfig.PowerBoot = TSMIPowerBoot.Checked;
SetPowerBoot(_AppConfig.PowerBoot);
SaveAppConfig(_AppConfig);
}
private void TSMICloseToMin_CheckedChanged(object sender, EventArgs e)
private void TSMICloseToHide_CheckedChanged(object sender, EventArgs e)
{
MessageBox.Show("暂未完成", "TODO");
if (_Initializing)
return;
_AppConfig.CloseToHide = TSMICloseToHide.Checked;
SaveAppConfig(_AppConfig);
}
#endregion
#region
/// <summary>
/// 设置开机自启动
/// </summary>
/// <param name="enable">if set to <c>true</c> [enable].</param>
private void SetPowerBoot(bool enable)
{
try
{
AppAutoStart.SetMeAutoStart(enable);
}
catch (Exception ex)
{
MessageBox.Show("设置开机自启时异常:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#endregion
}
}

View File

@@ -9,15 +9,21 @@ using System.Xml.Serialization;
namespace KeyGo
{
/// <summary>
/// KeyGo 核心功能类
/// </summary>
public class KeyGo
{
static int _RegMaxID;
#region Member
private static int _RegMaxID;
[XmlIgnore]
public IntPtr FormHandle { get; set; }
public List<HotKeyItem> Items { get; set; } = new List<HotKeyItem>();
#endregion Member
#region FILE IO
@@ -59,7 +65,7 @@ namespace KeyGo
}
}
#endregion
#endregion FILE IO
#region HotKey Register
@@ -84,7 +90,6 @@ namespace KeyGo
}
}
/// <summary>
/// Uns the reg all key.
/// </summary>
@@ -140,15 +145,19 @@ namespace KeyGo
case "ctrl":
keyModifiers |= AppHotKey.KeyModifiers.Ctrl;
break;
case "shift":
keyModifiers |= AppHotKey.KeyModifiers.Shift;
break;
case "alt":
keyModifiers |= AppHotKey.KeyModifiers.Alt;
break;
case "win":
keyModifiers |= AppHotKey.KeyModifiers.WindowsKey;
break;
default:
keyCode = (Keys)Enum.Parse(typeof(Keys), key);
break;
@@ -180,7 +189,21 @@ namespace KeyGo
item.HotKeyID = 0;
}
#endregion
#endregion HotKey Register
#region HotKey Trigger
/// <summary>
/// 热键触发时调用
/// </summary>
public event EventHandler<HotKeyTriggerEventArgs> HotKeyTriggerEvent;
private bool OnHotKeyTrigger(HotKeyItem item)
{
var args = new HotKeyTriggerEventArgs{ HotKeyItem = item };
HotKeyTriggerEvent?.Invoke(this, args);
return args.Handle;
}
/// <summary>
/// Processes the hotkey.
@@ -191,15 +214,26 @@ namespace KeyGo
var hotkey = Items.Find(k => k.HotKeyID == hotKey_id);
if (hotkey != null)
{
//Console.WriteLine($"ID:{hotkey.HotKeyID} Keys:{hotkey.HotKey} ProcessName:{hotkey.ProcessName}\nStartupPath:{hotkey.StartupPath}");
++hotkey.TriggerCounter;
// 触发事件,若被外部处理,则内部不再执行
if (OnHotKeyTrigger(hotkey))
return;
// 热键相应逻辑:
// 若应用未启动:启动应用
// 若应用未在最前:激活窗体,推到最前
// 若应用已在最前:最小化窗体
var process = Process.GetProcessesByName(hotkey.ProcessName).Where(p => p.MainWindowHandle != IntPtr.Zero).ToArray().FirstOrDefault();
var processes = Process.GetProcessesByName(hotkey.ProcessName);
if (processes == null || processes.Length < 1)
{
if (!string.IsNullOrWhiteSpace(hotkey.StartupPath) && File.Exists(hotkey.StartupPath))
Process.Start(hotkey.StartupPath);
}
else
{
var process = processes.Where(p => p.MainWindowHandle != IntPtr.Zero).ToArray().FirstOrDefault();
if (process != null)
{
if (AppControl.IsForegroundWindow(process))
@@ -207,15 +241,13 @@ namespace KeyGo
else
AppControl.ShowWindow(process);
}
else
{
if (!string.IsNullOrWhiteSpace(hotkey.StartupPath) && File.Exists(hotkey.StartupPath))
Process.Start(hotkey.StartupPath);
}
}
}
Console.WriteLine($"ID:{hotkey.HotKeyID} Keys:{hotkey.HotKey} ProcessName:{hotkey.ProcessName}\nStartupPath:{hotkey.StartupPath}");
}
}
#endregion HotKey Trigger
#region HotKey Manager
/// <summary>
/// 添加一个新热键
@@ -261,5 +293,23 @@ namespace KeyGo
if (item.Enabled)
RegKey(item);
}
#endregion HotKey Manager
}
/// <summary>
/// 热键触发事件参数
/// </summary>
public class HotKeyTriggerEventArgs
{
public HotKeyItem HotKeyItem { get; set; }
/// <summary>
/// 获取或设置该事件是否已经被处理
/// </summary>
/// <value>
/// <c>true</c> if handle; otherwise, <c>false</c>.
/// </value>
public bool Handle { get; set; }
}
}

View File

@@ -55,6 +55,13 @@
<ErrorReport>prompt</ErrorReport>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup>
<TargetZone>LocalIntranet</TargetZone>
</PropertyGroup>
<PropertyGroup>
<GenerateManifests>false</GenerateManifests>
</PropertyGroup>
<PropertyGroup />
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
@@ -69,6 +76,8 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="AppAutoStart.cs" />
<Compile Include="AppConfig.cs" />
<Compile Include="AppControl.cs" />
<Compile Include="AppHotKey.cs" />
<Compile Include="FormHotKey.cs">
@@ -134,7 +143,17 @@
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<COMReference Include="IWshRuntimeLibrary">
<Guid>{F935DC20-1CF0-11D0-ADB9-00C04FD58A0B}</Guid>
<VersionMajor>1</VersionMajor>
<VersionMinor>0</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>tlbimp</WrapperTool>
<Isolated>False</Isolated>
<EmbedInteropTypes>True</EmbedInteropTypes>
</COMReference>
</ItemGroup>
<ItemGroup>
<Content Include="KeyGo.ico" />
<Content Include="Resources\ImgAdd.png" />

View File

@@ -1,4 +1,5 @@
using System;
using System.Text;
using System.Windows.Forms;
namespace KeyGo
@@ -14,14 +15,64 @@ namespace KeyGo
var p = AppControl.GetCurrentRunningInstance();
if (p != null)
{
if (p.MainWindowHandle == IntPtr.Zero)
MessageBox.Show("应用程序已启用或未关闭,请勿重复启动程序。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
AppControl.ShowWindow(p);
}
else
{
//设置应用程序处理异常方式ThreadException处理
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
//处理UI线程异常
Application.ThreadException += Application_ThreadException;
//处理非UI线程异常
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FormMain());
}
}
private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
string str = GetExceptionMsg(e.Exception, e.ToString());
//Logger.Info("应用程序线程异常", e.Exception);
MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
//Logger.Info("未捕获异常", e.ExceptionObject as Exception);
MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
/// <summary>
/// 生成自定义异常消息
/// </summary>
/// <param name="ex">异常对象</param>
/// <param name="backStr">备用异常消息当ex为null时有效</param>
/// <returns>异常字符串文本</returns>
private static string GetExceptionMsg(Exception ex, string backStr)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("****************************异常文本****************************");
sb.AppendLine("【出现时间】:" + DateTime.Now.ToString());
if (ex != null)
{
sb.AppendLine("【异常类型】:" + ex.GetType().Name);
sb.AppendLine("【异常信息】:" + ex.Message);
#if DEBUG
sb.AppendLine("【堆栈调用】:" + ex.StackTrace);
#endif
}
else
{
sb.AppendLine("【未处理异常】:" + backStr);
}
sb.AppendLine("***************************************************************");
return sb.ToString();
}
}
}

View File

@@ -5,7 +5,7 @@ using System.Runtime.InteropServices;
// 控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("KeyGo")]
[assembly: AssemblyDescription("通过注册热键 启动/切换到/最小化 预设应用的小工具")]
[assembly: AssemblyDescription("通过注册热键 启动/切换到/最小化 预设应用的小工具 - 开源地址https://github.com/jie65535/KeyGo")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("KeyGo")]
@@ -31,5 +31,5 @@ using System.Runtime.InteropServices;
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.1.0.3")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@@ -119,6 +119,8 @@ namespace KeyGo
/// 删除热键项
/// </summary>
private void BtnDel_Click(object sender, EventArgs e)
{
if (MessageBox.Show("是否确定删除该热键?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
try
{
@@ -133,3 +135,4 @@ namespace KeyGo
}
}
}
}

View File

@@ -1,4 +1,4 @@
# KeyGo
ͨ<EFBFBD><EFBFBD>ע<EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD> <20><><EFBFBD><EFBFBD>/<2F>л<EFBFBD><D0BB><EFBFBD>/<2F><>С<EFBFBD><D0A1> Ԥ<><D4A4>Ӧ<EFBFBD>õ<EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD>
通过注册热键 启动/切换到/最小化 预设应用的小工具
![<EFBFBD><EFBFBD><EFBFBD>н<EFBFBD>ͼ](Docs\RunningImage1.png)
![运行截图](Docs/RunningImage1.png)