什么是正确的方式来使用GDI对象

Win32 - What is the proper way to use GDI objects?

本文关键字:GDI 对象 方式 什么      更新时间:2023-10-16

我在Win32做一个简单的游戏。目前,我在主文件中声明了大部分的绘图对象,这样我就不必删除它们或重新加载位图。所以基本上我所有的位图都加载在WM_CREATE中,然后没有任何东西需要被删除——有人告诉我,我可以依靠系统在项目终止时清理我的资源。在WM_PAINT(每秒调用大约10次)中,我有很多BitBlt()调用,以及相当多的SelectObject()调用。运行大约10 - 15分钟后,选择对象()。什么会导致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;
    }
}

确保当您销毁DC时,您已将原始位图选择回其中。当位图仍然处于选中状态时,永远不要销毁DC。

通过在BeginPaint之后调用SaveDC,在EndPaint之前调用RestoreDC,可以使生活变得容易得多。这将把DC恢复到它被释放之前的原始状态。

http://msdn.microsoft.com/en-us/library/windows/desktop/dd162945 (v = vs.85) . aspx