我正在Win32中制作一个简单的游戏。目前,我的大部分绘图对象都在主文件中全局声明,因此我永远不必删除它们或重新加载位图。所以基本上我的所有位图都加载在WM_CREATE中,然后什么都不需要删除 - 有人告诉我,我可以依靠系统来清理项目终止时的资源。在WM_PAINT(每秒调用大约10次)中,我有很多BitBlt()调用,以及相当多的SelectObject()调用。运行大约10-15分钟后,SelectObject()。什么会导致SelectObject()失败,我是否错误地使用GDI对象?
实施例
// Top of the file
HDC hdc;
HDC hdcmem;
HDC hbcmem;
HBITMAP colorsprites, blackwhitesprites, nums;
HBITMAP hdcold, hdcbmold, hdcbm;
// Some functions to get the window ready
// More variables used for drawing:
HBITMAP bg, side, mainCont, tmpbm, tmpold, bm_left, bm_right, sidebg, win_bm;
PAINTSTRUCT ps;
RECT rc;
HDC tmphdc;
HDC tmp;
HFONT font;
HBRUSH hbr;
HPEN pen;
BITMAP structBitmapHeader;
HGDIOBJ hBitmap;
HCURSOR crosshairs = LoadCursorW(hInst, IDC_CROSS);
HCURSOR normal = LoadCursorW(hInst, IDC_ARROW);
POINT cursor;
bool addPass = false, ignorePass;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
hdc = GetDC(hWnd);
hdcmem = CreateCompatibleDC(hdc);
GetClientRect(hWnd, &rc);
hdcbm = CreateCompatibleBitmap(hdc, rc.right, rc.bottom);
hbcmem = CreateCompatibleDC(hdcmem);
hdcbmold = (HBITMAP)SelectObject(hdcmem, hdcbm);
// Load bitmaps
bg = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BACKGROUND));
mainCont = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_GAME_CONT));
side = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_SIDEINFO));
colorsprites = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_COLOR_SPRITES));
blackwhitesprites = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BLACKWHITE_SPRITES));
sidebg = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_SIDEBG));
nums = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_NUMBERS));
bm_left = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_LEFT));
bm_right = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_RIGHT));
win_bm = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_VICTORY));
if(bg == NULL || mainCont == NULL || side == NULL || colorsprites == NULL
|| blackwhitesprites == NULL || sidebg == NULL || nums == NULL || bm_left == NULL
|| bm_right == NULL)
ThrowError("A bitmap failed to load.");
break;
case WM_PAINT:
BeginPaint(hWnd, &ps);
// SelectObject() and BitBlt() are called a lot in here
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
// System will clean up all GDI stuff - no need to delete anything
PostQuitMessage(0);
break;
}
}
答案 0 :(得分:3)
确保在销毁DC时,您已将原始位图重新选入其中。在您的位图仍处于选中状态时,切勿销毁DC。
答案 1 :(得分:2)
在EndPaint之前的BeginPaint和RestoreDC之后立即调用SaveDC可以让生活变得更轻松。这将在DC被释放之前将DC恢复到其原始状态。
http://msdn.microsoft.com/en-us/library/windows/desktop/dd162945(v=vs.85).aspx