Cximage double buffer plotting

Source: Internet
Author: User

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;
 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.