使刷新时画面不闪烁的技巧

使刷新时画面不闪烁的技巧

创建位图内存,在内存里放置绘画数据,然后显示内存。为了避免内存在刷新时不断创建,所以内存只存窗口大小变化时创建。
HDC hMemDC=0;//global variat
WM_PAINT:
if(hMemDC==0)
CreateMemDC(Rect,hDC,hMemDC); //Rect为窗体大小
DrawBack(hDC,Rect,hMemDC);
WM_SIZE:
GetClientRect(hWnd,&Rect);
if(Rect.right==0||Rect.bottom==0)
return 0;
if(hMemDC!=0)
ReleaseMemDC(hWnd,hMemDC);
hDC=GetDC(hWnd);
CreateMemDC(Rect,hDC,hMemDC);
DeleteDC(hDC);
WM_DESTROY:
ReleaseMemDC(hWnd,hMemDC);
/*名称:CreateMemDC
功能:创建显存hMemDC
参数:hDC
*/
int CreateMemDC(RECT Rect,HDC hDC,HDC &hMemDC)
{
hMemDC=CreateCompatibleDC(hDC);
hMembitmap=CreateCompatibleBitmap(hDC,Rect.right-Rect.left,Rect.bottom-Rect.top);
SelectObject(hMemDC,hMembitmap);
BitBlt(hMemDC,Rect.left,Rect.top,Rect.right-Rect.left,Rect.bottom-Rect.top,hDC,0,0,SRCCOPY);
return 1;
}
/*名称:ReleaseMemDC
功能:释放显存hMemDC
参数:释放显存hMemDC
*/
int ReleaseMemDC(HWND hWnd,HDC &hMemDC)
{
DeleteObject(hMembitmap);
DeleteObject(hMemDC);
return 1;
}
/*名称:DrawBack
功能:画波形
参数:HDC,显存hMemDC数据
*/
int DrawBack(HDC hDC,RECT Rect,HDC hMemDC)
{
TextOut(hMemDC,0,0,”hello”,5);//在内存写数据
BitBlt(hDC,Rect.left,Rect.top,Rect.right,Rect.bottom,hMemDC,0,0,SRCCOPY);//将hMemDC数据显示在hDC上。
Return 1;
}