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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值