CImageList 常见问题解决方法

来源:互联网 发布:js中字符串是引用类型 编辑:程序博客网 时间:2024/06/16 19:14

这两天被这个控件折磨好了好一阵 终于实现了功能。
使用指南以及一些技巧其他地方有很多讲解了。
所以现在来总结一下使用CImageList的时候常见的问题吧。


问题1:彻底清空CImageList
在使用的时候被这个问题困扰了蛮久

if (m_pImageList!=NULL){int len = m_pImageList->GetImageCount();for(int kk=0;kk<len;kk++)m_pImageList->Remove(0);m_MyList.DeleteAllItems();}
m_pImageList->Remove(0);
我原来写的时候是
m_pImageList->Remove(i);
这也是msdn上面的做法:
// Remove every other image from the image list.for (int i = 0; i < m_myImageList.GetImageCount(); i++){   m_myImageList.Remove(i);}
但是这样做在更新的时候出现很大的问题,就是清除不完整。导致后面读取新的图片的时候出现了错误。
看看MSDN上面的这句:
All items following nImage now move down one position. For example, if an image list contains two items, 
deleting the first item will cause the remaining item to now be in the first position. nImage=0 for the item in the first position.
如果我的理解没错的话应该是删除了一个元素后,排在它后面的元素将会处于被删除的元素的位置,也就是向前移动了。
所以
m_myImageList.Remove(i);
是删除不完全的。所以正确的做法是
m_pImageList->Remove(0);

问题2:如何使用CImageList读取png,jpg,等格式的图片

我也是借鉴了网上的代码,使用GDI+来处理,效果不错。

关于GDI+的使用网上也有很多,在这里就不介绍了。

BOOL CPictrueBrowserDlg::LoadBitmapToList(CString strFileName,int i){Bitmap bmp(strFileName);                //传入图片路径int sourceWidth = 32;                            //获得图片宽度,这个必须和Create的时候设定的宽度一样。否则有些图片不能正确显示!int sourceHeight = 32;                 //因为我Create的是32*32的像素,所以这里就直接设定成32                                Bitmap* pThumbnail = (Bitmap*)bmp.GetThumbnailImage(sourceWidth , sourceHeight , NULL, NULL); //设定缩略图的大小HBITMAP hBmp;pThumbnail->GetHBITMAP(Color(255,255,255),&hBmp);CBitmap *pImage = CBitmap::FromHandle(hBmp);         //转换成CBitmap格式位图int a=m_pImageList->Add(pImage,RGB(255,255,255));pImage->DeleteObject();return true;}

最后给出一个图片浏览器的源码,可以实现显示缩略图,预览功能。

http://download.csdn.net/detail/antion692980794/4042057


	
				
		
原创粉丝点击