Related references:
Implement double buffering under GDI-http://jingyan.baidu.com/article/e73e26c0f8df2424acb6a76e.html
<Win32_19> with double buffering technology for a smooth-http://www.myexception.cn/program/1407847.html
Set double buffering to reduce form flicker-Http://hi.baidu.com/robinlxzh/item/ad70a4ae92db5bf614329be7
The usual way to draw without using double buffering on a form:
Case WM_PAINT:
{
Paintstruct PS;
Prepares the drawing work for the specified window and fills in a PAINTSTRUCT structure with information about the drawing.
HDC HDC = BeginPaint (hwnd, &PS);
Hpen Hpen = CreatePen (ps_solid, 1, RGB (128, 128, 128)); Set color
Hpen Hpen = CreatePen (ps_solid, 1, GetSysColor (Color_btnshadow)); 3D Shadow of the button
SelectObject (hdc, hpen);
Rectangle (hdc,-1,-1, 680, 72); Draw a rectangle
DeleteObject (Hpen); Once you no longer need a brush, remember to delete it with the DeleteObject function
Draw Line
Hpen = CreatePen (ps_solid, 1, GetSysColor (Color_btnshadow)); 3D Shadow of the button
SelectObject (hdc, hpen);
MOVETOEX (hdc, 0, 484, NULL); Starting point
LineTo (HDC, 680, 484); End
DeleteObject (Hpen); Once you no longer need a brush, remember to delete it with the DeleteObject function
ReleaseDC (hwnd, HDC); Release
EndPaint (hwnd, &PS); End Slow
return 0;
}
-----------------------------------------------------------------------------
Use the double buffering technique below to draw, compared with the above code, just the original HDC----hdc_old, and then insert the red two parts to implement a double-buffered drawing.
The combination of WM_ERASEBKGND and WM_PAINT:
Case WM_ERASEBKGND:
{
return 1; Avoid heavy brush on window background-Http://hi.baidu.com/robinlxzh/item/ad70a4ae92db5bf614329be7
}
Case WM_PAINT:
{
Paintstruct PS;
Drawing with double buffering technology
Variable: hdc_old represents the system default HDC
Variable: HDC represents a double-buffered memory DC
Prepares the drawing work for the specified window and fills in a PAINTSTRUCT structure with information about the drawing.
HDC Hdc_old = BeginPaint (hwnd, &PS);
Memory DC for Buffering
HDC hdc = CreateCompatibleDC (hdc_old);
Need to get the width and height of the window, ease the size
RECT Clientrect;
GetClientRect (Hwnd,&clientrect);
printf ("width:%d, height:%d\n", clientrect.right,clientrect.bottom);
Create a memory-compatible bitmap hbmp
Hbitmap hbmp = CreateCompatibleBitmap (Hdc_old,clientrect.right,clientrect.bottom);
Select a memory bitmap into the buffer memory DC--so that multiple bitmaps can be drawn
SelectObject (hdc,hbmp);
If you do not perform these two steps, the window will appear with a black background
SelectObject (hdc, Getsyscolorbrush (Color_3dface)); Set Brush color-fill color of Rectangle ()
Since Rectangle () draws a black border line, this is deliberately drawn from -1,-1 +2, +2, so that the black border line beyond the visual range, you can not see
Rectangle (hdc,-1,-1, Clientrect.right + 2, Clientrect.bottom + 2); Draw the entire background of a form
Hpen Hpen = CreatePen (ps_solid, 1, RGB (128, 128, 128)); Set color
Hpen Hpen = CreatePen (ps_solid, 1, GetSysColor (Color_btnshadow)); 3D Shadow of the button
SelectObject (hdc, hpen);
Rectangle (hdc,-1,-1, 680, 72); Draw a rectangle
DeleteObject (Hpen); Once you no longer need a brush, remember to delete it with the DeleteObject function
Draw Line
Hpen = CreatePen (ps_solid, 1, GetSysColor (Color_btnshadow)); 3D Shadow of the button
SelectObject (hdc, hpen);
MOVETOEX (hdc, 0, 484, NULL); Starting point
LineTo (HDC, 680, 484); End
DeleteObject (Hpen); Once you no longer need a brush, remember to delete it with the DeleteObject function
displaying in-memory content to Windows-using the BitBlt function
BitBlt (hdc_old,0,0,clientrect.right,clientrect.bottom,hdc,0,0,srccopy);
Note Reclaim Memory resources
DeleteObject (hbmp);
DELETEDC (HDC);
ReleaseDC (hwnd, HDC); Release
EndPaint (hwnd, &PS); End Slow
return 0;
}
2014-07-03
Win32:wm_paint implementation of double buffering slow graph