windows 如何从mime类型中获取系统文件图标?

f4t66c6m  于 2023-11-21  发布在  Windows
关注(0)|答案(1)|浏览(290)

我正在使用Windows应用程序,接收电子邮件.虽然这,我想在下载它们之前显示附件的图标.在那一刻,我有一个MimeType.
有没有办法只从MimeType中获取系统图标?
下载附件后,我可以使用SHGetFileInfo,但我想在下载之前有图标。

o7jaxewo

o7jaxewo1#

是的,您可以检索特定文件类型(由MIME类型确定)的系统图标,而无需实际将文件存储在磁盘上。要在Windows上实现此目的,您可以使用SHGetFileInfo函数沿着SHGFI_USEFILEATTRIBUTES标志。
下面是一个简单的C++例子:

  1. #include <Windows.h>
  2. #include <ShlObj.h>
  3. HICON GetIconForMimeType(const wchar_t* mimeType) {
  4. SHFILEINFO sfi;
  5. memset(&sfi, 0, sizeof(sfi));
  6. // Use the SHGFI_USEFILEATTRIBUTES flag to specify that we are providing a file type (MIME type)
  7. SHGetFileInfo(mimeType, FILE_ATTRIBUTE_NORMAL, &sfi, sizeof(sfi), SHGFI_ICON | SHGFI_USEFILEATTRIBUTES);
  8. // Check if the function succeeded in getting the icon
  9. if (sfi.hIcon)
  10. return sfi.hIcon;
  11. // Return a default icon if the function fails
  12. return LoadIcon(nullptr, IDI_APPLICATION);
  13. }
  14. int main() {
  15. // Example: Get icon for a JPEG image (change the MIME type accordingly)
  16. const wchar_t* mimeType = L"image/jpeg";
  17. HICON icon = GetIconForMimeType(mimeType);
  18. // Now you can use the 'icon' handle as needed (e.g., display it in your application)
  19. // ...
  20. // Don't forget to clean up the icon handle when you're done with it
  21. DestroyIcon(icon);
  22. return 0;
  23. }

字符串
image/jpeg替换为附件的MIME类型。此代码检索与指定文件类型关联的图标,而无需磁盘上的实际文件。
确保包含必要的头文件(Windows.hShlObj.h)并链接到所需的库。
请记住在应用程序中适当地处理错误和边缘情况。

展开查看全部

相关问题