2
MSDN和衆多帖子都提示BeginPaint/EndPaint應該在WM_PAINT中使用。我還看到許多地方暗示如果在繪畫中使用雙緩衝,則在WM_CREATE中初始化DC和mem分配並在WM_PAINT中重新使用這些句柄更有意義。GetDC vs BeginPaint性能注意事項
例如,使用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清除更新區域,但將圖形委託給prev創建的對象。
謝謝。這很有幫助 – dave