如何为 Java 应用程序向 Windows 资源管理器添加上下文菜单?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/370114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:44:21  来源:igfitidea点击:

How can I add a context menu to the Windows Explorer for a Java application?

javawindowscontextmenuwindows-explorer

提问by Thilo

How would one go about adding a submenu item to the windows explorer context menu (like for example 7-Zip does) for a Java application?

如何将子菜单项添加到 Java 应用程序的 Windows 资源管理器上下文菜单(例如 7-Zip 所做的)?

采纳答案by Jayden

I am aware of two ways to do it. The fancy way is to write a windows shell extension, which is how powerarchiver, winzip etc do it I believe (this involves running code to determine what the context menu items will be dependent on the file chosen).

我知道有两种方法可以做到。奇特的方法是编写一个 windows shell 扩展,我相信这是 powerarchiver、winzip 等如何做到的(这涉及运行代码以确定上下文菜单项将依赖于所选文件)。

The simple way, for simple functionality, is you can add an entry in the registry :

对于简单的功能,最简单的方法是您可以在注册表中添加一个条目:

HKEY_CLASSES_ROOT\<file type>\shell\<display text>\command

Where <file type>is the files that this context menu should apply to i.e. *, .mdb, .doc

<file type>此上下文菜单应应用于的文件在哪里,即 *、.mdb、.doc

and

<display text>what you want to show in the context menu.

<display text>您想在上下文菜单中显示的内容。

Then add the default string as a path to the application you want to launch from the context menu, and you can use %1 to refer to the currently selected file i.e. for MS Access I use :

然后将默认字符串添加为要从上下文菜单启动的应用程序的路径,您可以使用 %1 来引用当前选定的文件,即我使用的 MS Access:

HKEY_CLASSES_ROOT\*\shell\MS Access 2000\command
"C:\Program Files\Microsoft Office\Office\MSACCESS.EXE" "%1"

This then adds a context menu item for any file I select (hence the *), which allows me to launch it in MS Access 2000.

然后为我选择的任何文件(因此是 *)添加一个上下文菜单项,这允许我在 MS Access 2000 中启动它。

Of course, always back up your registry before hacking it.

当然,在入侵之前一定要备份你的注册表。

Your program could do this during install, or on first run.

您的程序可以在安装期间或首次运行时执行此操作。

回答by Harkish

You could also package the java program in an installer like NSIS and you could use NSIS script to generate explorer context menu

您还可以在 NSIS 等安装程序中打包 java 程序,您可以使用 NSIS 脚本生成资源管理器上下文菜单