I am confused. Sometimes when I initiate my window, the Handle returned is good. and sometimes its not. Here is my code
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);
}
Sometimes GXRenderManager::mainWindow returns a number, but most of the time it returns a "expression can not be evaluated. Any takers ??
[edit]
the header
#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 is a static member . Prior to the responses below. I updated the code to the following...
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);
}
Still I see the same issue.
You are saving a pointer to a local variable (tempWin
) and expecting that will be valid after you return from the InitWindows()
function. Instead of making GXRenderManager::mainWindow
a pointer, declare it as an actual data member. So:
class GXRenderManager {
...
HWND mainWindow; // not a pointer
...
};
GXRenderManager::mainWindow = tempWin;
ShowWindow(GXRenderManager::mainWindow, *GXRenderManager::nCmdShow);
I suspect you might have a similar problem with nCmdShow
, but you haven't shown enough of your code to tell.
Hai
Problem lies with handle HWND tempWin;
, this is local variable of function, which mean it cleared from stack once function exit. it will lose it's memeory..
GXRenderManager::mainWindow = &tempWin;, so instead of storing reference of stakc variable, better store it's value
精彩评论