有时我的窗户手柄很好,有时很糟糕

时间:2010-06-23 03:29:00

标签: c++ c visual-studio visual-c++

我很困惑。有时当我启动窗口时,Handle返回是好的。有时它不是。这是我的代码

void GXRenderManager::InitWindows()
{
    WNDCLASSEX wndcex;

    wndcex.cbSize   = sizeof(WNDCLASSEX);
    wndcex.style    = CS_HREDRAW | CS_VREDRAW;
    wndcex.lpfnWndProc = GXRenderManager::WndProc;
    wndcex.cbClsExtra = 0;
    wndcex.cbWndExtra = 0;
    wndcex.hInstance = *GXRenderManager::hinstance;
    wndcex.hIcon = LoadIcon(NULL,IDI_APPLICATION);
    wndcex.hCursor  =   LoadCursor(NULL,IDC_ARROW);
    wndcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
    wndcex.lpszMenuName = NULL;
    wndcex.lpszClassName = L"GXRenderClass";
    wndcex.hIconSm  =   LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wndcex))
        throw GXWindowsException(L"Failed To Register Window");

    //EDIT AREA: This is a static size for window. Needs to be changed for dynamic size
    RECT rectangle = {0,0,GXRenderManager::width,GXRenderManager::height};
    AdjustWindowRect(&rectangle, WS_OVERLAPPEDWINDOW, FALSE);

    HWND tempWin;

    tempWin = CreateWindowEx(WS_EX_CLIENTEDGE,L"GXRenderClass",L"GXRender Engine",
        WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT, 
        (rectangle.right - rectangle.left),
        (rectangle.bottom - rectangle.top), NULL, NULL,*GXRenderManager::hinstance, NULL);

    if(!tempWin)
        GXWindowsException(L"GX had an issue creating a window.");

    GXRenderManager::mainWindow = &tempWin;

    ShowWindow(*GXRenderManager::mainWindow, *GXRenderManager::nCmdShow);

}

有时候GXRenderManager :: mainWindow会返回一个数字,但大多数时候它返回一个“表达式无法评估。任何接受者?

[编辑]

标题

#ifndef GXRM
#define GXRM
#include <windows.h>
#include "DeviceEnum.h"
#include "GXRenderDevice.h"
#include "GXExceptions.h"

class GXRenderManager {

public:
    static int Ignite(HINSTANCE*, int*, GXDEVICE , int w = 800, int h = 600);
    static GXRenderer* Device();
    static void InitWindows();
    static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);

    static int Run();

private:
    bool running;
    static GXRenderer *renderDevice;

protected:
    static HINSTANCE * hinstance;
    static int *nCmdShow;
    static HWND mainWindow;
    static int width;
    static int height;

};

#endif

的.cpp

GXRenderManager :: mainWindow是一个静态成员。在下面的回复之前。我将代码更新为以下内容......

void GXRenderManager::InitWindows()
{
    WNDCLASSEX wndcex;

    wndcex.cbSize   = sizeof(WNDCLASSEX);
    wndcex.style    = CS_HREDRAW | CS_VREDRAW;
    wndcex.lpfnWndProc = GXRenderManager::WndProc;
    wndcex.cbClsExtra = 0;
    wndcex.cbWndExtra = 0;
    wndcex.hInstance = *GXRenderManager::hinstance;
    wndcex.hIcon = LoadIcon(NULL,IDI_APPLICATION);
    wndcex.hCursor  =   LoadCursor(NULL,IDC_ARROW);
    wndcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
    wndcex.lpszMenuName = NULL;
    wndcex.lpszClassName = L"GXRenderClass";
    wndcex.hIconSm  =   LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wndcex))
        throw GXWindowsException(L"Failed To Register Window");

    //EDIT AREA: This is a static size for window. Needs to be changed for dynamic size
    RECT rectangle = {0,0,GXRenderManager::width,GXRenderManager::height};
    AdjustWindowRect(&rectangle, WS_OVERLAPPEDWINDOW, FALSE);

    HWND tempWin;

    tempWin = CreateWindowEx(WS_EX_CLIENTEDGE,L"GXRenderClass",L"GXRender Engine",
        WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT, 
        (rectangle.right - rectangle.left),
        (rectangle.bottom - rectangle.top), NULL, NULL,*GXRenderManager::hinstance, NULL);

    if(!tempWin)
        GXWindowsException(L"GX had an issue creating a window.");

    GXRenderManager::mainWindow = tempWin;

    ShowWindow(GXRenderManager::mainWindow, *GXRenderManager::nCmdShow);

}

我仍然看到同样的问题。

2 个答案:

答案 0 :(得分:1)

您正在保存指向本地变量(tempWin)的指针,并期望从InitWindows()函数返回后该指针有效。而不是使GXRenderManager::mainWindow指针,将其声明为实际的数据成员。所以:

class GXRenderManager {
    ...
    HWND mainWindow; // not a pointer
    ...
};

GXRenderManager::mainWindow = tempWin;

ShowWindow(GXRenderManager::mainWindow, *GXRenderManager::nCmdShow);

我怀疑您可能会遇到与nCmdShow类似的问题,但是您没有展示足够的代码。

答案 1 :(得分:0)

问题在于句柄HWND tempWin;,这是函数的局部变量,这意味着一旦函数退出就会从栈中清除。它将失去它的记忆..

GXRenderManager :: mainWindow =&amp; tempWin;,所以不是存储stakc变量的引用,而是更好地存储它的值