From dd6ddb17844744aeea8d189e654b5ff75295943c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AD=B1=E5=82=91?= <840465812@qq.com> Date: Sat, 19 Jun 2021 13:56:16 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E5=8F=91=E5=B8=83=20=E7=89=88=E6=9C=ACv1.?= =?UTF-8?q?1.0.0=202.=20=E6=96=B0=E5=A2=9E=20=E5=B0=86=E5=8F=AF=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=E6=96=87=E4=BB=B6=E6=88=96=E5=BF=AB=E6=8D=B7=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=E6=8B=96=E6=8B=BD=E5=88=B0=E8=BD=AF=E4=BB=B6=E4=B8=8A?= =?UTF-8?q?=EF=BC=8C=E5=8F=AF=E4=BB=A5=E5=BF=AB=E6=8D=B7=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E7=83=AD=E9=94=AE=203.=20=E6=96=B0=E5=A2=9E=20=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E7=AE=A1=E7=90=86=E5=8A=9F=E8=83=BD=EF=BC=8C=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E9=80=9A=E8=BF=87=E4=BB=BB=E5=8A=A1=E6=A0=8F=E5=9B=BE?= =?UTF-8?q?=E6=A0=87=E5=8F=B3=E9=94=AE=E9=80=89=E6=8B=A9=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=9A=84=E5=AF=BC=E5=85=A5=E5=AF=BC=E5=87=BA?= =?UTF-8?q?=E6=88=96=E8=80=85=E4=BB=8E=E5=89=AA=E5=88=87=E6=9D=BF=E4=B8=AD?= =?UTF-8?q?=E5=AF=BC=E5=85=A5=E5=AF=BC=E5=87=BA=E9=85=8D=E7=BD=AE=204.=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20=E9=87=8D=E5=90=AF=E6=97=B6=E5=8F=AF?= =?UTF-8?q?=E8=83=BD=E5=AF=BC=E8=87=B4=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=B8=A2=E5=A4=B1=E9=97=AE=E9=A2=98=EF=BC=88=E4=B8=8D=E5=86=8D?= =?UTF-8?q?=E5=9C=A8=E8=BD=AF=E4=BB=B6=E7=BB=93=E6=9D=9F=E6=97=B6=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E3=80=82=E4=B8=8D?= =?UTF-8?q?=E7=A1=AE=E5=AE=9A=E6=9C=89=E6=95=88=E3=80=82=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- KeyGo/FormMain.Designer.cs | 61 ++++++++++- KeyGo/FormMain.cs | 175 ++++++++++++++++++++++++++++--- KeyGo/KeyGo.cs | 15 ++- KeyGo/Properties/AssemblyInfo.cs | 2 +- 4 files changed, 233 insertions(+), 20 deletions(-) diff --git a/KeyGo/FormMain.Designer.cs b/KeyGo/FormMain.Designer.cs index 7bf56a0..65b134d 100644 --- a/KeyGo/FormMain.Designer.cs +++ b/KeyGo/FormMain.Designer.cs @@ -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; } } diff --git a/KeyGo/FormMain.cs b/KeyGo/FormMain.cs index 38cf232..0c5fe12 100644 --- a/KeyGo/FormMain.cs +++ b/KeyGo/FormMain.cs @@ -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 items) + { + return string.Join(Environment.NewLine, items.Select(item => $"{item.HotKey}|{item.ProcessName}|{item.StartupPath}")); + } + + private List 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 } } \ No newline at end of file diff --git a/KeyGo/KeyGo.cs b/KeyGo/KeyGo.cs index 20ec5c7..4bfa19b 100644 --- a/KeyGo/KeyGo.cs +++ b/KeyGo/KeyGo.cs @@ -253,14 +253,24 @@ namespace KeyGo /// 添加一个新热键 /// /// The item. + /// + /// item + /// or + /// HotKey - 热键不能为空! + /// + /// + /// 功能键不能为空! + /// or + /// 快捷键不能为空! + /// 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); } /// @@ -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); } /// diff --git a/KeyGo/Properties/AssemblyInfo.cs b/KeyGo/Properties/AssemblyInfo.cs index 2bef735..b461641 100644 --- a/KeyGo/Properties/AssemblyInfo.cs +++ b/KeyGo/Properties/AssemblyInfo.cs @@ -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")] \ No newline at end of file