标签:NPU setimage ctr temp sizeof stc let path span
我们知道通过CImageList可以让listctr显示出图片,但是添加的图片大小必须和要CImageList 创建的图片大小一致,才能显示出来。最近遇到一个需求,需要把很多大小不一的jpeg图片通过列表框来显示出缩略图。
首先创建指定大小的CImageList
1 void CListEx::setCreateImageList(int iWidth, int iheight) 2 { 3 m_iImageWidth = iWidth; 4 m_iImageHeight = iheight; 5 m_imageList.Create(m_iImageWidth, m_iImageHeight, ILC_COLOR32, 20, 1); 6 SetImageList(&m_imageList, LVSIL_SMALL); 7 }
采用Gdi来缩放的,路径为图片的路径,jpeg也支持,不需要转换格式
void CListEx::AddImage(LPCSTR imagePath) { WCHAR path[512] = { 0 }; ::MultiByteToWideChar(CP_ACP, 0, (const char *)imagePath, strlen(imagePath), path, sizeof(path)); Gdiplus::Bitmap bmp(path); int sourceWidth = m_iImageWidth; int sourceHeight = bmp.GetHeight(); if (sourceHeight > m_iImageHeight) { sourceHeight = m_iImageHeight; } else { sourceHeight = bmp.GetHeight(); } //设定缩略图的大小 Gdiplus::Bitmap* pThumbnail = (Gdiplus::Bitmap*)bmp.GetThumbnailImage(sourceWidth, sourceHeight, NULL, NULL); HBITMAP hBmp; pThumbnail->GetHBITMAP(Gdiplus::Color(LIST_BKCOLOR), &hBmp); CBitmap *pImage = CBitmap::FromHandle(hBmp); m_imageList.Add(pImage, RGB(255, 0, 255)); // 下面的代码,如果没有,会产生内存泄漏 delete pThumbnail; pThumbnail = NULL; pImage->DeleteObject(); pImage->DeleteTempMap(); }
别忘了初始化gdi,在CXXXApp::InitInstance()里添加初始化代码
//初始化Gdi+ Gdiplus::GdiplusStartupInput gdiplusStartupInput; Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
在CXXXApp::ExitInstance()里添加关闭gdi代码
Gdiplus::GdiplusShutdown(m_gdiplusToken);
在CXXXApp的头文件里添加m_gdiplusToken的声明
ULONG_PTR m_gdiplusToken;
标签:NPU setimage ctr temp sizeof stc let path span
原文地址:https://www.cnblogs.com/ahcc08/p/10987829.html