1. Cause
I originally wanted to use gdi for drawing, but when I thought of using gdi + libpng, I had to handle a lot of troubles like alpha (and involved in processing the computing of each pixel, it is usually time-consuming.) I have always been skeptical about my ability to process pixels .. So, use cximage first. I don't know whether cross-platform is supported ..
2. It is really easy to use cximage to draw png on the screen. Just a few lines of code are simple.
3. When the WM_PAINT message is re-painted, a buffer plot is added, and the black area ------------- is displayed in the background because HBITMAP is used in the memory. The instance code is as follows:
[Cpp]
[Cpp]
Case WM_PAINT:
Hdc = BeginPaint (hWnd, & ps );
// TODO: add any drawing code here...
/* 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: add any drawing code here...
/* 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. It is easy to remove the black area behind the scenes.
You can copy the blank when the image is not drawn to the compatible DC (hdcBk). When creating a png image, you can first draw the hdcBk in the memory DC (hMemDC, when using cximage to draw png to hMemDC, the background is white, so simple.
Main Code:
[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 );
}
When processing a message:
[Cpp]
Case WM_PAINT:
Hdc = BeginPaint (hWnd, & ps );
// TODO: add any drawing code here...
/* 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: add any drawing code here...
/* 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;