在做一个内存DC的时候,碰到了一个问题:
  在这个DC中,我只能表现出两种颜色,也就是黑白两色,对于我设进去的颜色,都不起作用了,请问这个是什么原因,我从中提出部分实现代码如下,请达人解决一下。谢谢先
HDC hMemDC = ::CreateCompatibleDC(NULL);
HBITMAP hBitmap = ::CreateCompatibleBitmap(hMemDC, 800, 600);
HBITMAP hOldBitmap = (HBITMAP)::SelectObject(hMemDC, hBitmap);::SetBkColor(hMemDC, RGB(255, 0, 0)); // 注:这里设置已经成功,用GetBkColor能得到红色RGB值
::SetTextColor(hMemDC, RGB(0, 255, 0)); // 同样,这里也已经设置成功了::DrawText(hMemDC, ...); // 输出文字按说这应该显示红底绿字,可最终显示的却是默认的黑底白字,也就是说,这个DC仅仅能显示黑白两种颜色,是不是创建一个DC后,还要对其进行相应的设置,才能让其显示多种颜色,请帮个忙,谢谢了PS:
如果不是创建的内存DC,则能正确显示
HDC hDC = ::GetDC(m_hWnd);SetBkColor(hDC, RGB(255, 0, 0));
SetTextColor(hDC, RGB(0 ,255, 0));DrawText(hDC, ...);

解决方案 »

  1.   

    HDC hDC = ::GetDC(m_hWnd);
    HDC hMemDC = ::CreateCompatibleDC(hDC); //把memdc和你的窗口DC关联起来
      

  2.   

    HBITMAP hBitmap = ::CreateCompatibleBitmap(hMemDC, 800, 600);这里不对
    CreateCompatibleBitmap传入的DC不能是内存DC,否则创建出的是Mask位图,也就是黑白位图
      

  3.   

    这是MSDN的相关内容
    Because memory device contexts allow both color and monochrome bitmaps, the format of the bitmap returned by the CreateCompatibleBitmap function differs when the specified device context is a memory device context. However, a compatible bitmap that was created for a nonmemory device context always possesses the same color format and uses the same color palette as the specified device context. Note: When a memory device context is created, it initially has a 1-by-1 monochrome bitmap selected into it. If this memory device context is used in CreateCompatibleBitmap, the bitmap that is created is a monochrome bitmap. To create a color bitmap, use the hDC that was used to create the memory device context, as shown in the following code:    HDC memDC = CreateCompatibleDC ( hDC );
        HBITMAP memBM = CreateCompatibleBitmap ( hDC );
        SelectObject ( memDC, memBM );
      

  4.   

    HBITMAP hBitmap = ::CreateCompatibleBitmap(hMemDC, 800, 600); 
    是这句代码引起的,不能用hMemDC,因为这时hMemDC没有选进任何bitmap,只有黑白两种颜色
    需要把 hMemDC换成窗口的 DC HDC hMemDC = ::CreateCompatibleDC(NULL);
    HDC hWndDC=::GetDC(m_hWnd);
    HBITMAP hBitmap = ::CreateCompatibleBitmap(hWndDC, 800, 600);
    HBITMAP hOldBitmap = (HBITMAP)::SelectObject(hMemDC, hBitmap);
    ...
      

  5.   

    改为
    HDC hDC = GetDC(NULL);
    HDC hMemDC = ::CreateCompatibleDC(NULL);
    HBITMAP hBitmap = ::CreateCompatibleBitmap(hDC, 800, 600);
    HBITMAP hOldBitmap = (HBITMAP)::SelectObject(hMemDC, hBitmap);
      

  6.   

    明白,谢谢 goodboyws(深夜不眠者) 
    这也就是说内存DC只能创建出Mask位图Res
    The color format of the bitmap created by the CreateCompatibleBitmap function matches the color format of the device identified by the hdc parameter. This bitmap can be selected into any memory device context that is compatible with the original device.在内存DC中,其Device identified只会是黑白位图的是吧