如何使用Delphi从文件扩展名中获取图标和描述?
基本上我有一个 TcxGrid,它将列出各种文件名,我想根据文件扩展名提供进一步的详细信息,特别是它的描述(例如,对于 .PDF,它是“Adobe Acrobat Document”)及其相关图标。
我注意到已经有一个非常相似的问题< /a> 但它与 C# 相关,我想要一些基于 Delphi 的东西。
关于在哪里寻找此类信息的建议会很好,如果有一个类似于上面 C# 帖子中提到的类(显然是在 Delphi 中),那就太好了。
Basically I have a TcxGrid which will be listing various files names and I'd like to give further details based on the file extension, specifically it's description (e.g. for .PDF it's "Adobe Acrobat Document") and it's related icon.
I notice there is a very similar question already but it's C# related and I'd like something Delphi based.
Suggestions on where to look for this kind of info would be good and if there is a class similar to the one mentioned in the C# post above (obviously in Delphi) that would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
感谢 Rob Kennedy 为我指明了 ShGetFileInfo 的方向。 然后我在 Google 上搜索并找到了这两个示例 - Delphi 3000、托里的。 从那时起,我编写了以下课程来完成我需要的事情。
另外,就在我完成任务时,比尔·米勒的回答给了我我所需要的最后一点帮助。 最初,我将完整的文件名传递给 ShGetFileInfo,这并不是我想要的。 建议传递“*.EXT”的调整很棒。
该课程还可以做更多的工作,但它满足了我的需要。 它似乎可以处理没有相关详细信息的文件扩展名。
最后,在我正在使用的内容中,我将其切换为使用 TcxImageList 而不是 TImageList,因为我遇到了图标上出现黑色边框的问题,因为这是一个快速修复。
这里还有一个使用它的示例测试应用程序,它非常简单,只是一个带有 TPageControl 的表单。 我的实际用途不是为了这个,而是为了在 TcxGrid 中使用 Developer Express TcxImageComboxBox。
谢谢大家的回答!
Thanks to Rob Kennedy for pointing me in the direction of ShGetFileInfo. I then Googled on that and found these two examples - Delphi 3000, Torry's. From that I wrote the following class to do what I needed.
Also, just as I was finishing up Bill Miller's answer gave me the final bit of help I needed. Originally I was passing full file names through to ShGetFileInfo, which wasn't ideally what I wanted. The tweak suggested of passing "*.EXT" was great.
The class could do with more work but it does what I need. It seems to handle file extensions that have no details associated either.
Finally, in what I'm using I've switched it to using a TcxImageList instead of a TImageList, since I was having problems with black borders appearing on the icons, because it was a quick fix.
Also here is an example test app using it, it's very simple, just a form with a TPageControl on it. My actual use was not for this, but for with a Developer Express TcxImageComboxBox in a TcxGrid.
Thanks everyone for your answers!
调用
ShGetFileInfo
。 它可以告诉您描述(该函数词汇中的“类型名称”),并且可以为您提供图标句柄,或系统图像列表的句柄(图标所在的位置),或包含该图标的模块的路径图像资源。 该函数可以执行许多不同的操作,因此请务必仔细阅读文档。MSDN 说
ShGetFileInfo
“可能会很慢”并调用IExtractIcon
接口是一个“更灵活、更高效”的替代方案。 但它建议的顺序是使用IShellFolder
< /a> 接口,然后调用GetUIObjectOf
获取文件的IExtractIcon
接口,然后调用GetIconLocation
和提取< /code>
来检索图标的句柄。
据我所知,这正是
ShGetFileInfo
所做的,但它要麻烦得多,而且在完成所有这些之后,您仍然不会获得文件的类型描述。 坚持使用ShGetFileInfo,直到速度和效率成为一个明显的问题。Call
ShGetFileInfo
. It can tell you the description (the "type name," in that function's vocabulary), and it can give you an icon handle, or a handle to the system image list, where the icon resides, or the path to the module that holds the image resource. That function can do lots of different things, so make sure to read the documentation carefully.MSDN says
ShGetFileInfo
"may be slow" and calls theIExtractIcon
interface a "more flexible and efficient" alternative. But the sequence it recommends is to use anIShellFolder
interface, then callGetUIObjectOf
to get the file'sIExtractIcon
interface, and then callGetIconLocation
andExtract
on it to retrieve the icon's handle.For all I know, that's exactly what
ShGetFileInfo
does anyway, but it's much more cumbersome, and after you've done all that, you still wouldn't have the file's type description. Stick withShGetFileInfo
until speed and efficiency become a noticeable problem.听起来不是油嘴滑舌,但谷歌是你的朋友。 以下是“delphi 关联图标”的一些第一个结果:
http:// www.delphi3000.com/articles/article_453.asp?SK=
http: //www.jpgriffiths.com/tutorial/Snippets%5Cgetassociatedicon.html
Not to sound glib, but Google is your friend. Here are a couple of the first results for "delphi associated icon":
http://www.delphi3000.com/articles/article_453.asp?SK=
http://www.jpgriffiths.com/tutorial/Snippets%5Cgetassociatedicon.html
另一种方法是在注册表中的 HKEY_CLASSES_ROOT 下查找扩展名,然后按照默认值(如果有)中的键进行操作,其默认值是描述。 您还可以在第二个级别获取要打开的 shell 命令,或打印文件类型以及默认图标的路径。
The other method is to hunt down the extension in the registry under HKEY_CLASSES_ROOT, then follow the key in the default value (if available) and its default is the description. This second level is also where you can get the shell commands to open, or print the file type as well as the path to the default icon.
以下是来自 bitwisemag.com 的几个使用 ShGetFileInfo 的好示例:
http://www.bitwisemag.com bitwisemag.com/copy/delphi/lpad1.html
http://www .bitwisemag.com/copy/delphi/prog_groups2.html
Here are a couple good examples of using ShGetFileInfo from bitwisemag.com:
http://www.bitwisemag.com/copy/delphi/lpad1.html
http://www.bitwisemag.com/copy/delphi/prog_groups2.html