基本位图不显示 - GetObject()返回null

时间:2014-04-20 04:11:11

标签: winapi visual-c++ visual-studio-2013

我正在尝试使用Visual c ++在窗口中显示位图。位图似乎正确加载:

case WM_CREATE:
    hInstance = GetModuleHandle(NULL);
    squareBitmap = (HBITMAP)LoadImage(hInstance, TEXT("C:\\Users\\User1\\ResEdit Projects\\Test1\\bitmap1.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    if (squareBitmap == NULL)
    {
        MessageBox(windowHandle, TEXT("Trouble..."), TEXT("Loading square"), MB_OK);
    }
    break;

不显示错误消息框。

我认为问题出在WM_PAINT,但我不知道我应该在WM_PAINT中进行哪种错误检查。

BITMAP bm;
    PAINTSTRUCT ps;

    HDC hdc = BeginPaint(windowHandle, &ps);
    if (hdc == NULL)
    {
        MessageBox(windowHandle, TEXT("Trouble..."), TEXT("hdc"), MB_OK);
    }
        HDC hdcMem = CreateCompatibleDC(hdc);

    if (hdcMem == NULL)
    {
        MessageBox(windowHandle, TEXT("Trouble..."), TEXT("hdcMem"), MB_OK);
    }



    HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, squareBitmap);

    int testGO = GetObject(squareBitmap, sizeof(bm), &bm);
    if (testGO == NULL)
    {
        MessageBox(windowHandle, TEXT("Trouble..."), TEXT("hdcObject"), MB_OK);
    }
    //char buffer[20];
    //itoa(bm.bmHeight, buffer, 52);
    //MessageBox(windowHandle, buffer, TEXT(bm.bmHeight), MB_OK);
    int testBB = BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
    if (testBB == NULL)
    {
        MessageBox(windowHandle, TEXT("Trouble..."), TEXT("testBB"), MB_OK);
    }
    SelectObject(hdcMem, hbmOld);
    DeleteDC(hdcMem);

    EndPaint(windowHandle, &ps);
    break;

GetObject返回NULL。我认为这意味着我已经错误地加载了我的资源文件。

1 个答案:

答案 0 :(得分:0)

就像Jonathan Potter建议的那样,我正在初始化并加载我的图像,以便在我到NULL时它们是WM_PAINT。所以与WinApi无关,只是一个逻辑错误。