在贴一些大点的图片,或者要又叠加多张图片在一起的时候,最好用双缓存.
下面是双缓存的代码,是一个播放进度条,一共三个图片.在缓存画好后一起搬到前台DC上显示
//双缓冲绘图
HDC hdc=GetDC(hWnd);
HDC hdcMem1 = CreateCompatibleDC(hdc);
HDC hdcMem2 = CreateCompatibleDC(hdc);
HBITMAP hBit=CreateCompatibleBitmap(hdc,
rcVolBarBk.right-rcVolBarBk.left,
rcVolBarBk.bottom-rcVolBarBk.top);
HBITMAP hOldSel1 = (HBITMAP)SelectObject(hdcMem1,hBit);
HBITMAP hBmp=LoadBitmap(hInst,MAKEINTRESOURCE(IDB_APVOLBARBK));
HBITMAP hOldSel2 = (HBITMAP)SelectObject(hdcMem2,hBmp);
BitBlt(hdcMem1,0,0,rcVolBarBk.right-rcVolBarBk.left,rcVolBarBk.bottom-rcVolBarBk.top,
hdcMem2,0,0,SRCCOPY);
SelectObject(hdcMem2,hOldSel2);
DeleteObject(hBmp);
hBmp=LoadBitmap(hInst,MAKEINTRESOURCE(IDB_APVOLBAR));
hOldSel2 = (HBITMAP)SelectObject(hdcMem2,hBmp);
BitBlt(hdcMem1,0,0,rcVolBar.right-rcVolBar.left,rcVolBar.bottom-rcVolBar.top,
hdcMem2,0,0,SRCCOPY);
SelectObject(hdcMem2,hOldSel2);
DeleteObject(hBmp);
hBmp=LoadBitmap(hInst,MAKEINTRESOURCE(IDB_APVOLBARTHUMB));
hOldSel2 = (HBITMAP)SelectObject(hdcMem2,hBmp);
COLORREF Tcolor=RGB(255,0,255);
TransparentBlt(hdcMem1,rcVolBarThumb.left-rcVolBarBk.left,rcVolBarThumb.top-rcVolBarBk.top,
rcVolBarThumb.right-rcVolBarThumb.left,rcVolBarThumb.bottom-rcVolBarThumb.top,
hdcMem2,0,0,rcVolBarThumb.right-rcVolBarThumb.left,rcVolBarThumb.bottom-rcVolBarThumb.top,Tcolor);
SelectObject(hdcMem2,hOldSel2);
DeleteObject(hBmp);
BitBlt(hdc,rcVolBarBk.left,rcVolBarBk.top,rcVolBarBk.right-rcVolBarBk.left,rcVolBarBk.bottom-rcVolBarBk.top,
hdcMem1,0,0,SRCCOPY);
SelectObject(hdcMem1,hOldSel1);
DeleteObject(hBit);
DeleteDC(hdcMem2);
DeleteDC(hdcMem1);
ReleaseDC(hWnd,hdc);