Josh Posted February 24, 2021 Share Posted February 24, 2021 There are too many unknown factors to make this code part of the official API yet, so I am posting it here for now: #include "UltraEngine.h" #include "GetFileIcon.h" #ifdef _WIN32 #include <commoncontrols.h> #endif namespace UltraEngine::Editor { shared_ptr<Pixmap> GetFileIcon(const WString& file, const int size) { shared_ptr<Pixmap> pixmap; #ifdef _WIN32 CoInitialize(nullptr); WString path = RealPath(file).Replace("/", "\\"); IShellItemImageFactory* pImageFactory; IShellItem* psiFile; LPCWSTR szFilePath = path.Data(); auto hr = SHCreateItemFromParsingName(szFilePath, NULL, IID_PPV_ARGS(&pImageFactory)); if (SUCCEEDED(hr)) { HBITMAP hbmp; SIZE sz = { size,size }; //SIIGBF_BIGGERSIZEOK //SIIGBF_RESIZETOFIT //this may invert some images hr = pImageFactory->GetImage(sz, SIIGBF_RESIZETOFIT | SIIGBF_BIGGERSIZEOK, &hbmp); if (SUCCEEDED(hr)) { BITMAP bminfo = {}; if (GetObjectW(hbmp, sizeof(bminfo), &bminfo) != 0) { auto hdc = GetDC(GetDesktopWindow()); BITMAPINFO bmi = {}; bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader); auto r = GetDIBits(hdc, hbmp, 0, 1, nullptr, &bmi, DIB_RGB_COLORS); TextureFormat format = TextureFormat(0); if (bminfo.bmBitsPixel == 24) format = TEXTURE_BGR; if (bminfo.bmBitsPixel == 32) format = TEXTURE_BGRA; bmi.bmiHeader.biHeight = -Abs(bmi.bmiHeader.biHeight); bmi.bmiHeader.biCompression = BI_RGB; if (format != 0) { pixmap = CreatePixmap(bminfo.bmWidth, bminfo.bmHeight, format); Assert(pixmap->pixels->GetSize() == bmi.bmiHeader.biSizeImage); auto r = GetDIBits(hdc, hbmp, 0, bminfo.bmHeight, pixmap->pixels->Data(), &bmi, DIB_RGB_COLORS); } } DeleteObject(hbmp); } pImageFactory->Release(); } #endif return pixmap; } } Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Josh Posted April 21, 2023 Author Share Posted April 21, 2023 Updated to fix flipped images. Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.