From 38a10ddb9d7ac07b8d299b624d4f31695de64793 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=93=9D=E7=82=B9lilac?= Date: Wed, 30 Dec 2020 21:02:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=BB=BA=E8=8F=9C=E5=8D=95=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E6=B7=BB=E5=8A=A0=E6=9B=B4=E6=94=B9=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controls/Interfaces/ITsiCommandItem.cs | 6 ++-- ContextMenuManager/Controls/ShellNewItem.cs | 35 ++++++++++++++++--- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/ContextMenuManager/Controls/Interfaces/ITsiCommandItem.cs b/ContextMenuManager/Controls/Interfaces/ITsiCommandItem.cs index 9db164c..dbfc3ec 100644 --- a/ContextMenuManager/Controls/Interfaces/ITsiCommandItem.cs +++ b/ContextMenuManager/Controls/Interfaces/ITsiCommandItem.cs @@ -12,6 +12,8 @@ namespace ContextMenuManager.Controls.Interfaces sealed class ChangeCommandMenuItem : ToolStripMenuItem { + public bool CommandCanBeEmpty { get; set; } + public ChangeCommandMenuItem(ITsiCommandItem item) : base(AppString.Menu.ChangeCommand) { this.Click += (sender, e) => @@ -21,12 +23,12 @@ namespace ContextMenuManager.Controls.Interfaces }; } - public static string ChangeCommand(string command) + private string ChangeCommand(string command) { using(InputDialog dlg = new InputDialog { Text = command, Title = AppString.Menu.ChangeCommand }) { if(dlg.ShowDialog() != DialogResult.OK) return null; - if(string.IsNullOrEmpty(dlg.Text)) + if(!CommandCanBeEmpty && string.IsNullOrEmpty(dlg.Text)) { MessageBoxEx.Show(AppString.MessageBox.CommandCannotBeEmpty); return ChangeCommand(command); diff --git a/ContextMenuManager/Controls/ShellNewItem.cs b/ContextMenuManager/Controls/ShellNewItem.cs index 564abe0..3846bd0 100644 --- a/ContextMenuManager/Controls/ShellNewItem.cs +++ b/ContextMenuManager/Controls/ShellNewItem.cs @@ -16,11 +16,12 @@ namespace ContextMenuManager.Controls * 但当ShellNew项中不存在合法的MenuText键值时,菜单名称取HKCR\的FriendlyTypeName键值或者默认值,后两个键值都为空时也不成立 * 3.ShellNew项中存在"NullFile", "Data", "FileName", "Directory", "Command"中的一个或多个键值*/ sealed class ShellNewItem : MyListItem, IChkVisibleItem, ITsiTextItem, IBtnShowMenuItem, IBtnMoveUpDownItem, - ITsiIconItem, ITsiWebSearchItem, ITsiFilePathItem, ITsiRegPathItem, ITsiRegDeleteItem, ITsiRegExportItem + ITsiIconItem, ITsiWebSearchItem, ITsiFilePathItem, ITsiRegPathItem, ITsiRegDeleteItem, ITsiRegExportItem, ITsiCommandItem { public static readonly string[] SnParts = { "ShellNew", "-ShellNew" }; public static readonly string[] UnableSortExtensions = { ".library-ms", ".lnk", "Folder" }; private static readonly string[] UnableEditDataValues = { "Directory", "FileName", "Handler", "Command" }; + private static readonly string[] UnableChangeCommandValues = { "Data", "Directory", "FileName", "Handler" }; public ShellNewItem(ShellNewList list, string regPath) { @@ -55,6 +56,7 @@ namespace ContextMenuManager.Controls private string DefaultOpenModePath => $@"{HKCR}\{DefaultOpenMode}";//默认关联打开方式注册表路径 private bool CanEditData => UnableEditDataValues.All(value => Registry.GetValue(RegPath, value, null) == null);//能够编辑初始数据的 + private bool CanChangeCommand => UnableChangeCommandValues.All(value => Registry.GetValue(RegPath, value, null) == null);//能够更改菜单命令的 public bool CanSort => !UnableSortExtensions.Contains(Extension, StringComparer.OrdinalIgnoreCase);//能够排序的 public string ItemFilePath @@ -148,6 +150,25 @@ namespace ContextMenuManager.Controls set => Registry.SetValue(RegPath, "Data", value); } + public string ItemCommand + { + get => Registry.GetValue(RegPath, "Command", null)?.ToString(); + set + { + if(value.IsNullOrWhiteSpace()) + { + if(Registry.GetValue(RegPath, "NullFile", null) != null) + { + RegistryEx.DeleteValue(RegPath, "Command"); + } + } + else + { + Registry.SetValue(RegPath, "Command", value); + } + } + } + public ShellNewList Owner { get; private set; } public MoveButton BtnMoveUp { get; set; } public MoveButton BtnMoveDown { get; set; } @@ -161,6 +182,7 @@ namespace ContextMenuManager.Controls public RegLocationMenuItem TsiRegLocation { get; set; } public DeleteMeMenuItem TsiDeleteMe { get; set; } public RegExportMenuItem TsiRegExport { get; set; } + public ChangeCommandMenuItem TsiChangeCommand { get; set; } readonly ToolStripMenuItem TsiDetails = new ToolStripMenuItem(AppString.Menu.Details); readonly ToolStripMenuItem TsiEditData = new ToolStripMenuItem(AppString.Menu.InitialData); @@ -174,22 +196,27 @@ namespace ContextMenuManager.Controls TsiSearch = new WebSearchMenuItem(this); TsiChangeText = new ChangeTextMenuItem(this); TsiChangeIcon = new ChangeIconMenuItem(this); + TsiChangeCommand = new ChangeCommandMenuItem(this); TsiFileLocation = new FileLocationMenuItem(this); TsiFileProperties = new FilePropertiesMenuItem(this); TsiRegLocation = new RegLocationMenuItem(this); TsiRegExport = new RegExportMenuItem(this); TsiDeleteMe = new DeleteMeMenuItem(this); + TsiChangeCommand.CommandCanBeEmpty = true; ContextMenuStrip.Items.AddRange(new ToolStripItem[] {TsiChangeText, new ToolStripSeparator(), TsiChangeIcon, new ToolStripSeparator(), TsiDetails, new ToolStripSeparator(), TsiDeleteMe }); TsiDetails.DropDownItems.AddRange(new ToolStripItem[] { TsiSearch, new ToolStripSeparator(), - TsiEditData, TsiFileProperties, TsiFileLocation, TsiRegLocation, TsiRegExport }); + TsiEditData,TsiChangeCommand, TsiFileProperties, TsiFileLocation, TsiRegLocation, TsiRegExport }); TsiEditData.Click += (sender, e) => EditInitialData(); - ContextMenuStrip.Opening += (sender, e) => TsiEditData.Visible = CanEditData; - + ContextMenuStrip.Opening += (sender, e) => + { + TsiEditData.Visible = CanEditData; + TsiChangeCommand.Visible = CanChangeCommand; + }; BtnMoveUp.MouseDown += (sender, e) => Owner.MoveItem(this, true); BtnMoveDown.MouseDown += (sender, e) => Owner.MoveItem(this, false); }