当前位置:编程学习 > C/C++ >>

cximage双缓冲绘图

1、起因

本来是想用gdi绘图的,但是一想到用gdi+libpng,还要自己处理一些比如alpha的效果之类的巨麻烦(而且涉及到处理每一个像素点的计算,一般都很耗时),我对自己处理像素点的能力一直持有怀疑态度。。so,先用cximage将就着,不知道是否可以跨平台。。


2、用cximage绘制png到屏幕上面确实很容易,简单的就几行代码

 

3、但是简单的在WM_PAINT消息进行重绘的时候,加入缓冲绘图,会发现背后出现黑色的区域-------------因为是在内存中使用了HBITMAP造成的,实例代码:

[cpp] 
 

[cpp] 
case WM_PAINT: 
    hdc = BeginPaint(hWnd, &ps); 
 
    // TODO: 在此添加任意绘图代码...  
    /*      hdc= GetDC(hWnd);*/ 
    hMemDC  = CreateCompatibleDC(hdc); 
    hbitmap = CreateCompatibleBitmap(hdc, m_pCxImage->GetWidth(), m_pCxImage->GetHeight()); 
    oldBmp = (HBITMAP)SelectObject(hMemDC, hbitmap); 
    m_pCxImage->Draw(hMemDC, 0, 0);  
    BitBlt(hdc, 0, 0, m_pCxImage->GetWidth(), m_pCxImage->GetHeight(), hMemDC, 0, 0, SRCCOPY); 
 
    ReleaseDC(NULL,hdc); 
    EndPaint(hWnd, &ps); 
    break; 

 case WM_PAINT:
  hdc = BeginPaint(hWnd, &ps);

  // TODO: 在此添加任意绘图代码...
  /*  hdc= GetDC(hWnd);*/
  hMemDC  = CreateCompatibleDC(hdc);
  hbitmap = CreateCompatibleBitmap(hdc, m_pCxImage->GetWidth(), m_pCxImage->GetHeight());
  oldBmp = (HBITMAP)SelectObject(hMemDC, hbitmap);
  m_pCxImage->Draw(hMemDC, 0, 0);
  BitBlt(hdc, 0, 0, m_pCxImage->GetWidth(), m_pCxImage->GetHeight(), hMemDC, 0, 0, SRCCOPY);

  ReleaseDC(NULL,hdc);
  EndPaint(hWnd, &ps);
  break;

 

4、其实去除背后的黑易做图域很简单

可以先把没绘制时候的空白,先拷贝一份在兼容DC(hdcBk)中,在绘制png的时候,先在内存DC(hMemDC)中绘制hdcBk,在用cximage绘制png到hMemDC中,那么背后就是白色的了,就这么简单


主要代码:

[cpp] 
void GetBackImage(HWND hWnd, HDC &hdcBk, int iWidth, int iHeight) 

    HDC hdcParent/*, hdcBk*/; 
    HBITMAP hbitmap; 
    hdcParent = GetDC(hWnd); 
    hdcBk = CreateCompatibleDC(hdcParent); 
    hbitmap = CreateCompatibleBitmap(hdcParent, iWidth, iHeight); 
    SelectObject(hdcBk, hbitmap); 
    BitBlt(hdcBk, 0, 0, iWidth, iHeight, hdcParent, 0, 0, SRCCOPY); 
 
    ReleaseDC(hWnd, hdcParent); 

void GetBackImage(HWND hWnd, HDC &hdcBk, int iWidth, int iHeight)
{
 HDC hdcParent/*, hdcBk*/;
 HBITMAP hbitmap;
 hdcParent = GetDC(hWnd);
 hdcBk = CreateCompatibleDC(hdcParent);
 hbitmap = CreateCompatibleBitmap(hdcParent, iWidth, iHeight);
 SelectObject(hdcBk, hbitmap);
 BitBlt(hdcBk, 0, 0, iWidth, iHeight, hdcParent, 0, 0, SRCCOPY);

 ReleaseDC(hWnd, hdcParent);
}
 

在消息处理的时候:

[cpp]
case WM_PAINT: 
    hdc = BeginPaint(hWnd, &ps); 
 
    // TODO: 在此添加任意绘图代码...  
    /*      hdc= GetDC(hWnd);*/ 
 
    hMemDC  = CreateCompatibleDC(hdc); 
    hbitmap = CreateCompatibleBitmap(hdc, m_pCxImage->GetWidth(), m_pCxImage->GetHeight()); 
    oldBmp = (HBITMAP)SelectObject(hMemDC, hbitmap); 
 
    if (hdcBk == 0) 
    { 
        GetBackImage(hWnd, hdcBk, m_pCxImage->GetWidth(), m_pCxImage->GetHeight()); 
    } 
     
    BitBlt(hMemDC, 0, 0, m_pCxImage->GetWidth(), m_pCxImage->GetHeight(), hdcBk, 0, 0, SRCCOPY); 
    m_pCxImage->Draw(hMemDC, 0, 0); 
    BitBlt(hdc, 0, 0, m_pCxImage->GetWidth(), m_pCxImage->GetHeight(), hMemDC, 0, 0, SRCCOPY); 
 
    DeleteObject(SelectObject(hMemDC, oldBmp)); 
    DeleteDC(hMemDC); 
 
    ReleaseDC(NULL,hdc); 
    EndPaint(hWnd, &ps); 
    break; 

 case WM_PAINT:
  hdc = BeginPaint(hWnd, &ps);

  // TODO: 在此添加任意绘图代码...
  /*  hdc= GetDC(hWnd);*/

  hMemDC  = CreateCompatibleDC(hdc);
  hbitmap = CreateCompatibleBitmap(hdc, m_pCxImage->GetWidth(), m_pCxImage->GetHeight());
  oldBmp = (HBITMAP)SelectObject(hMemDC, hbitmap);

  if (hdcBk == 0)
  {
   GetBackImage(hWnd, hdcBk, m_pCxImage->GetWidth(), m_pCxImage->GetHeight());
  }
  
  BitBlt(hMemDC, 0, 0, m_pCxImage->GetWidth(), m_pCxImage->GetHeight(), hdcBk, 0, 0, SRCCOPY);
  m_pCxImage->Draw(hMemDC, 0, 0);
  BitBlt(hdc, 0, 0, m_pCxImage->GetWidth(), m_pCxImage->GetHeight(), hMemDC, 0, 0, SRCCOPY);

  DeleteObject(SelectObject(hMemDC, oldBmp));
  DeleteDC(hMemDC);

  ReleaseDC(NULL,hdc);
  EndPaint(hWnd, &ps);
  break;
 

  

 

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,