LRESULT CALLBACK MainWnd::WindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) { if(WM_NCCREATE == uMsg) { if(!DefWindowProc(hWnd, uMsg, wParam, lParam)) { return FALSE; } /* Cannot find /this/ pointer if a message (e.g. WM_GETMINMAXINFO) is sent before this message. */ new MainWnd(hWnd); return TRUE; } MainWnd *wnd = reinterpret_cast< MainWnd * >(GetWindowLong(hWnd, GWL_USERDATA)); switch(uMsg) { case WM_NCDESTROY: delete wnd; // if NC create failed, wnd should be NULL so this is OK break; // chain to default handler case WM_CREATE: return wnd->on_create(reinterpret_cast< LPCREATESTRUCT >(lParam)); case WM_DESTROY: wnd->on_destroy(); return 0; case WM_PAINT: wnd->paint(); return 0; case WM_COMMAND: wnd->on_command(HIWORD(wParam), LOWORD(wParam), reinterpret_cast< HWND >(lParam)); return 0; } return DefWindowProc(hWnd, uMsg, wParam, lParam); }