1. 发布 版本v1.1.0.0

2. 新增 将可执行文件或快捷方式拖拽到软件上,可以快捷添加热键
3. 新增 配置管理功能,可以通过任务栏图标右键选择配置文件的导入导出或者从剪切板中导入导出配置
4. 修复 重启时可能导致配置文件丢失问题(不再在软件结束时操作配置文件。不确定有效。)
This commit is contained in:
筱傑 2021-06-19 13:56:16 +08:00
parent ddc54d9a97
commit dd6ddb1784
4 changed files with 233 additions and 20 deletions

View File

@ -38,12 +38,18 @@ namespace KeyGo
this.TSMICloseToHide = new System.Windows.Forms.ToolStripMenuItem();
this.TSMIPowerBoot = new System.Windows.Forms.ToolStripMenuItem();
this.TSMIExit = new System.Windows.Forms.ToolStripMenuItem();
this.TSMIConfigFile = new System.Windows.Forms.ToolStripMenuItem();
this.TSMIExportConfig = new System.Windows.Forms.ToolStripMenuItem();
this.TSMIImportConfig = new System.Windows.Forms.ToolStripMenuItem();
this.TSMICopyConfig = new System.Windows.Forms.ToolStripMenuItem();
this.TSMIPasteConfig = new System.Windows.Forms.ToolStripMenuItem();
this.FLPHotKeys.SuspendLayout();
this.contextMenuStrip1.SuspendLayout();
this.SuspendLayout();
//
// FLPHotKeys
//
this.FLPHotKeys.AllowDrop = true;
this.FLPHotKeys.AutoScroll = true;
this.FLPHotKeys.BackColor = System.Drawing.Color.White;
this.FLPHotKeys.Controls.Add(this.BtnAdd);
@ -54,6 +60,8 @@ namespace KeyGo
this.FLPHotKeys.Padding = new System.Windows.Forms.Padding(3);
this.FLPHotKeys.Size = new System.Drawing.Size(488, 197);
this.FLPHotKeys.TabIndex = 0;
this.FLPHotKeys.DragDrop += new System.Windows.Forms.DragEventHandler(this.FLPHotKeys_DragDrop);
this.FLPHotKeys.DragEnter += new System.Windows.Forms.DragEventHandler(this.FLPHotKeys_DragEnter);
//
// BtnAdd
//
@ -75,17 +83,18 @@ namespace KeyGo
// contextMenuStrip1
//
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.TSMIConfigFile,
this.TSMICloseToHide,
this.TSMIPowerBoot,
this.TSMIExit});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.Size = new System.Drawing.Size(149, 70);
this.contextMenuStrip1.Size = new System.Drawing.Size(181, 114);
//
// TSMICloseToHide
//
this.TSMICloseToHide.CheckOnClick = true;
this.TSMICloseToHide.Name = "TSMICloseToHide";
this.TSMICloseToHide.Size = new System.Drawing.Size(148, 22);
this.TSMICloseToHide.Size = new System.Drawing.Size(180, 22);
this.TSMICloseToHide.Text = "关闭为最小化";
this.TSMICloseToHide.CheckedChanged += new System.EventHandler(this.TSMICloseToHide_CheckedChanged);
//
@ -93,17 +102,56 @@ namespace KeyGo
//
this.TSMIPowerBoot.CheckOnClick = true;
this.TSMIPowerBoot.Name = "TSMIPowerBoot";
this.TSMIPowerBoot.Size = new System.Drawing.Size(148, 22);
this.TSMIPowerBoot.Size = new System.Drawing.Size(180, 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(148, 22);
this.TSMIExit.Size = new System.Drawing.Size(180, 22);
this.TSMIExit.Text = "退出";
this.TSMIExit.Click += new System.EventHandler(this.TSMIExit_Click);
//
// TSMIConfigFile
//
this.TSMIConfigFile.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.TSMIExportConfig,
this.TSMIImportConfig,
this.TSMICopyConfig,
this.TSMIPasteConfig});
this.TSMIConfigFile.Name = "TSMIConfigFile";
this.TSMIConfigFile.Size = new System.Drawing.Size(180, 22);
this.TSMIConfigFile.Text = "配置文件";
//
// TSMIExportConfig
//
this.TSMIExportConfig.Name = "TSMIExportConfig";
this.TSMIExportConfig.Size = new System.Drawing.Size(180, 22);
this.TSMIExportConfig.Text = "导出文件";
this.TSMIExportConfig.Click += new System.EventHandler(this.TSMIExportConfig_Click);
//
// TSMIImportConfig
//
this.TSMIImportConfig.Name = "TSMIImportConfig";
this.TSMIImportConfig.Size = new System.Drawing.Size(180, 22);
this.TSMIImportConfig.Text = "导入文件";
this.TSMIImportConfig.Click += new System.EventHandler(this.TSMIImportConfig_Click);
//
// TSMICopyConfig
//
this.TSMICopyConfig.Name = "TSMICopyConfig";
this.TSMICopyConfig.Size = new System.Drawing.Size(180, 22);
this.TSMICopyConfig.Text = "复制到剪切板";
this.TSMICopyConfig.Click += new System.EventHandler(this.TSMICopyConfig_Click);
//
// TSMIPasteConfig
//
this.TSMIPasteConfig.Name = "TSMIPasteConfig";
this.TSMIPasteConfig.Size = new System.Drawing.Size(180, 22);
this.TSMIPasteConfig.Text = "从剪切板读入";
this.TSMIPasteConfig.Click += new System.EventHandler(this.TSMIPasteConfig_Click);
//
// FormMain
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F);
@ -137,6 +185,11 @@ namespace KeyGo
private System.Windows.Forms.ToolStripMenuItem TSMIExit;
private System.Windows.Forms.ToolStripMenuItem TSMIPowerBoot;
private System.Windows.Forms.ToolStripMenuItem TSMICloseToHide;
private System.Windows.Forms.ToolStripMenuItem TSMIConfigFile;
private System.Windows.Forms.ToolStripMenuItem TSMIExportConfig;
private System.Windows.Forms.ToolStripMenuItem TSMIImportConfig;
private System.Windows.Forms.ToolStripMenuItem TSMICopyConfig;
private System.Windows.Forms.ToolStripMenuItem TSMIPasteConfig;
}
}

View File

@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
@ -103,7 +105,8 @@ namespace KeyGo
private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
{
_KeyGo.UnRegAllKey();
SaveHotKeyItems(_KeyGo);
// 关闭后不用保存,修改时保存即可
//SaveHotKeyItems(_KeyGo);
}
#endregion
@ -192,17 +195,7 @@ namespace KeyGo
var frm = new FormHotKey();
if (frm.ShowDialog() == DialogResult.OK)
{
try
{
var item = frm.HotKeyItem;
_KeyGo.AddHotKey(item);
FLP_AddItem(item);
SaveHotKeyItems(_KeyGo);
}
catch (Exception ex)
{
MessageBox.Show("在添加新的热键时异常:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
AddHotKeyItem(frm.HotKeyItem);
}
}
@ -226,6 +219,30 @@ namespace KeyGo
FLPHotKeys.Controls.SetChildIndex(BtnAdd, i1);
}
private void AddHotKeyItem(HotKeyItem item, bool save = true)
{
try
{
_KeyGo.AddHotKey(item);
}
catch (Exception ex)
{
if (item.Enabled)
{
// 禁用后再添加
item.Enabled = false;
_KeyGo.AddHotKey(item);
}
else
{
MessageBox.Show("在添加新的热键时异常:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
FLP_AddItem(item);
if (save) SaveHotKeyItems(_KeyGo);
}
#endregion
#region
@ -309,5 +326,139 @@ namespace KeyGo
}
#endregion
#region
private void FLPHotKeys_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Link;
else
e.Effect = DragDropEffects.None;
}
private void FLPHotKeys_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetData(DataFormats.FileDrop) is Array files && files.Length > 0)
{
var filepath = files.GetValue(0) as string;
var frm = new FormHotKey
{
HotKeyItem = new HotKeyItem
{
StartupPath = filepath,
}
};
if (frm.ShowDialog() == DialogResult.OK)
{
AddHotKeyItem(frm.HotKeyItem);
}
}
}
#endregion
#region
private string ConfigToString(List<HotKeyItem> items)
{
return string.Join(Environment.NewLine, items.Select(item => $"{item.HotKey}|{item.ProcessName}|{item.StartupPath}"));
}
private List<HotKeyItem> ParseConfig(string config)
{
if (string.IsNullOrWhiteSpace(config))
throw new ArgumentNullException(nameof(config));
return config.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
.Select(s =>
{
var strs = s.Split('|');
if (strs.Length < 3)
throw new Exception("数据格式不正确,无法解析");
return new HotKeyItem
{
HotKey = strs[0],
ProcessName = strs[1],
StartupPath = strs[2],
};
}).ToList();
}
private void ImportConfig(string config)
{
if (string.IsNullOrWhiteSpace(config))
{
MessageBox.Show("导入失败,数据为空!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
var items = ParseConfig(config);
foreach (var item in items)
AddHotKeyItem(item, false);
SaveHotKeyItems(_KeyGo);
MessageBox.Show("导入完成", "提示");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "导入失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void TSMIExportConfig_Click(object sender, EventArgs e)
{
SaveFileDialog frm = new SaveFileDialog
{
Title = "请选择文件保存位置",
Filter = "Config file (*.config)|*.config|All file (*.*)|*.*",
FileName = "KeyGo_Hotkey.config",
RestoreDirectory = true,
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
};
if (frm.ShowDialog() == DialogResult.OK)
{
using (StreamWriter writer = new StreamWriter(frm.OpenFile()))
{
writer.Write(ConfigToString(_KeyGo.Items));
}
if (MessageBox.Show("写入完成,是否打开目录?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
Process.Start("Explorer", "/select,"+ frm.FileName);
}
}
private void TSMIImportConfig_Click(object sender, EventArgs e)
{
OpenFileDialog frm = new OpenFileDialog
{
Title = "请选择配置文件",
Filter = "Config file (*.config)|*.config|All file (*.*)|*.*",
RestoreDirectory = true,
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
};
if (frm.ShowDialog() == DialogResult.OK)
{
string data;
using (StreamReader reader = new StreamReader(frm.OpenFile()))
{
data = reader.ReadToEnd();
}
ImportConfig(data);
}
}
private void TSMICopyConfig_Click(object sender, EventArgs e)
{
Clipboard.SetDataObject(ConfigToString(_KeyGo.Items), true);
}
private void TSMIPasteConfig_Click(object sender, EventArgs e)
{
ImportConfig(Clipboard.GetText());
}
#endregion
}
}

View File

@ -253,14 +253,24 @@ namespace KeyGo
/// 添加一个新热键
/// </summary>
/// <param name="item">The item.</param>
/// <exception cref="ArgumentNullException">
/// item
/// or
/// HotKey - 热键不能为空!
/// </exception>
/// <exception cref="InvalidOperationException">
/// 功能键不能为空!
/// or
/// 快捷键不能为空!
/// </exception>
public void AddHotKey(HotKeyItem item)
{
if (item is null)
throw new ArgumentNullException(nameof(item));
Items.Add(item);
if (item.Enabled)
RegKey(item);
Items.Add(item);
}
/// <summary>
@ -272,10 +282,9 @@ namespace KeyGo
if (item is null)
throw new ArgumentNullException(nameof(item));
Items.Remove(item);
if (item.HotKeyID != 0)
UnRegKey(item);
Items.Remove(item);
}
/// <summary>

View File

@ -31,5 +31,5 @@ using System.Runtime.InteropServices;
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]