简体   繁体   English

无法释放设备上下文,HWND,也无法注销Windows类(OpenGL)-

[英]Cannot release Device Context, HWND, and cannot unregister Windows Class(OpenGL) -

I am following the http://nehe.gamedev.net/tutorial/creating_an_opengl_window_(win32)/13001/ OpenGL tutorial, and I got the code from that working. 我正在关注http://nehe.gamedev.net/tutorial/creating_an_opengl_window_(win32)/13001/ OpenGL教程,并且我从该代码中获取了代码。 Now, I am trying to organise things by using multiple classes. 现在,我试图通过使用多个类来组织事物。 As I was creating this class, I became unable to release the Device Context, HWND and I couldn't unregister the Windows Class. 在创建此类时,我无法释放设备上下文,HWND,也无法注销Windows类。 The code below is the code used to check if they can be released or not: 以下代码是用于检查它们是否可以释放的代码:

GLvoid KillGLWindow(GLvoid){ 
if (fullscreen){
    ChangeDisplaySettings(NULL, 0);
    ShowCursor(true);
}
if (hRC){
    if (!wglMakeCurrent(NULL, NULL)){
        MessageBox(NULL, "Release of DC and RC failed", "Shutdown error", MB_OK | MB_ICONINFORMATION);
    }
    if (!wglDeleteContext(hRC)){
        MessageBox(NULL, "Release of RC failed", "Shutdown error", MB_OK | MB_ICONEXCLAMATION);
    }
    hRC = NULL;
}
if (hDC && !ReleaseDC(hWnd, hDC)){
    MessageBox(NULL, "Release of DC failed", "Shutdown error", MB_OK | MB_ICONINFORMATION);
    hDC = NULL;
}
if (hWnd && !DestroyWindow(hWnd)){
    MessageBox(NULL, "Release of hWnd failed", "Shutdown error", MB_OK | MB_ICONINFORMATION);
    hWnd = NULL;
}
if (!UnregisterClass("OpenGL", hInstance)){
    MessageBox(NULL, "Could not unregister window class", "Shutdown error", MB_OK | MB_ICONINFORMATION);
    hInstance = NULL;
}
}

(The last three if statments fired) (如果解禁,则最后三个)

The code I was moving around that caused these errors is the key detecting code in the WinMain function. 我正在移动的导致这些错误的代码是WinMain函数中的关键检测代码。 This is the only code I changed. 这是我更改的唯一代码。

else{
        if (active){

            if (testKey.isEsc()){
                done = true;
            }
            if (testKey.isA()){
                KillGLWindow();
            }
            else{
                DrawGLScene();
                SwapBuffers(hDC);
            }
        }
            if (testKey.isF1()){
                //Keys::keys[VK_F1] = false;
                KillGLWindow();
                fullscreen = !fullscreen;
                if (!CreateGLWindow("XcoxGL", 640, 480, 16, fullscreen)){
                    return 0;
                }
            }

What I changed is the testKey.THING part. 我更改的是testKey.THING部分。 testKey is initiated in the main class by the line testKey在主类中由以下行启动

Keys testKey

The Keys.cpp looks like this: Keys.cpp看起来像这样:

bool Keys::keys[256] = { false };

bool Keys::isA(){
    if (&keys[0x41]){
        return true;
    }
    else{
        return false;
    }
}

bool Keys::isF1(){
    if (&keys[VK_F1]){
        return true;
    }
    else{
        return false;
    }
}

bool Keys::isEsc(){
    if (&keys[VK_ESCAPE]){
        return true;
    }
    else{
        return false;
    }
}

And finally, Keys.h looks like this: 最后,Keys.h如下所示:

#pragma once
class Keys{
public:
    static bool keys[256];
     bool isA();

     bool isF1();

     bool isEsc();
};

I can post the full code if you want, but the way I create my DC and HWND is shown and explained at the tutorial I posted above. 我可以根据需要发布完整的代码,但是我在上面发布的教程中显示并解释了创建DC和HWND的方式。

Does anyone know what in my Key code is causing my DC and HWND to not be able to release? 有谁知道我的密钥代码中的什么导致我的DC和HWND无法释放?

Probably KillGLWindow is called more than one time, and its not really proper, try this: 可能是KillGLWindow被调用了多次,并且它不是很正确,请尝试以下操作:

GLvoid KillGLWindow(GLvoid){ 
if (fullscreen){
    ChangeDisplaySettings(NULL, 0);
    ShowCursor(true);
}
if (hRC){
    if (!wglMakeCurrent(NULL, NULL)){
        MessageBox(NULL, "Release of DC and RC failed", "Shutdown error", MB_OK | MB_ICONINFORMATION);
    }
    if (!wglDeleteContext(hRC)){
        MessageBox(NULL, "Release of RC failed", "Shutdown error", MB_OK | MB_ICONEXCLAMATION);
    }
}
hRC = NULL;
if (hDC && !ReleaseDC(hWnd, hDC)){
    MessageBox(NULL, "Release of DC failed", "Shutdown error", MB_OK | MB_ICONINFORMATION);
}
hDC = NULL;
if (hWnd && !DestroyWindow(hWnd)){
    MessageBox(NULL, "Release of hWnd failed", "Shutdown error", MB_OK | MB_ICONINFORMATION);
}
hWnd = NULL;
if (hInstance && !UnregisterClass("OpenGL", hInstance)){
    MessageBox(NULL, "Could not unregister window class", "Shutdown error", MB_OK | MB_ICONINFORMATION);
}
hInstance = NULL;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM