关于win32绘图出现无法显示彩色问题的解决

来源:互联网 发布:php娱乐网源码 编辑:程序博客网 时间:2024/04/29 12:48

CView::OnCreate()中的代码如下: 

 ScrnWidth=GetSystemMetrics(SM_CXSCREEN);  
  ScrnHeight=GetSystemMetrics(SM_CYSCREEN);  
  CDC   *dcpView;  
  CRect   rcView;  
  GetClientRect(rcView);  
  dcpView=GetDC();  
  m_dcBack.CreateCompatibleDC(dcpView);   //creates a memory device context (DC) compatible with the specified device.
 // m_bmpBackDC.CreateCompatibleBitmap(&m_dcBack,ScrnWidth,ScrnHeight);   //错误的

  m_bmpBackDC.CreateCompatibleBitmap(&dcpView,ScrnWidth,ScrnHeight); //正确的写法
  m_bmppOldBackDC=m_dcBack.SelectObject(&m_bmpBackDC);  
  m_brshBackDC.CreateSolidBrush(RGB(0,255,255));  
  m_brshpOldBackDC=m_dcBack.SelectObject(&m_brshBackDC);  
  m_dcBack.PatBlt(0,0,ScrnWidth,ScrnHeight,PATCOPY);  
  ReleaseDC(dcpView);  

CView::OnDraw()即处理WM_PAINT消息的函数里还需要有

 pDC->BitBlt(rcView.left,rcView.top,rcView.Width(),rcView.Height(),&m_dcBack,0,0,SRCCOPY);  

但是在createCompatibleBitmap函数的第一个参数的选择要注意,如果给的hdc是内存设备号(memory device context )则此次创建的bitmap就只支持黑白色。如果要支持彩色需要给它你的设备号即上面程序的dcpView这样才能保证输出的是彩色。

可以查阅msdn上的CreateCompatibleBitmap函数说明:

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.

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, nWidth, nHeight );    SelectObject ( memDC, memBM );

If an application sets the nWidth or nHeight parameters to zero, CreateCompatibleBitmap returns the handle to a 1-by-1 pixel, monochrome bitmap