内存绘图转换为位图对象,显示在clistctrl中的实现缩略图的有关问题,解决可加分

内存绘图转换为位图对象,显示在clistctrl中的实现缩略图的问题,解决可加分
本帖最后由 VisualEleven 于 2014-05-08 18:35:26 编辑
请教下 我是想 用clistctrl实现缩略图 ,显示我在内存绘图 (dc)中绘制的图形,我的思路是  先用兼容dc 绘图  然后将兼容dc里面的图形保存到一个位图对象比如 CBitmap bitmap; 然后m_ImgList.Add(pbitmap, RGB(100,100,100)); 应该可以显示了吧。
关机的一个问题:如何将兼容dc中的图形保存到那个位图对象中CBitmap bitmap;;新手对于位图和dc内部的机制不是很清楚
部分代码
CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
CDC  m_dcCompatible;
if(!m_dcCompatible.m_hDC)

m_dcCompatible.CreateCompatibleDC(&dc);
}

//CRect rect;
//GetClientRect(&rect);
CBitmap bitmap;
bitmap.CreateCompatibleBitmap(&dc,30,30);
m_dcCompatible.SelectObject(&bitmap);
m_dcCompatible.BitBlt(0,0,30,30,&dc,0,0,SRCCOPY);
m_dcCompatible.SelectObject(pBrush);
SelectObject(m_dcCompatible, bitmap);

//CBitmap *pOldBit=dc.SelectObject(&bitmap);


m_dcCompatible.Rectangle(0,0,30,30);
m_dcCompatible.LineTo(5,15);
m_ImgList.Create(50,50,ILC_COLOR24,11,0);  

m_ListCtl.SetImageList(&m_ImgList,LVSIL_NORMAL);
m_ImgList.Add(&bitmap, RGB(100,100,100));

------解决方案--------------------
先复制到 ClipBoard ,用 ”绘图“ paste 进来 看看 , 图 对不对。
------解决方案--------------------
void paint(CDC &dc)
{
CDC  dcCompatible;
dcCompatible.CreateCompatibleDC(&dc);

CBitmap bitmap;
bitmap.CreateCompatibleBitmap(&dc,30,30);
dcCompatible.SelectObject(&bitmap);
dcCompatible.BitBlt(0,0,30,30,&dc,0,0,SRCCOPY);
//
CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
dcCompatible.SelectObject(pBrush);

dcCompatible.Rectangle(0,0,30,30);
dcCompatible.MoveTo(0,0);
dcCompatible.LineTo(30,30);

#if 1 //def TEST
OpenClipboard(0);
EmptyClipboard(); 
SetClipboardData(CF_BITMAP,bitmap.GetSafeHandle());
CloseClipboard();
#endif
//m_ImgList.Create(50,50,ILC_COLOR24,11,0);  
//m_ListCtl.SetImageList(&m_ImgList,LVSIL_NORMAL);
//m_ImgList.Add(&bitmap, RGB(100,100,100));
}

------解决方案--------------------
else
{
CPaintDC dc(this); // device context for painting
paint(dc);
// CDialog::OnPaint();
}

------解决方案--------------------
内存绘图转换为位图对象,显示在clistctrl中的实现缩略图的有关问题,解决可加分
1k的不行大的到可以,**** 怪吧?
------解决方案--------------------
我的是对话框,有背景图. Clouds.bmp
CPaintDC dc(this); // device context for painting
paint(dc);// 这个dc 就是对话框的
------解决方案--------------------
" 设置 dc背景"
CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));

你创建一个PatternBrush 来代替它就可以;了.
------解决方案--------------------
CBrush *pBackBr=new CBrush;
pBackBr->CreatePatternBrush(Bmp);// CBitmap Bmp; 装入 一个.bmp

------解决方案--------------------
给个函数,自己看看吧。

void CMapXViewView::SaveHwndToBmpFile(HWND hWnd, LPCTSTR lpszPath)
{
HDC hDC = ::GetDC(hWnd);
if(hDC == NULL)
{
CString cstrError;
cstrError.Format("%d",GetLastError());
AfxMessageBox(_T("GetWindowDC is Error!")+cstrError);
}

HDC hMemDC = ::CreateCompatibleDC(hDC);
if(hMemDC == NULL)
{
CString cstrError;
cstrError.Format("%d",GetLastError());
AfxMessageBox(_T("CreateCompatibleDC is Error!")+cstrError);
}

RECT rc;
::GetWindowRect(hWnd, &rc);

HBITMAP hBitmap = ::CreateCompatibleBitmap(hDC, rc.right - rc.left -5, rc.bottom - rc.top-5);
if(hBitmap == NULL)
{
CString cstrError;
cstrError.Format("%d",GetLastError());
AfxMessageBox(_T("CreateCompatibleBitmap is Error!")+cstrError);
}

HBITMAP hOldBmp = (HBITMAP)::SelectObject(hMemDC, hBitmap);
BOOL bPrintRes;
bPrintRes = ::PrintWindow(hWnd, hMemDC, PW_CLIENTONLY);
if(0 == bPrintRes)
{
CString cstrError;
cstrError.Format("%d",GetLastError());
AfxMessageBox(_T("PrintWindow is Error!")+cstrError);
}
//::StretchBlt()

BITMAP bitmap = {0};
::GetObject(hBitmap, sizeof(BITMAP), &bitmap);
BITMAPINFOHEADER bi = {0};
BITMAPFILEHEADER bf = {0};

CONST int nBitCount = 24;
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bitmap.bmWidth;
bi.biHeight = bitmap.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = nBitCount;
bi.biCompression = BI_RGB;
DWORD dwSize = ((bitmap.bmWidth * nBitCount + 31) / 32) * 4 * bitmap.bmHeight;

HANDLE hDib = GlobalAlloc(GHND, dwSize + sizeof(BITMAPINFOHEADER));
LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);