我自己写了注册表,让鼠标右键点击一个文件时,增加了一项“我的应用”到ContextMenu。
现在我的问题是,我选中一个文件,这样右键操作,则运行“我的应用“一次。
但是,如果我多选文件,再这样右键操作,则会运行”我的应用“N次。
我想问,有没有办法能在我多选的时候,让”我的应用“只运行一次且获得我多选的所有文件的命令行参数(这个文件的绝对路径)。求VB解决方案,最好有代码实例,万分感谢!
我自己已经查了很多,比如shell拓展,进程间通信,但是可能是我悟性不够,没太懂。
上面表述也许不够清楚,具体想实现的例子就是像 压缩软件 在多选文件压缩的时候的那个”添加到压缩文件“的效果。
stackoverflow上有人给出一个办法
编写一个vbs
set WshShell = WScript.CreateObject("WScript.Shell")
set WMIService = GetObject("winmgmts:root\cimv2")
dim FirstCommandLine, CommandLineParts, AllFiles
set AllInstances1 = WMIService.ExecQuery("Select CommandLine FROM Win32_Process WHERE CommandLine LIKE '%" & wscript.scriptname & "%'")
For Each item In AllInstances1
FirstCommandLine = item.CommandLine 'Get the command line of the first instance of this script
exit for
Next
if InStr(FirstCommandLine,WScript.Arguments.Unnamed(0)) then 'This is the first instance
WScript.Sleep 400
'Update instance list to check if other instances appeared
set AllInstances2 = WMIService.ExecQuery("Select CommandLine FROM Win32_Process WHERE CommandLine LIKE '%" & wscript.scriptname & "%'")
while AllInstances2.count > AllInstances1.count 'While other instances keep appearing
WScript.Sleep 400
'Keep updating instance list
set AllInstances1 = AllInstances2
set AllInstances2 = WMIService.ExecQuery("Select CommandLine FROM Win32_Process WHERE CommandLine LIKE '%" & wscript.scriptname & "%'")
wend
For Each item In AllInstances2
CommandLineParts = Split(item.CommandLine,"""")
AllFiles = AllFiles & """" & CommandLineParts(UBound(CommandLineParts)-1) & """ " 'Get all the file paths
Next
WshShell.Run("""Path\to your\application.exe"" /switches" & AllFiles) '这里是你实际要调用的用来打开文件的VB程序。用Command语句和Split得到文件
else 'This is not the first instance. Before exiting, sleep for a moment for the first one to detect.
WScript.Sleep 4000
end if
注册表中 wscript //nologo "Full Path\myscript.vbs" "%1" 作为命令行(Full Path\myscript.vbs是这个脚本的路径)
这里还有一个基于消息通讯的
https://stackoverflow.com/questions/21062046/open-multiple-files-via-shell-context-menu-as-params
不过是delphi写的,思路我看了下,VB应该也可以实现。