trap in CreateCompatibleBitmap and memory dc

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);

你可能感兴趣的:(trap in CreateCompatibleBitmap and memory dc)