HBITMAP Cxxx::CopyBitmap(HBITMAP hSourcehBitmap)
{
CDC sourcedc;
CDC destdc;
// 转载者注:创建空的兼容性dc,其内容是由选入的HBITMAP决定的,
// 但是由GetDC来创建的dc(即null改为GetDC()),其内容则是句柄本所在的窗口内容
sourcedc.CreateCompatibleDC(NULL);
destdc.CreateCompatibleDC(NULL);
//the bitmap information.
BITMAP bm = {0};
//get the bitmap information.
::GetObject(hSourcehBitmap, sizeof(bm), &bm);
// create a bitmap to hold the result
HBITMAP hbmresult = ::CreateCompatibleBitmap(CClientDC(NULL), bm.bmWidth, bm.bmHeight);
HBITMAP hbmoldsource = (HBITMAP)::SelectObject(sourcedc.m_hDC, hSourcehBitmap);
HBITMAP hbmolddest = (HBITMAP)::SelectObject(destdc.m_hDC, hbmresult);
destdc.BitBlt(0,0,bm.bmWidth, bm.bmHeight, &sourcedc, 0, 0, SRCCOPY);
// restore dcs
::SelectObject(sourcedc.m_hDC, hbmoldsource);
::SelectObject(destdc.m_hDC, hbmolddest);
::DeleteObject(sourcedc.m_hDC);
::DeleteObject(destdc.m_hDC);
return hbmresult;
}
========================================================
在网上大部分都是说用临时CDC,Bitblt来拷贝
这个方法简单点,就是先把CBitmap强制转换到HBITMAP后再Attach()
void Cxxx::CopyBitmap(CBitmap* hSourceBitmap, CBitmap* hDescBitmap)
{
HBITMAP HBM=(HBITMAP)hSourceBitmap->m_hObject;
//HBITMAP HBM=(HBITMAP)hSourceBitmap->Detach();//如果希望清除掉原图资源
//hDescBitmap = new CBitmap;
hDescBitmap->Attach(HBM);
}