我有一个传统的WTL应用程序,我希望使用CToolBarCtrl http://msdn.microsoft.com/en-us/library/tf5d6ca2(v=vs.80).aspx类在工具栏中显示png图像。截至目前,BMP图像显示在工具栏中(通过调用LoadToolBar函数并在资源中使工具栏可用)。
我想修改它以使用PNG图像。我曾尝试使用CToolBarCtrl类来加载png图像但是徒劳无功。有办法吗?
答案 0 :(得分:1)
Here是将PNG转换为HBITMAP的示例:
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")
ATLINLINE HBITMAP AtlLoadGdiplusImage(ATL::_U_STRINGorID bitmap, ATL::_U_STRINGorID type = (UINT) 0)
{
USES_CONVERSION;
static bool s_bInitied = false;
if( !s_bInitied ) {
s_bInitied = true;
Gdiplus::GdiplusStartupInput gsi;
Gdiplus::GdiplusStartupOutput gso;
ULONG uToken = 0;
Gdiplus::GdiplusStartup(&uToken, &gsi, &gso);
}
Gdiplus::Bitmap* pBitmap = NULL;
if( HIWORD(bitmap.m_lpstr) != NULL ) {
// Load from filename
pBitmap = new Gdiplus::Bitmap(T2CW(bitmap.m_lpstr));
}
else if( type.m_lpstr != NULL && type.m_lpstr != RT_BITMAP ) {
// Loading PNG, JPG resources etc
WTL::CResource res;
if( !res.Load(type, bitmap) ) return NULL;
DWORD dwSize = res.GetSize();
HANDLE hMemory = ::GlobalAlloc(GMEM_MOVEABLE, dwSize);
if( hMemory == NULL ) return NULL;
::memcpy(::GlobalLock(hMemory), res.Lock(), dwSize);
::GlobalUnlock(hMemory);
IStream* pStream = NULL;
if( FAILED( ::CreateStreamOnHGlobal(hMemory, TRUE, &pStream) ) ) {
::GlobalFree(hMemory);
return FALSE;
}
pBitmap = new Gdiplus::Bitmap(pStream);
pStream->Release();
}
else {
// This only loads BMP resources
pBitmap = new Gdiplus::Bitmap(_Module.GetResourceInstance(), (LPCWSTR) (UINT) bitmap.m_lpstr);
}
if( pBitmap == NULL ) return NULL;
HBITMAP hBitmap = NULL;
pBitmap->GetHBITMAP(NULL, &hBitmap);
delete pBitmap;
return hBitmap;
}