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

C++ C++;Win32。SelectObject失败,GetLastError返回错误1400(无效的窗口句柄),c++,winapi,handle,C++,Winapi,Handle,所以我尝试在Win32中复制Pong,一切正常,但后来我做了很多物理方面的工作,当我测试它时,sprite位图甚至不再显示:/ 下面是我初始化渲染的方法: int InitRenderer(int showCMD) { context = GetDC(winHandle); if(!context) { return EXIT_FAILURE; } ShowWindow(winHandle, showCMD); UpdateW

所以我尝试在Win32中复制Pong,一切正常,但后来我做了很多物理方面的工作,当我测试它时,sprite位图甚至不再显示:/

下面是我初始化渲染的方法:

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);
}
缓冲区结构(仅供参考)如下所示:

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.
}
自从它起作用以来,我就没有改变过

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

我环顾了其他问题,这些问题的解决方案似乎与注册窗口类等问题有关。我检查了我的问题,没有发现任何问题。我已经调试过了,hOldBitmap2是缓冲区中唯一为空的部分。其余的都好


你们这些圣贤们能提供的任何帮助都将不胜感激

我刚刚遇到了这个问题

应用程序不能同时将一个位图选择到多个DC中 一段时间。位图是否已选择到另一个DC中theB 8月27日 15点18分09分

快到了

确定:

SelectObject( hdc_bitmap, buf_bitmap )
SelectObject( hdc_bitmap, buf_bitmap )
SelectObject( hdc_bitmap, buf_bitmap )
失败:

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

我刚刚遇到这个问题

应用程序不能同时将一个位图选择到多个DC中 一段时间。位图是否已选择到另一个DC中theB 8月27日 15点18分09分

快到了

确定:

SelectObject( hdc_bitmap, buf_bitmap )
SelectObject( hdc_bitmap, buf_bitmap )
SelectObject( hdc_bitmap, buf_bitmap )
失败:

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

(HBITMAP)选择对象(buffer->hdcBitmap,bmp)你的bmp句柄有效吗?位图是否已被选择到另一个DC中?是什么让您认为
SelectObject()
填充
GetLastError()
以报告错误代码?它不是以这种方式记录的。大多数GDI函数不使用
GetLastError()
。如果给定的API函数没有记录为填充
GetLastError()
,不要假设它是这样的。@RemyLebeau我以为所有GDI函数都使用它。该死,那么错误可能不在那里,而是在其他地方?嗯…@theB:不应该,但我没想到要检查它。
(HBITMAP)选择object(buffer->hdcBitmap,bmp)你的bmp句柄有效吗?位图是否已被选择到另一个DC中?是什么让您认为
SelectObject()
填充
GetLastError()
以报告错误代码?它不是以这种方式记录的。大多数GDI函数不使用
GetLastError()
。如果给定的API函数没有记录为填充
GetLastError()
,不要假设它是这样的。@RemyLebeau我以为所有GDI函数都使用它。该死,那么错误可能不在那里,而是在其他地方?嗯…@theB:不应该,但我没想到要检查一下。希望他没有为这个答案等了3年多。我认为你是不对的。RenderSprite函数保存并恢复原始位图。希望他没有等待这个答案3年多。我认为你是不对的。RenderSprite函数保存并恢复原始位图。