[visual-studio]有时我 windows 句柄是好的有时它坏

发布时间: 2014/2/28 20:20:07
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我很困惑。有时当我启动我的窗口,返回的句柄是好的。而且有时它不。这里是我的代码

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);

}

我仍然看到了同样的问题。

解决方法 1:

您要保存到一个本地变量的指针 ( tempWin ) 和期待,您从返回后,将有效 InitWindows() 函数。而要让 GXRenderManager::mainWindow 指针,将它声明为一个实际的数据成员。所以:

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

GXRenderManager::mainWindow = tempWin;

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

我怀疑你可能有类似问题 nCmdShow ,但你还没足够的代码,以告诉。

赞助商