开发者

How to get the associated icon of a file in HTA (HTML/Javascript/VBScript)

I'm building an HTA application in which I need to display a list of file with their associated system icon.

I'm using FileSystemObject to list the file but there seem to have no way to get the icon...

I've found a script in VBS that can save the icon of a file into a .ico .

It read the file (PE resource file, .exe or dll) and parse the icon data. I modified that script to return the icon's bytes, convert it to base64 and use embed base64 images in HTML. Here's the original script: http://gilpin.us/IconSiphon/

Issue

  1. ) In most case the .ico contains multiple icons (many sizes and color depth) but there's no way I can specify which one to use (as I need 16x16 icons).

  2. ) Not all icons are displayed

  3. ) Could be slow with many file as it read exe and dll (but I'm ok with that, I can cache already fetched icon)

I've also tried some ActiveX control but none seem to work properly. Even those provided by microsoft (ShellFolderView or ListView) are very buggy.

Requirements

  • Must display 16x16 icon

  • Must allow multiple file selection

  • Everything must be embed in hta (if possible). No external .exe

Does anyone know a way t开发者_StackOverflow中文版o achieve that?

Thanks!


Use SHGetFileInfo() with the SHGFI_ICON flag.

http://msdn.microsoft.com/en-us/library/windows/desktop/bb762179(v=vs.85).aspx


The filesystemobject will provide you the necessary functions for enumerating files on the local filesystem. However to get the icon image you will need to use the win32 api per @seanchase's response or an external exe.

However you can access the win32api via javascript in the hta using the wshApiToolkit activex object - http://www.google.com/search?q=wshAPIToolkit.ucATO%2F&rls=com.microsoft:en-us&ie=UTF-8&oe=UTF-8&startIndex=&startPage=1

Find a copy of that and you're close to being done. It does require distributing the activex object with your code and shell executing the registration process from within the HTA so that might violate your third constraint. Though I believe you can base64 encode the exe into the hta in a dataurl and write that back out to the file system so it would at least be bundled into a single file. If you support that option then maybe embedding an exe that does the same would meet your requriements.

Definitely some hacky stuff that may be unstable on future OS versions - heck I'm not even sure the wshApiToolkit works on windows 7, and 8 is just around the corner. Good luck!


You indicated you're opened to installing ActiveX components and using them in your HTA.

If I had the time, I would approach this for myself by creating ActiveX components using Visual Studio to call FindResource, LoadResource and LockResource. These will enable access to the Group Icon resource for which I would then provide rich interfaces to iterate through the Icons offering the ability to extract BMPs (or PNGs).

This is "how" I would go about achieving this short of actually going off doing it.


Once I build a similar HTA interface and I faced the same problem. I solved the problem by creating a custom icon gallery and converting the images using base64. You may achieve the same by either converting or using sprite. Many UI does it, even java.swing has its own collection embbebed. As you noticed, reading from *.dll can speed down the application

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜