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;