I have a problem with this simple Win32 application I am trying to make. As you can tell by the title, the function CreateWindow() always returns 0, which indicates failure. Here is my code.
int MessageLoop()
{
MSG msg;
ZeroMemory(&msg, sizeof(MSG));
while (GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
INT WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd)
{
if (!MakeAWindow(hInstance, nShowCmd))
{
MessageBox(0, "Window Creation -- Failed", "Error", MB_OK);
return 0;
}
return MessageLoop();
}
bool MakeAWindow(HINSTANCE appInstance,int show)
{
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = appInstance;
wc.hIcon = LoadIcon(0,IDI_APPLICATION);
wc.hCursor = LoadCursor(0,IDC_ARROW);
wc.hbrBackground = static_cast<HBRUSH>(GetStockObject(BLACK_BRUSH));
wc.lpszMenuName = 0;
wc.lpszClassName = "wc";
if (!RegisterClass(&wc))
{
MessageBox(0, "Failed to register the window class", "Windows Sucks!", 0);
return false;
}
MainWindow = CreateWindow("wc",
"Hello",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
0,
0,
appInstance,
0);
if(MainWindow == NULL)
{
::MessageBox(0,"Function CreateWindow() Failed",0,0);
return false;
}
::ShowWindow(MainWindow, show);
::UpdateWindow(MainWindow);
return true;
}
LRESULT CALLBACK WndProc(HWND window,UINT msg,WPARAM wParam,LPARAM lParam)
{
switch(msg)
{
case WM_KEYDOWN:
if (wParam == VK_ESCAPE )
{
::DestroyWindow(MainWindow);
return 0;
}
case WM_DESTROY:
::PostQuitMessage(0);
return 0;
}
return DefWindowProc(MainWindow, msg, wParam, lParam);
}
Does anyone have any ideas as to why this will not work? I have searched for a while and cannot think of anything. I need some new eyes on this code.