MSDN 和许多帖子都建议在 WM_PAINT 中使用 BeginPaint/EndPaint。我还看到很多地方表明如果在绘画中使用双缓冲,那么在 WM_CREATE 中初始化 DC 和内存分配并在 WM_PAINT 中重用这些句柄会更有意义。
例如,使用 BeginPaint,我通常会看到:
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
MemDC = CreateCompatibleDC(hdc);
bmp = CreateCompatibleBitmap(hdc, width, height);
oldbmp = SelectObject(MemDC,bmp);
g = new Graphics(MemDC);
//do paint on bmp
//blt bmp back to hdc
EndPaint(hWnd, &ps);
DeleteObject(bmp);
g->ReleaseHDC(MemDC);
DeleteDC(MemDC);
delete g;
为了保存初始化和拆除,是否可以这样做:
case WM_CREATE:
hdc = GetDC(hWnd);
//create memDC and graphics object references ...
case WM_DESTROY
//delete memDC and graphics object references...
case WM_PAINT
BeginPaint(hWnd, &ps);
//use previously create mem and graphics object to paint
EndPaint(hWnd, &ps);
所以我们只使用 EndPaint 来清除更新区域,但将绘图委托给先前创建的对象。