c++ win32 用LoadBitmap加载位图资源和系统位图并显示

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static HBITMAP hBitmap, hSysBitmap[5];
    BITMAP bmp;
    HDC memDC = NULL;
    long w;
    long y;
    switch (message)
    {
    case WM_CREATE:
        hBitmap = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP1));
        if (!hBitmap)
        {
            MessageBox(hWnd,_T("位图加载失败"),NULL,MB_OK);

        }
        for (size_t i = 0; i < sizeof(hSysBitmap)/sizeof(HBITMAP); i++)
        {
            hSysBitmap[i] = LoadBitmap(NULL, (TCHAR*)(OBM_REDUCE + i));
        }
        break;
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // 分析菜单选择:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: 在此处添加使用 hdc 的任何绘图代码...
            if (hBitmap)
            {
                memDC = CreateCompatibleDC(hdc);
                SelectObject(memDC, hBitmap);
                GetObject(hBitmap, sizeof(bmp), &bmp);
                BitBlt(hdc, 0, 0, bmp.bmWidth, bmp.bmHeight, memDC, 0, 0, SRCCOPY);
                w = bmp.bmWidth;
                y = 0;
                for (size_t i = 0; i < sizeof(hSysBitmap)/sizeof(HBITMAP); i++)
                {
                    SelectObject(memDC,hSysBitmap[i]);
                    GetObject(hSysBitmap[i], sizeof(bmp), &bmp);
                    BitBlt(hdc, 5+w, y, bmp.bmWidth, bmp.bmHeight, memDC, 0, 0, SRCCOPY);
                    y += bmp.bmHeight + 5;
                }

            }
            DeleteDC(memDC);

            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        DeleteObject(hBitmap);
        for (size_t i = 0; i < sizeof(hSysBitmap)/sizeof(HBITMAP); i++)
        {
            DeleteObject(hSysBitmap[i]);
        }
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/dxm809/article/details/114012378