Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

程序配置选项新增鼠标右键菜单配置按钮 #60

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions App/Common/RightKeyHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Security;
using Microsoft.Win32;

namespace PDFPatcher.Common
{
class RightKeyHelper
{
/// <summary>
/// 为程序添加鼠标右键菜单
/// 首次提交PR后Owner很快就回复,感谢他的建议现在采用向HKEY_CURRENT_USER\SOFTWARE\Classes下添加项,完美避开提权问题。不需要修改app.manifest
/// 有关注册表的描述:https://www.c-sharpcorner.com/UploadFile/f9f215/windows-registry/
/// 注册表的提权操作见:https://docs.microsoft.com/zh-cn/dotnet/api/system.security.accesscontrol.registrysecurity?view=net-6.0#methods
/// </summary>

public static bool Add(string appName, string appPath) {
try {
RegistryKey software = Registry.CurrentUser.OpenSubKey(@"SOFTWARE");
RegistryKey key = software;
RegistryKey shell = key.OpenSubKey(@"Classes\*\shell", true);
RegistryKey pdfPather = shell.CreateSubKey(appName);
RegistryKey command = shell.CreateSubKey(appName+@"\command");
command.SetValue("", appPath+@" %1");
pdfPather.SetValue("", "使用PDF补丁丁打开");
pdfPather.SetValue("Icon",appPath);
return true;
}
catch (Exception e){
FormHelper.InfoBox(e.ToString());
return false;
}
}

public static bool Delete(string appName) {
RightKeyHelper reg = new();
try {
RegistryKey software = Registry.CurrentUser.OpenSubKey(@"SOFTWARE");
RegistryKey key = software;
RegistryKey shell = key.OpenSubKey(@"Classes\*\shell", true);
RegistryKey pdfPather = shell.OpenSubKey(appName, true);
pdfPather.DeleteSubKey(@"command");
shell.DeleteSubKey(appName);
return true;
}
catch (Exception e) {
FormHelper.InfoBox(e.ToString());
return false;
}
}


public static bool IsExist(string appName) {
RightKeyHelper reg = new();
string[] subkeyNames;
try {
RegistryKey software = Registry.CurrentUser.OpenSubKey(@"SOFTWARE");
RegistryKey key = software;
RegistryKey shell = key.OpenSubKey(@"Classes\*\shell", true);
subkeyNames=shell.GetSubKeyNames();
foreach(string keyName in subkeyNames) {
if(keyName == appName) {
key.Close();
return true;
}
}
return false;
}
catch(Exception e) {
FormHelper.InfoBox(e.ToString());
return false;
}
}

}
}
30 changes: 30 additions & 0 deletions App/CommonCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,35 @@ internal static void CreateShortcut() {
internal static void VisitHomePage() {
System.Diagnostics.Process.Start(Constants.AppHomePage);
}

internal static void RightKey(Functions.AppOptionForm appOptionForm) {
bool isExist = RightKeyHelper.IsExist(Constants.AppEngName);
var path = System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
var exeAbsPath = FileHelper.CombinePath(path, Constants.AppEngName + ".exe");

if (isExist == true) {
bool isDelete = RightKeyHelper.Delete(Constants.AppEngName);
if (isDelete) {
FormHelper.InfoBox(Constants.AppEngName + "右键菜单删除成功。");
appOptionForm._RightKeyButton.Text = @"添加右键菜单";

}
}
else {
bool isAdd = RightKeyHelper.Add(Constants.AppEngName, exeAbsPath);
if (isAdd) {
FormHelper.InfoBox(Constants.AppEngName + "右键菜单创建成功。");
appOptionForm._RightKeyButton.Text = @"删除右键菜单";

}

}
}
internal static void RightKeyButton_Text(Functions.AppOptionForm appOptionForm) {
bool isExist = RightKeyHelper.IsExist(Constants.AppEngName);
if(isExist == true) {
appOptionForm._RightKeyButton.Text = @"删除右键菜单";
}
}
}
}
Loading