在使用Java Windows中更改文件图标 [英] Change File Icon in windows using Java

查看:298
本文介绍了在使用Java Windows中更改文件图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

项目名称:文件标记

我个人使用文件夹标记组织我的文件夹。它给我的选择来改变从上下文菜单(右键菜单)的文件夹图标。
我可以从文本菜单中选择计划工作​​,做了一半的工作,所做的工作,并根据我的文件夹图标的变化。

现在我想要实现单个文件相同的概念。

我想用Java来实现在Windows环境下这种解决方案。

第1步:
要在上下文菜单中添加的东西,最简单的方法是启动一个注册表项。结果
所以我在这里需要做的是 -

添加上下文菜单项:文件标记
添加上下文子菜单项:计划的工作,做了一半的工作,所做的工作(文件标记的子菜单)

第二步:

添加自定义属性。

如果(WorkStatus的工作计划){
设置一个特定的图标,其值为计划的工作文件
我想我需要使用iconHandler这里
}

我需要你对如何实现它的建议。

这是该解决方案的正确途径。还是有更好的解决方案?

我也很高兴能有样品code的第一步和第二步

谢谢
@布赖恩


解决方案

嗯,我有一些好消息和一些坏消息...

关于你的第2步(设置为特定的文件图标):

既然你是在谈论注册表项,我假设你是在Windows下。在这种情况下,恐怕你不能改变一个特定的文件的图标,除非它是一个快捷方式或.exe或.DDL文件(该文件可以包含资源,如图标)。您的什么能的变化,为在文件类型的默认图标,这意味着同一类型的所有文件会受到影响(这显然不符合您的要求)。

关于你的第1步(修改上下文菜单):

为了在 Windows 7或更高版本,以达到你所描述的结构(文件制作子菜单,为各种状态的多个条目(计划的工作,半完成的工作等等)),你需要创建一个静态的级联菜单中描述的此处。 (在WindowsXP和更早的版本,你需要实现一个ContextMenuHandler这是一个显著更复杂的任务 - 这我不打算在这个asnwer覆盖)。

样品code

您可以从命令提示符运行以下命令(有管理员权限)或.bat文件复制并粘贴命令,然后运行它以管理员身份。了这些命令是:先注册(在注册表项中看到*)的每个文件和可用的子菜单,条目的子菜单,然后为每个子入境显示文本和命令。

注意的:你是劝在进行任何修改之前总是备份注册表。注册表是一个微妙的结构,所以请句柄将额外照顾和您自己的风险:)

  REG ADDHKEY_CLASSES_ROOT \\ * \\壳\\文件标记^
/ VMUIVerb/吨REG_SZ / d的文件标记/˚FREG ADDHKEY_CLASSES_ROOT \\ * \\壳\\文件标记^
/ V子命令/吨REG_EXPAND_SZ / DWinIconChanger.PLANNED_WORK; WinIconChanger.HALF-DONE_WORK; WinIconChanger.DONE_WORK/˚FREG ADD \"HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CommandStore\\shell\\WinIconChanger.PLANNED_WORK\" ^
/ VMUIVerb/吨REG_SZ / d的工作计划/˚F
REG ADD \"HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CommandStore\\shell\\WinIconChanger.PLANNED_WORK\\command\" ^
/ VE /吨REG_SZ / d的\\C:\\路径\\为\\ JRE \\ BIN \\ javaw.exe的\\-jar \\C:\\路径\\为\\ WinIconChanger.jar \\\\PLANNED_WORK \\\\% %1 \\/˚FREG ADD \"HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CommandStore\\shell\\WinIconChanger.HALF-DONE_WORK\" ^
/ VMUIVerb/吨REG_SZ / d的半完成的工作/˚F
REG ADD \"HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CommandStore\\shell\\WinIconChanger.HALF-DONE_WORK\\command\" ^
/ VE /吨REG_SZ / d的\\C:\\路径\\为\\ JRE \\ BIN \\ javaw.exe的\\-jar \\C:\\路径\\为\\ WinIconChanger.jar \\\\半DONE_WORK \\\\ %% 1 \\/˚FREG ADD \"HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CommandStore\\shell\\WinIconChanger.DONE_WORK\" ^
/ VMUIVerb/吨REG_SZ / d的完成工作/˚F
REG ADD \"HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CommandStore\\shell\\WinIconChanger.DONE_WORK\\command\" ^
/ VE /吨REG_SZ / d的\\C:\\路径\\为\\ JRE \\ BIN \\ javaw.exe的\\-jar \\C:\\路径\\为\\ WinIconChanger.jar \\\\DONE_WORK \\\\% %1 \\/˚F暂停

不要忘了替换C:\\路径\\为\\ WinIconChanger.jar和C:\\路径\\为\\ JRE \\ BIN \\ javaw.exe的在你的系统中的实际路径
<。 / p>

为了撤消完成注册表,您可以执行这些命令的修改(或者将它们保存在一个.bat文件并运行它)有管理权限的:

  REG DELETEHKEY_CLASSES_ROOT \\ * \\壳\\文件标记/˚FREG DELETE \"HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CommandStore\\shell\\WinIconChanger.PLANNED_WORK\" /F
REG DELETE \"HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CommandStore\\shell\\WinIconChanger.HALF-DONE_WORK\" /F
REG DELETE \"HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CommandStore\\shell\\WinIconChanger.DONE_WORK\" /F暂停



当然,你需要将采取两个参数,工作即状态(例如PLANNED_WORK,DONE_WORK等)和路径右键单击文件中的.jar文件,并做一些有用的东西与他们(如果你只能改变该文件的图标:D)

下面是一个例子类

 类WinIconChanger {
    静态公共无效的主要(字串[] args){
        字符串文件路径=未知;
        字符串状态=未知;
        如果(args.length == 2){
            文件路径= ARGS [1];            开关(参数[0]){
            案PLANNED_WORK:
            案半DONE_WORK:
            案DONE_WORK:
                状态= ARGS [0];
                打破;
            默认:
                打破;
            }
        }        如果(未知.equals(州)){
            javax.swing.JOptionPane.showMessageDialog(
                    空值,
                    未知文件或状态!
                    WinIconChanger错误,
                    javax.swing.JOptionPane.ERROR_MESSAGE);
        }其他{
            / *
             *更改args中指定的文件的图标[1]
             *根据args中指定的状态[0]
             * /
            弦乐味精=的String.format(
                    假设我只是改变了'%s'的图标到%s!,
                    文件路径,状态);
            javax.swing.JOptionPane.showMessageDialog(
                    空值,
                    味精,
                    WinIconChanger信息,
                    javax.swing.JOptionPane.INFORMATION_MESSAGE);
        }
    }
}

(这只是一个粗略的例子,而不是生产就绪code。你需要验证的第二个参数确实对应的路径到现有文件,微调错误的消息,捕获异常等。)

Project Name: File Marker

I personally organize my folders using "Folder Marker". It gives me option to change the folder icon from context menu (right-click menu). I can select planned work, half-done work, done work from contextmenu and my folder icon change according.

Now I want to implement the same concept for individual files.

I want to use Java to implement this solution in windows environment.

Step 1: To add something in the context menu, the easiest way is to initiate a registry entry.
So what i need to do here is --

Add Context Menu item : File Marker Add Context SubMenu item : planned work, half-done work, done work (submenu of File Marker)

step 2:

Add custom attribute

if(WorkStatus is "planned work") { set an specific icon for the file whose value is "planned work" I think i need to use iconHandler Here }

I need your suggestion on how to implement it.

Is it the right approach for this solution. Or is there a better solution?

I also would be glad to have sample code for step1 and step2

Thanks @Brian

解决方案

Well, I have some good news and some bad news...

Regarding your Step 2 (Setting icon for specific file):

Since you are talking about "Registry entries", I assume you are under Windows. In that case, I am afraid you cannot change the icon of a specific file, unless it is a shortcut or a .EXE or .DDL file (which can contain resources, such as icons). What you can change, is the default icon for the file type, meaning all files of the same type would be affected (and that would clearly not match your requirement).

Regarding your Step 1 (Modifying the context menu):

In order to achieve the structure you described (a "File Maker" submenu, with multiple entries for the various states ("Planned work", "Half-done work" etc)) in Windows 7 or later, you need to create a static Cascading Menu as described here. (In WindowsXP and earlier you need to implement a ContextMenuHandler which is a significantly more complex task - which I do not intend to cover in this asnwer).

Sample code:
You can run the following commands from a command prompt (with administrative priviledges) or copy and paste the commands in a .BAT file and then run it as administrator. What these commands do is: first register a submenu for each file (see "*" in registry key) and the available submenu-entries and then set the display-text and command for every submenu-entry.

CAUTION: You are adviced to always back-up the registry before making any modifications. The registry is a delicate construct, so please handle will extra care and at your own risk :)

REG ADD "HKEY_CLASSES_ROOT\*\shell\File Marker" ^
/v "MUIVerb" /t REG_SZ /d "File Marker" /f

REG ADD "HKEY_CLASSES_ROOT\*\shell\File Marker" ^
/v "SubCommands" /t REG_EXPAND_SZ /d "WinIconChanger.PLANNED_WORK;WinIconChanger.HALF-DONE_WORK;WinIconChanger.DONE_WORK" /f

REG ADD "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\WinIconChanger.PLANNED_WORK" ^
/v "MUIVerb" /t REG_SZ /d "Planned work" /f
REG ADD "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\WinIconChanger.PLANNED_WORK\command" ^
/ve /t REG_SZ /d "\"C:\path\to\jre\bin\javaw.exe\" -jar \"C:\path\to\WinIconChanger.jar\" \"PLANNED_WORK\" \"%%1\"" /f

REG ADD "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\WinIconChanger.HALF-DONE_WORK" ^
/v "MUIVerb" /t REG_SZ /d "Half-done work" /f
REG ADD "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\WinIconChanger.HALF-DONE_WORK\command" ^
/ve /t REG_SZ /d "\"C:\path\to\jre\bin\javaw.exe\" -jar \"C:\path\to\WinIconChanger.jar\" \"HALF-DONE_WORK\" \"%%1\"" /f

REG ADD "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\WinIconChanger.DONE_WORK" ^
/v "MUIVerb" /t REG_SZ /d "Done work" /f
REG ADD "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\WinIconChanger.DONE_WORK\command" ^
/ve /t REG_SZ /d "\"C:\path\to\jre\bin\javaw.exe\" -jar \"C:\path\to\WinIconChanger.jar\" \"DONE_WORK\" \"%%1\"" /f

PAUSE

Do not forget to replace "C:\path\to\WinIconChanger.jar" and "C:\path\to\jre\bin\javaw.exe" with the actual paths in your system.

In order to undo the modifications done to the registry, you can execute these commands (or save them in a .BAT file and run it) with administrative priviledges:

REG DELETE "HKEY_CLASSES_ROOT\*\shell\File Marker" /f

REG DELETE "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\WinIconChanger.PLANNED_WORK" /f
REG DELETE "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\WinIconChanger.HALF-DONE_WORK" /f
REG DELETE "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\WinIconChanger.DONE_WORK" /f

PAUSE


Of course, you will need a .JAR file that will take two arguments, namely the state of work (e.g. PLANNED_WORK, DONE_WORK etc) and the path to the right-clicked file, and does something useful with them (if you could only change the icon of the file :D).
Here is an example class:

class WinIconChanger {
    static public void main(String[] args) {
        String filePath = "UNKNOWN";
        String state = "UNKNOWN";
        if (args.length == 2) {
            filePath = args[1];

            switch (args[0]) {
            case "PLANNED_WORK":
            case "HALF-DONE_WORK":
            case "DONE_WORK":
                state = args[0];
                break;
            default:
                break;
            }
        }

        if ("UNKNOWN".equals(state)) {
            javax.swing.JOptionPane.showMessageDialog(
                    null, 
                    "Unknown file or state !", 
                    "WinIconChanger Error", 
                    javax.swing.JOptionPane.ERROR_MESSAGE);
        } else {
            /* 
             * Change the icon of the file specified in args[1]
             * according to the state specified in args[0]
             */
            String msg = String.format(
                    "Let's assume I just changed the icon of '%s' to %s !",
                    filePath, state);
            javax.swing.JOptionPane.showMessageDialog(
                    null, 
                    msg,
                    "WinIconChanger Info",
                    javax.swing.JOptionPane.INFORMATION_MESSAGE);
        }
    }
}

(This is just a sketchy example and not production-ready code. You need to verify that the second argument does indeed correspond to a path to an existing file, fine-tune the error-messages, catch Exceptions etc.)

这篇关于在使用Java Windows中更改文件图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆