brent's hut

trap in CreateCompatibleBitmap and memory dc

        CDC dcMem;
        VERIFY(dcMem.CreateCompatibleDC(NULL));
        CBitmap bmp;
        VERIFY(bmp.CreateCompatibleBitmap(
&dc,100,100));
        CBitmap 
* pOld = dcMem.SelectObject(&bmp);
        dcMem.FillSolidRect(
0,0,100,100,RGB(255,0,0));
        dcMem.MoveTo(
0,0);
        dcMem.LineTo(
100,100);
        dc.BitBlt(
0,0,100,100,&dcMem,0,0,SRCCOPY);
        dcMem.SelectObject(pOld);
these codes will run smoothly. but you will get nothing other a bitmap either black or white.

See what MSDN says:

CBitmap::CreateCompatibleBitmap

If pDC is a memory device context, the bitmap returned has the same format as the currently selected bitmap in that device context.

When a memory device context is created, GDI automatically selects a monochrome stock bitmap for it.


Solution:
1) pass in  a CPaintDC instead of a memory dc
2) just replace VERIFY(bmp.CreateCompatibleBitmap(&dc,100,100)); with bmp.CreateBitmap(100,100,1,32,NULL);

posted on 2008-07-14 17:15 brent 阅读(1076) 评论(0)  编辑 收藏 引用 所属分类: Windows


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理