2
這裏是從的WndProc功能開關的代碼,我一直在考慮作爲一個例子:爲什麼我需要在使用Win32 GDI進行繪圖時將句柄保存到舊的位圖?
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// Create a system memory device context.
bmHDC = CreateCompatibleDC(hdc);
// Hook up the bitmap to the bmHDC.
oldBM = (HBITMAP)SelectObject(bmHDC, ghBitMap);
// Now copy the pixels from the bitmap bmHDC has selected
// to the pixels from the client area hdc has selected.
BitBlt(
hdc, // Destination DC.
0, // 'left' coordinate of destination rectangle.
0, // 'top' coordinate of destination rectangle.
bmWidth, // 'right' coordinate of destination rectangle.
bmHeight, // 'bottom' coordinate of destination rectangle.
bmHDC, // Bitmap source DC.
0, // 'left' coordinate of source rectangle.
0, // 'top' coordinate of source rectangle.
SRCCOPY); // Copy the source pixels directly
// to the destination pixels
// Select the originally loaded bitmap.
SelectObject(bmHDC, oldBM);
// Delete the system memory device context.
DeleteDC(bmHDC);
EndPaint(hWnd, &ps);
return 0;
我的問題是,爲什麼有必要保存和恢復選擇對象的返回值()在oldBM?
比泄漏位圖資源更糟糕的是雙重刪除:如果您按照規則進行遊戲,並將您的位圖資源(仍然選擇到設備上下文中)處理掉,則系統將執行相同的操作,當WM_PAINT '處理程序返回。 – IInspectable