Original Post
I've created an OO window/app system, but it's not quite working. My WndProc function seems to become invalid after I create the window. I don't get any errors, but Alt-F4 and closing the window both fail to actually stop the app, whereas manually setting mainLoopDone elsewhere still does. Can anyone help me figure out what went wrong?
HRESULT App::Initialize(const AppParameters& ap)
{
HRESULT hr = window.RegisterWindowClass(ap.wndStyle, App::StaticWndProc, ap.wndIcon, ap.wndCursor, NULL, ap.wndName);
if(FAILED(hr))
return hr;
...
}
LRESULT App::WndProc(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
if(msg == WM_DESTROY ||
msg == WM_CLOSE ||
msg == WM_QUIT)
{
mainLoopDone = true;
return 0;
}
return (DefWindowProc(wnd, msg, wparam, lparam));
}
LRESULT CALLBACK App::StaticWndProc(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
App* parent;
if(msg == WM_NCCREATE)
{
parent = (App*)((LPCREATESTRUCT)lparam)->lpCreateParams;
SetWindowLongPtr(wnd, GWL_USERDATA, (LONG_PTR)parent);
}
else
{
parent = (App*)GetWindowLongPtr(wnd, GWL_USERDATA);
if(!parent)
return (DefWindowProc(wnd, msg, wparam, lparam));
}
return parent->WndProc(wnd, msg, wparam, lparam);
}
HRESULT Window::RegisterWindowClass(UINT style, WNDPROC wndproc, HICON icon, HCURSOR cursor, HBRUSH background, LPCSTR className)
{
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.hbrBackground = background;
windowClass.hCursor = cursor;
windowClass.hIcon = icon;
windowClass.hInstance = GetModuleHandle(NULL);
windowClass.lpfnWndProc = wndproc;
windowClass.lpszClassName = className;
windowClass.lpszMenuName = NULL;
windowClass.style = style;
HRESULT hr = RegisterClass(&windowClass);
...
}