【问题标题】:C++ Win32. SelectObject Fails, with GetLastError returning error 1400 (invalid window handle)C++ Win32。 SelectObject 失败,GetLastError 返回错误 1400(无效的窗口句柄)
【发布时间】:2015-08-27 16:11:05
【问题描述】:

所以我试图在 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 为空。 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 是缓冲区中唯一为空的部分。其余的都很好。

如果您能提供任何帮助,我们将不胜感激。

【问题讨论】:

  • (HBITMAP)SelectObject(buffer-&gt;hdcBitmap, bmp); 你的 bmp 句柄有效吗?
  • 是什么让您认为SelectObject() 填充GetLastError() 以报告错误代码?它没有以这种方式记录。大多数 GDI 函数不使用GetLastError()。如果给定的 API 函数未记录为填充 GetLastError(),请不要假设它确实如此。
  • @RemyLebeau 我认为所有 GDI 函数都使用了它。该死,所以错误甚至可能不在其中,而是在其他地方?嗯...
  • @theB:不应该,但我没想过要检查。

标签: c++ winapi handle


【解决方案1】:

我刚遇到这个问题。

应用程序无法在多个 DC 中选择一个位图 一次。位图是否已经选择到另一个 DC 中? – theB 8 月 27 日 15 年 18 点 09 分

接近了!

好的:

SelectObject( hdc_bitmap, buf_bitmap )

失败:

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

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

【讨论】:

  • 希望他没有为这个答案等待 3 年多。我不认为你是正确的。 RenderSprite 函数保存和恢复原始位图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-24
  • 2015-05-18
  • 1970-01-01
  • 2014-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多