gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:28:56 25 4
gpt4 key购买 nike

所以我试图在 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);
}

作为引用,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 和一个文件名字符串,以及一些操作它们的函数。当我想绘制 Sprite 时,这个函数被调用:

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

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

最佳答案

我刚遇到这个问题。

An application cannot select a single bitmap into more than one DC at a time. Is the bitmap already selected into another DC? – theB Aug 27 '15 at 18:09

接近了!

确定:

SelectObject( hdc_bitmap, buf_bitmap )

失败:

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

这意味着如果您的RenderSprite 函数试图连续两次绘制同一个 Sprite ,它会失败。

关于C++ Win32。 SelectObject 失败,GetLastError 返回错误 1400(无效的窗口句柄),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32254555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com