Returns a handle to the bitmap containing the Path Copy Copy icon that can be used for contextual menu items, loading it if necessary. @return Handle of bitmap containing the icon. Will be NULL if loading failed.
| 945 | // @return Handle of bitmap containing the icon. Will be NULL if loading failed. |
| 946 | // |
| 947 | HBITMAP CPathCopyCopyContextMenuExt::GetPCCIcon() |
| 948 | { |
| 949 | // Load on first call. |
| 950 | if (m_spPCCIcon == nullptr) { |
| 951 | // Load PNG resource. |
| 952 | HRSRC hPngRsrcInfo = ::FindResourceW(CPathCopyCopyModule::HInstance(), MAKEINTRESOURCEW(IDB_PCCICON2), L"PNG"); |
| 953 | if (hPngRsrcInfo != nullptr) { |
| 954 | HGLOBAL hPngRsrc = ::LoadResource(CPathCopyCopyModule::HInstance(), hPngRsrcInfo); |
| 955 | const DWORD pngSize = ::SizeofResource(CPathCopyCopyModule::HInstance(), hPngRsrcInfo); |
| 956 | if (hPngRsrc != nullptr && pngSize != 0) { |
| 957 | void* pPngData = ::LockResource(hPngRsrc); |
| 958 | if (pPngData != nullptr) { |
| 959 | // Store PNG data in a global block of memory. |
| 960 | StGlobalBlock globalPngData(GMEM_MOVEABLE, pngSize); |
| 961 | if (globalPngData.Get() != nullptr) { |
| 962 | StGlobalLock pngLock(globalPngData.Get()); |
| 963 | if (pngLock.GetPtr() != nullptr) { |
| 964 | ::memcpy(pngLock.GetPtr(), pPngData, pngSize); |
| 965 | } else { |
| 966 | globalPngData.Acquire(nullptr); |
| 967 | } |
| 968 | } |
| 969 | if (globalPngData.Get() != nullptr) { |
| 970 | // Create IStream object on this HGLOBAL to be able to pass it to GDI+. |
| 971 | ATL::CComPtr<IStream> cpPngStream; |
| 972 | if (SUCCEEDED(::CreateStreamOnHGlobal(globalPngData.Get(), FALSE, &cpPngStream))) { |
| 973 | // Init GDI+ to be able to use its calls, since shell doesn't do it. |
| 974 | StGdiplusStartup gdiPlusStartup; |
| 975 | if (gdiPlusStartup.Started()) { |
| 976 | // Extract HBITMAP using GDI+. |
| 977 | HBITMAP hPngBitmap = nullptr; |
| 978 | Gdiplus::Bitmap pngBitmap(cpPngStream, FALSE); |
| 979 | if (pngBitmap.GetHBITMAP(Gdiplus::Color(), &hPngBitmap) == Gdiplus::Ok) { |
| 980 | // We have HBITMAP, save it in our member. |
| 981 | m_spPCCIcon = std::make_shared<StImage>(hPngBitmap, IMAGE_BITMAP, false); |
| 982 | } |
| 983 | } |
| 984 | } |
| 985 | } |
| 986 | } |
| 987 | } |
| 988 | } |
| 989 | } |
| 990 | return (m_spPCCIcon != nullptr && m_spPCCIcon->GetLoadResult() == ERROR_SUCCESS) ? m_spPCCIcon->GetBitmap() : nullptr; |
| 991 | } |
| 992 | |
| 993 | // |
| 994 | // Given the path to an icon file, returns a handle to a Win32 bitmap for that icon |