Skip to content

Commit bceb5c2

Browse files
authored
Merge pull request Jack251970#77 from LanYun2022/fix
修复使用VBScript实现的右键功能在Windows11失效的问题
2 parents 825273e + a5636ec commit bceb5c2

3 files changed

Lines changed: 63 additions & 5 deletions

File tree

ContextMenuManager/Controls/ShellExecuteDialog.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,37 @@ protected override bool RunDialog(IntPtr hwndOwner)
2929

3030
public static string GetCommand(string fileName, string arguments, string verb, int windowStyle, string directory = null)
3131
{
32-
arguments = arguments.Replace("\"", "\"\"");
3332
if (directory == null)
3433
{
3534
ObjectPath.GetFullFilePath(fileName, out var filePath);
3635
directory = Path.GetDirectoryName(filePath);
3736
}
38-
return "mshta vbscript:createobject(\"shell.application\").shellexecute" +
39-
$"(\"{fileName}\",\"{arguments}\",\"{directory}\",\"{verb}\",{windowStyle})(close)";
37+
38+
if (Environment.OSVersion.Version.Major >= 10)
39+
{
40+
string winStyleStr;
41+
switch (windowStyle)
42+
{
43+
case 0: winStyleStr = "Hidden"; break;
44+
case 1: winStyleStr = "Normal"; break;
45+
case 2: winStyleStr = "Minimized"; break;
46+
case 3: winStyleStr = "Maximized"; break;
47+
default: winStyleStr = "Normal"; break;
48+
}
49+
50+
string psFileName = "'" + fileName.Replace("'", "''") + "'";
51+
string psDir = string.IsNullOrEmpty(directory) ? "$null" : "'" + directory.Replace("'", "''") + "'";
52+
string psVerb = "'" + verb.Replace("'", "''") + "'";
53+
string psArgs = "'" + arguments.Replace("'", "''") + "'";
54+
55+
return $"powershell -WindowStyle Hidden -Command \"Start-Process -FilePath {psFileName} -ArgumentList {psArgs} -WorkingDirectory {psDir} -Verb {psVerb} -WindowStyle {winStyleStr}\"";
56+
}
57+
else
58+
{
59+
arguments = arguments.Replace("\"", "\"\"");
60+
return "mshta vbscript:createobject(\"shell.application\").shellexecute" +
61+
$"(\"{fileName}\",\"{arguments}\",\"{directory}\",\"{verb}\",{windowStyle})(close)";
62+
}
4063
}
4164

4265
private sealed class ShellExecuteForm : RForm

ContextMenuManager/Methods/ObjectPath.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using BluePointLilac.Methods;
1+
using BluePointLilac.Methods;
22
using Microsoft.Win32;
33
using System;
44
using System.Collections.Generic;
@@ -13,6 +13,8 @@ internal static class ObjectPath
1313
public enum PathType { File, Directory, Registry }
1414
private const string RegAppPath = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths";
1515
private const string ShellExecuteCommand = "mshta vbscript:createobject(\"shell.application\").shellexecute(\"";
16+
private const string PowerShellCommandPrefix = "powershell -WindowStyle Hidden -Command \"Start-Process";
17+
1618

1719
private static readonly char[] IllegalChars = { '/', '*', '?', '\"', '<', '>', '|' };
1820
private static readonly List<string> IgnoreCommandParts = new() { "", "%1", "%v" };
@@ -76,6 +78,24 @@ public static string ExtractFilePath(string command)
7678
}
7779
}
7880
}
81+
else if (partCmd.StartsWith(PowerShellCommandPrefix, StringComparison.OrdinalIgnoreCase))
82+
{
83+
int idx = partCmd.IndexOf("-FilePath '", StringComparison.OrdinalIgnoreCase);
84+
if (idx != -1)
85+
{
86+
int start = idx + 11;
87+
int end = partCmd.IndexOf("'", start);
88+
if (end != -1)
89+
{
90+
var fileName = partCmd.Substring(start, end - start).Replace("''", "'");
91+
if (GetFullFilePath(fileName, out filePath))
92+
{
93+
FilePathDic.Add(command, filePath);
94+
return filePath;
95+
}
96+
}
97+
}
98+
}
7999

80100
var strs = Array.FindAll(partCmd.Split(IllegalChars), str
81101
=> IgnoreCommandParts.Any(part => !part.Equals(str.Trim()))).Reverse().ToArray();

ContextMenuManager/Properties/Resources/Texts/EnhanceMenusDic.xml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<!--此文件为常用右键菜单字典, 可为此字典添加多语言翻译,添加一个Culture子项并设置为en-US、ja-JP等即可
33
Tip属性为鼠标悬浮在开关上时的提示信息,从每个Item节点开始, 子元素Value表示该项的注册表键值,目前仅支持REG_SZ、REG_DWORD、REG_EXPAND_SZ、REG_BINARY的键值类型,
44
子元素SubKey的所有子元素是该项的子项,项名即为元素名; 每一Item项和SubKey的所有子元素的属性Default为该注册表项默认值,不放在Value\REG_SZ元素里面是为了防止与可能存在的键名为Default的键产生冲突
@@ -792,6 +792,7 @@ Tip属性为鼠标悬浮在开关上时的提示信息,从每个Item节点开
792792
</Value>
793793
</Item>
794794
<Item KeyName='CopyAsPath'>
795+
<OSVersion Compare="&lt;">10.0</OSVersion>
795796
<Tip Value='系统原生菜单项需按住Shift显示,&#x000A;此项可以直接显示, 路径不带双引号'/>
796797
<Tip Value='The system raw lettuce item needs to hold down Shift display,&#x000A;this item can be displayed directly, path without double quotation marks'>
797798
<Culture>en-US</Culture>
@@ -804,6 +805,20 @@ Tip属性为鼠标悬浮在开关上时的提示信息,从每个Item节点开
804805
<Command Default='mshta vbscript:clipboarddata.setdata("text","%1")(close)'/>
805806
</SubKey>
806807
</Item>
808+
<Item KeyName='CopyAsPath'>
809+
<OSVersion Compare="&gt;=">10.0</OSVersion>
810+
<Tip Value='系统原生菜单项需按住Shift显示,&#x000A;此项可以直接显示, 路径不带双引号'/>
811+
<Tip Value='The system raw lettuce item needs to hold down Shift display,&#x000A;this item can be displayed directly, path without double quotation marks'>
812+
<Culture>en-US</Culture>
813+
</Tip>
814+
<Value>
815+
<!--复制路径-->
816+
<REG_SZ MUIVerb='@shell32.dll,-30329' Icon='imageres.dll,-5302'/>
817+
</Value>
818+
<SubKey>
819+
<Command Default='powershell -WindowStyle Hidden -Command "Set-Clipboard -Value $args[0]" "%1"'/>
820+
</SubKey>
821+
</Item>
807822
<Item KeyName='Windows.CopyAsPath'>
808823
<OSVersion Compare="&gt;=">6.2</OSVersion>
809824
<Tip Value='系统原生菜单项需按住Shift显示,&#x000A;此项可以直接显示, 路径带双引号'/>

0 commit comments

Comments
 (0)