sorry, I spent a whole day cleaning up my disk so I can install a new big MSVC .Net, I thought the code does not compile because I am using MSVC 6.0, so I tried to compile it with .Net, unfortunately, the problems still isn't solved, CreateWindow() still fails and returns 0. I then tried in a yet another different compiler, Dev-C++ 4.9.8.0, the result still is the same.
"Does the WM_CREATE message box ever come up?"
no. I changed my code as Ken Fitlike suggested, this time the program run but it does no show, I can only see it in Windows Task Manager, the Processes tab, it is not in the Applications tab.
"Did you replace the default of the switch statement with a call to DefWindowProc instead?"
Is this just what I need to do? I did it, but the test.Create() still returns 0.
"After modifying his code using Ken's suggestions, my version of his program runs properly."
Dante Shamest, what compiler are you using? I wonder why mine doesn't works properly.
This is my revised code:
main.cpp
Code:
#include <windows.h>
#include "Window.h"
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
int returnvalue;
CWindow test(hInstance);
returnvalue = test.RegisterWindow();
returnvalue = test.Create();
// I checked the return value in debugging, found that
// test.RegisterWindow() returns 1 while
// test.Create() returns 0.
MSG msg;
while((returnvalue = GetMessage(&msg, NULL, 0, 0)) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
[edit]
sorry, forget this:
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:
DestroyWindow(hwnd);
PostQuitMessage(0);
break;
default:
DefWindowProc(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 hasn't changed.