所有教程都向您展示了如何创建 OpenGL 上下文以覆盖整个窗口空间。
例如,使用树视图,您可以这样做:
HWND treeViewHwnd = CreateWindowEx(WS_EX_CLIENTEDGE, WC_TREEVIEW, 0, WS_CHILD | WS_VISIBLE, 0, 0, 192, rc.bottom, hwnd, NULL, hInstance, NULL);
创建占据屏幕一部分的树视图(192 宽)。
但是对于 OpenGL,我似乎无法创建从 192,0 到应用程序窗口右下角的上下文。这是代码:
HWND openGLHwnd = CreateWindowEx(WS_EX_CLIENTEDGE, "openGLHwnd", 0, CS_OWNDC | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_CHILD | WS_VISIBLE, 192, 0, rc.right, rc.bottom, hwnd, NULL, hInstance, NULL);
HDC hdc;
HGLRC hrc;
PIXELFORMATDESCRIPTOR pfd;
int format;
hdc = GetDC(openGLHwnd);
memset(&pfd, '\0', sizeof(pfd));
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
format = ChoosePixelFormat(hdc, &pfd);
SetPixelFormat(hdc, format, &pfd);
hrc = wglCreateContext(hdc);
if(hrc == NULL) return 0;
if(wglMakeCurrent(hdc, hrc) == FALSE) return 0;
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glEnable(GL_COLOR_MATERIAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_NORMALIZE);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, rc.right, rc.bottom);
gluPerspective(45.0f, (GLfloat)rc.right / (GLfloat)rc.bottom, 0.1f, 1024.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glClear(GL_COLOR_BUFFER_BIT);
SwapBuffers(hdc);
ShowWindow(hwnd, nCmdShow);
ShowWindow(openGLHwnd, SW_SHOW);
UpdateWindow(hwnd);
UpdateWindow(openGLHwnd);
空间中什么都没有显示,尽管应该有一个红色框,因为这是我设置的清晰颜色。