资源中的JPG在CListCtrl中显示

 
 
#include <afxpriv2.h>


#define THUMWIDTH 100 
#define THUMHEIGHT 100
//////////////////
// Load from stream (IStream). This is the one that really does it: call
// OleLoadPicture to do the work.
//
HANDLE LoadPicture(IStream* pstm)
{
 IPicture* p; 
 HRESULT hr = OleLoadPicture(pstm, 0, FALSE,
  IID_IPicture, (void**)&p);
//  ASSERT(SUCCEEDED(hr) && m_spIPicture); 
 HANDLE hB = 0;   
 p->get_Handle((UINT *)&hB);  
 HANDLE hBB = CopyImage(hB,IMAGE_BITMAP,THUMWIDTH,THUMHEIGHT,LR_COPYRETURNORG); //THUMWIDTH,THUMHEIGHT为自定义的宽度和高度  
 p->Release();   
 p = NULL;
 return hBB; 
}

//////////////////
// Load from archive--create stream and load from stream.
//
HANDLE LoadPicture(CArchive& ar)
{
 CArchiveStream arcstream(&ar);
 return LoadPicture((IStream*)&arcstream);
}
//////////////////
// Load from CFile
//
HANDLE LoadPicture(CFile& file)
{
 CArchive ar(&file, CArchive::load | CArchive::bNoFlushOnDelete);
 return LoadPicture(ar);
}
//////////////////
// Load from path name.
//
HANDLE LoadPicture(LPCTSTR pszPathName)
{
 CFile file;
 if (!file.Open(pszPathName, CFile::modeRead|CFile::shareDenyWrite))
  return FALSE;
 HANDLE bRet = LoadPicture(file);
 file.Close();
 return bRet;
}

// Load from resource. Looks for "jpg" type.
//
HANDLE LoadPicture(UINT nIDRes)
{
 // find resource in resource file
 HINSTANCE hInst = AfxGetResourceHandle();
 HRSRC hRsrc = ::FindResource(hInst,
  MAKEINTRESOURCE(nIDRes),
  _T("jpg")); // type
 if (!hRsrc)
  return FALSE;
 // load resource into memory
 DWORD len = SizeofResource(hInst, hRsrc);
 BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc);
 if (!lpRsrc)
  return FALSE;
 // create memory file and load it
 CMemFile file(lpRsrc, len);
 HANDLE bRet = LoadPicture(file);
 FreeResource(hRsrc);
 return bRet;
}
CBitmap bitmap;
CImageList m_cImageList;
void CListCtrlStyle2() 
{
//  将CListCtrl设置成图标类型
  LONG lStyle;
 lStyle = GetWindowLong(((CListCtrl*)GetDlgItem(IDC_LIST1))->m_hWnd, GWL_STYLE);//获取当前窗口style
 lStyle &= ~LVS_TYPEMASK; //清除显示方式位
 lStyle |= LVS_ICON; //设置style
 SetWindowLong(((CListCtrl*)GetDlgItem(IDC_LIST1))->m_hWnd, GWL_STYLE, lStyle);//设置style

m_cImageList.Create(THUMWIDTH, THUMHEIGHT, ILC_COLORDDB|ILC_MASK, 20, 1);  
 ((CListCtrl*)GetDlgItem(IDC_LIST1))->SetImageList(&m_cImageList, LVS_ICON);


HBITMAP hbitmap;

//hbitmap = (HBITMAP)LoadPicture(strBmpPath);//strBmpPath为图片的路径名
hbitmap = (HBITMAP)LoadPicture(IDR_JPG1););//IDR_JPG1为图片的资源ID

bitmap.Attach(hbitmap);


int n = m_cImageList.Add(&bitmap, RGB(255, 0, 255));
 CString strItem=_T("信箱1");
 LVITEM lvi;
 lvi.mask = LVIF_IMAGE|LVIF_TEXT;
 lvi.iItem = 0;
 lvi.iSubItem = 0;
 lvi.pszText = (LPTSTR)(LPCTSTR)(strItem);
 lvi.iImage = 0;
 ((CListCtrl*)GetDlgItem(IDC_LIST1))->InsertItem(&lvi);

} 

资源中的JPG要在CListCtrl中显示,必须最后要转换成CBitmap类型。(CBitmap类型图片在这个控件中显示百度一大把例子)

这里简单说一下转换步骤
资源ID IDR_JPG1 ->CMemFile(CFile)->CArchive->CArchiveStream(IStream)->IPicture->HANDLE(HBITMAP)->CBitmap.

括号中表示可以强制转换

记录这些的原因是 资源ID 转换成 IStream

你可能感兴趣的:(资源中的JPG在CListCtrl中显示)