Please help me check my Win32 Wrapper code, it compiled right, and run with no error, but it exit immediately after running, I have look at my code many times but still don't know what's wrong with it. Here's my code:
main.cpp
Code:
#include <windows.h>
#include "Window.h"
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
CWindow test(hInstance);
test.RegisterWindow();
test.Create();
MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
Window.cpp
Code:
#include "Window.h"
LRESULT CALLBACK CWindow::staticWinMsgHandler(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
CWindow* pWnd;
if(uMsg == WM_NCCREATE)
{
SetWindowLong(hwnd, GWL_USERDATA, (long)((LPCREATESTRUCT(lParam))->lpCreateParams));
}
pWnd = (CWindow *)GetWindowLong(hwnd, GWL_USERDATA);
if(pWnd)
return pWnd->WinMsgHandler(hwnd, uMsg, wParam, lParam);
else
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
LRESULT CALLBACK CWindow::WinMsgHandler(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_CREATE:
MessageBox(NULL, "Create", "test", MB_OK);
break;
case WM_CLOSE:
PostQuitMessage(0);
break;
default:
return CWindow::WinMsgHandler(hwnd, uMsg, wParam, lParam);
}
return 0;
}
BOOL CWindow::RegisterWindow()
{
WNDCLASS wcx;
wcx.style = CS_HREDRAW | CS_VREDRAW;
wcx.lpfnWndProc = CWindow::staticWinMsgHandler;
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hInstance = m_hInstance;
wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
wcx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wcx.lpszMenuName = NULL;
wcx.lpszClassName = "Window";
return (RegisterClass(&wcx) != 0);
}
BOOL CWindow::Create()
{
m_hwnd = CreateWindow("Window", "Hello",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, m_hInstance, (void *)this);
return (m_hwnd != NULL);
}
Window.h
Code:
#ifndef Window_H_
#define Window_H_
#include <windows.h>
// The class is based on the example taken from Code Project article:
// "A Simple Win32 Window Wrapper Class" By Jason Henderson
class CWindow
{
protected:
HINSTANCE m_hInstance;
HWND m_hwnd;
char szClassName[256];
char szWindowTitle[256];
virtual LRESULT CALLBACK WinMsgHandler(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
public:
CWindow(HINSTANCE hInst)
: m_hInstance(hInst){};
virtual BOOL RegisterWindow();
virtual BOOL Create();
static LRESULT CALLBACK staticWinMsgHandler(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
};
#endif
I think that the
Code:
CWindow test(hInstance);
test.RegisterWindow();
test.Create();
runs ok, the problem is in CWindow::staticWinMsgHandler, I think.