C ++ Win32。 SelectObject失败,GetLastError返回错误1400(窗口句柄无效)

问题描述 投票:1回答:1

所以我试图在Win32中克隆Pong,并且工作正常,但后来我用物理做了很多东西,当我测试它时,精灵位图甚至不再显示:/

那么,这是我如何初始化渲染内容:

int InitRenderer(int showCMD)
{
    context = GetDC(winHandle);

    if(!context)
    {
        return EXIT_FAILURE;
    }

    ShowWindow(winHandle, showCMD);

    UpdateWindow(winHandle);

    CreateDoubleBuffer(&globalBuffer);
    ClearWindow(globalBuffer.hdcBack, globalBuffer.scrnRect);

    return EXIT_SUCCESS;
}

这是CreateDoubleBuffer函数:

void CreateDoubleBuffer(BUFFER *buffer)
{
    buffer->hwnd = winHandle;
    GetClientRect(winHandle, &(buffer->scrnRect));

    buffer->hdcFront    = GetDC(buffer->hwnd);  //get a handle to the DC and plop it into the front buffer.
    buffer->hdcBack     = CreateCompatibleDC(buffer->hdcFront); //get a compatible DC for the Back buffer.
    buffer->hdcBitmap   = CreateCompatibleDC(buffer->hdcFront); //get a compatible DC for the bitmap.
    buffer->hCompBitmap = CreateCompatibleBitmap(buffer->hdcFront, buffer->scrnRect.right, buffer->scrnRect.bottom);    //Create a compatible bitmap as a dummy, and store in the front buffer.
    buffer->hOldBitmap  = (HBITMAP)SelectObject(buffer->hdcBack, buffer->hCompBitmap);
}

BUFFER结构,供参考,如下所示:

struct BUFFER                   // This is our back buffering structure
{
    HWND hwnd;                  // This holds the current window's handle
    RECT scrnRect;              // This holds the client rectangle of the window
    HANDLE hCompBitmap;         // This holds the compatible bitmap for the backbuffer
    HANDLE hOldBitmap;          // This is used for storage to free when the program quits
    HANDLE hOldBitmap2;         // This is used as storage to swap between selected bitmaps when using selectObject()
    HDC hdcFront;               // This is the front buffer (The part we see)
    HDC hdcBack;                // This is the back buffer (the part we draw to, then flip)
    HDC hdcBitmap;              // This is a temp buffer to swap the bitmap back and forth from
};

所以我有一个Sprite类,它只包装HBITMAP和文件名的字符串,以及一些操作它们的函数。当我想绘制精灵时,会调用此函数:

void RenderSprite(BUFFER *buffer, HBITMAP bmp, Vec2I pos, Vec2F origin)
{
    buffer->hOldBitmap2 = (HBITMAP)SelectObject(buffer->hdcBitmap, bmp);    //we put the bitmap into the extra HDC to hold it there.

    if(!buffer->hOldBitmap2)
    {
        std::cout << GetLastError() << "\n";
    }

    BitBlt(buffer->hdcBack, pos.GetX() + (int)origin.GetX(), pos.GetY() + (int)origin.GetY(), buffer->scrnRect.right, buffer->scrnRect.bottom, buffer->hdcBitmap, 0, 0, SRCCOPY);   //blit the bitmap into the backbuffer.

    SelectObject(buffer->hdcBitmap, buffer->hOldBitmap2);   //put the old handle to the bitmap back where it belongs.
}

并且在此函数的开头,SelectObject失败,因此buffer-> hOldBitmap2为null。 GetLastError返回的错误是1400,这意味着窗口句柄无效,所以我猜winHandle(一个全局变量,只是你知道)搞砸了。但我不知道怎么做。以下是我初始化它的方法:

int Game::Start(HINSTANCE instance, int showCMD)
{
    WNDCLASSEX winClass    = {0};
    winClass.cbSize        = sizeof(winClass);
    winClass.style         = CS_HREDRAW | CS_VREDRAW;
    winClass.lpfnWndProc   = WndProc;
    winClass.hInstance     = instance;
    winClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    winClass.lpszClassName = _winClassName;
    winClass.hCursor       = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(OCR_CROSS), IMAGE_CURSOR, 0, 0, LR_SHARED);   //using a cross for a cursor because we're hipsters here at cow_co industries.

    RegisterClassEx(&winClass);

    /**
    *   WS_EX_CLIENTEDGE gives the client a sunken edge.
    **/
    winHandle = CreateWindowEx(WS_EX_CLIENTEDGE, _winClassName, "Win32 Pong", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, instance, NULL);

    if(!winHandle)
    {
        return EXIT_FAILURE;
    }

    //other stuff...

自从它发挥作用以来,我没有改变它。

就屏幕上发生的事情而言,我只是得到一个空白的白色窗口,所以没有崩溃消息或其他什么,但它只是......空白。

我已经查看了其他问题,这些问题的解决方案似乎与注册窗口类等问题有关。我已经检查了我的问题,我看不出有什么问题。我已经调试过,而hOldBitmap2是缓冲区中唯一的null部分。剩下的很好。

任何帮助你的圣经提供的帮助将非常感激。

c++ winapi handle
1个回答
0
投票

我刚遇到这个问题。

应用程序无法一次将单个位图选择为多个DC。位图是否已经被选入另一个DC? --B于2015年8月27日18:09

很近!

好:

SelectObject( hdc_bitmap, buf_bitmap )

失败:

SelectObject( hdc_bitmap, buf_bitmap )
SelectObject( hdc_bitmap, buf_bitmap )

这意味着如果您的RenderSprite函数尝试连续两次绘制相同的精灵,它将失败。

© www.soinside.com 2019 - 2024. All rights reserved.