gpt4 book ai didi

c++ - openGL 渲染在 WS_OVERLAPPEDWINDOW 中工作,但在 WS_POPUP 中不工作

转载 作者:行者123 更新时间:2023-11-28 04:58:32 26 4
gpt4 key购买 nike

我正在尝试在 Windows 中创建一个非常简单的 openGL 窗口。问题是,当使用 WS_OVERLAPPEDWINDOW 创建窗口时,openGL 渲染工作正常,但是当我将其更改为全屏 WS_POPUP 时,它只显示全屏白色/空白窗口。

这里是主要的:

int main(int argc, char **argv)
{
hInstance = GetModuleHandle(NULL);
if (hInstance == NULL)
{
return 1;
}

int retCode = createWindow(hInstance);
if (retCode != 0)
{
printf("Window Creation Failed!\n");
std::cin.ignore();
return -1;
}

retCode = createOpenGLContext(hWindow);
if (retCode != 0)
{
printf("GLRenderContext creation failed!\n");
std::cin.ignore();
return -1;
}

printf("Initialization Complete!\n");

MSG message;
while (true)
{
if (hWindow == NULL)
{
printf("hWindow is null, skipping!\n");
continue;
}

PeekMessage(&message, NULL, 0, 0, PM_REMOVE);
TranslateMessage(&message);
DispatchMessage(&message);
}

return 0;
}

这里是创建窗口:

int createWindow(HINSTANCE hInstance)
{
////////////////CREATE THE CLASS
WNDCLASS windowClass = {};
windowClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
windowClass.lpfnWndProc = windowProc;
windowClass.hCursor = LoadCursor(NULL, IDC_CROSS);
windowClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
windowClass.hInstance = hInstance;
windowClass.lpszClassName = WINDOW_CLASSNAME;

////////////////REGISTER THE CLASS
RegisterClass(&windowClass);

////////////////CREATE THE WINDOW
hWindow = CreateWindowA(WINDOW_CLASSNAME, WINDOW_NAME, WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
nullptr, nullptr, hInstance, nullptr);

ShowWindow(hWindow, SW_SHOW);
UpdateWindow(hWindow);

if (hWindow != NULL)
{
printf("hWindow is Created!\n");
return 0;
}
else
{
printf("hWindow creation failed!\n");
return -1;
}
}

最后,创建 OpenGLContext:

int createOpenGLContext(HWND hWindow)
{
hDeviceContext = GetDC(hWindow);
if (hDeviceContext == NULL)
{
printf("Device context is null!\n");
return -1;
}

///////////////////SET PIXEL FORMAT
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.iLayerType = PFD_MAIN_PLANE;

int chosenFormat = ChoosePixelFormat(hDeviceContext, &pfd);
if (SetPixelFormat(hDeviceContext, chosenFormat, &pfd) == TRUE)
{
printf("PixelFormat set!\n");
}
else
{
printf("PixelFormat couldn't be set!\n");
return -1;
}

hGLRenderContext = wglCreateContext(hDeviceContext);
if (hGLRenderContext == NULL)
{
char errorString[256];
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), 0, errorString, 255, NULL);

printf("RenderContext is null!\n");
std::cout << errorString << "\n";
return -1;
}

wglMakeCurrent(hDeviceContext, hGLRenderContext);

return 0;
}

有人知道为什么将 WS_OVERLAPPEDWINDOW 更改为 WS_POPUP 会破坏它吗?

编辑:这是请求的 windowProc:

LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
int x;
int y;

switch (uMsg)
{
case WM_MOUSEMOVE:
x = LOWORD(lParam);
y = HIWORD(lParam);

//printf("Mouse Movement: %d, %d\n", x, y);
break;

case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case 105:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
}
break;
case WM_PAINT:
{
printf("WM_PAINT message!\n");
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code that uses hdc here...
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}

最佳答案

您没有显示您的 WindowProc,但是如果您的窗口根据 WS_POPUPWS_OVERLAPPENDWINDOW 显示不同的行为,首先要检查的是如果您的代码依赖于 WM_SIZE 进行 View 重新配置(glViewportgluPerspective 等)。如果是这种情况,也请在初始化窗口后立即调用此代码。

关于c++ - openGL 渲染在 WS_OVERLAPPEDWINDOW 中工作,但在 WS_POPUP 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46630268/

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