Click to See Complete Forum and Search --> : Window Not showing up but compiles fine


dan369
July 24th, 2010, 08:23 PM
Hi, guys/gals will rather new here to the windows API but been following a few tutorials online, looking more in-depth at each function i use using MSDN.
So got a bit bored earlier, so decided to create my very own window using my other code as a template, looking at msdn in particular i was looking more at all the other options available to me to use in creating my window.

So after about half an hour, finally got the code done and compiled up without any errors, when to run it and well the window doesn't show :(

So anyway here's the code:

/*
Creating a simple Window using windows API.
*/

//Includes
#include <windows.h>

//Setup Callbacks
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
//Closing the Window by clicking the X
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_KEYDOWN:
switch(wParam)
{
case VK_ESCAPE:
DestroyWindow(hwnd);
break;
default:
break;
}
//Destoy Open Windows and any Child windows
case WM_DESTROY:
//Indicate to the system that we want to quit
PostQuitMessage(0);
break;
default:
//Use Default window procedure
DefWindowProc(hwnd, msg, wParam, lParam);
}

return 0;
}


//Main Window Function
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
//Window handler
HWND hWnd;
//Create Windows ClassEX struct
WNDCLASSEX wc;
//Create message struct
MSG Msg;

//Register our Window Class
wc.cbSize = sizeof(WNDCLASSEX); //Size of the structrure in bytes needed
wc.style = CS_HREDRAW | CS_VREDRAW; //Redraw if window width/height is ever changed. Also have a shadow effect here
wc.lpfnWndProc = WindowProc; //Use our window procedure function above
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW); //load default cursor
wc.hbrBackground = (HBRUSH)COLOR_WINDOW; //Background colour for our window
wc.lpszClassName = L"WindowClass"; //String of our class name
RegisterClassEx(&wc); //Finally register the window classEX structure

//Create Our window to overlap also with a vertical scroller, width and height = 300.
hWnd = CreateWindow(L"WindowClass", L"My Window", WS_OVERLAPPEDWINDOW, 300, 300, 800, 600, NULL, NULL, hInstance, NULL);
//Show our window, controls how the window is to be shown, default is "Normal".
ShowWindow(hWnd, nCmdShow);
//Update Our Window
UpdateWindow(hWnd);

//GetMessage waits until a posted message is available, PeekMessage doesn't wait
while(GetMessage(&Msg, NULL, 0, 0))
{
//Translates Virtual Key messages into character ones so they can be used
TranslateMessage(&Msg);
//Dispatches a message to the window procedure
DispatchMessage(&Msg);
}
return Msg.wParam;
}


Please excuse the heavy commenting, just helps me personally remember :). Would love to now what exactly i'm doing wrong here.

Paul McKenzie
July 24th, 2010, 09:32 PM
Hi, guys/gals will rather new here to the windows API but been following a few tutorials online, looking more in-depth at each function i use using MSDN. Those tutorials should have told you to check the return codes for those functions.

Your code assumes that hWnd is valid, when it may not be. You should be checking hWnd for a NULL value. Secondly, use the debugger to see what the program is really doing. For example, that call to RegisterClassEx() probably fails, since you did not fill in the entire WNDCLASSEX strudture with valid values.

ZeroMemory(&wc, sixeof(wc));
For Windows API structures, the usual default for all members of the structure is 0, so zeroing out the struct should be done before you assign values to the members.

Also, you are to return the value of DefWindowProc() in the Window procedure. Just calling it and always returning 0 is wrong.

Regards,

Paul McKenzie

Igor Vartanov
July 26th, 2010, 07:48 AM
Paul's answer illustrated:
/*
Creating a simple Window using windows API.
*/

#define WRONG_DEFAULT_PROCESSING 0
#define GARBAGE_IN_WINDOWCLASS 0

//Includes
#include <windows.h>

//Setup Callbacks
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
//Closing the Window by clicking the X
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_KEYDOWN:
switch(wParam)
{
case VK_ESCAPE:
DestroyWindow(hwnd);
break;
default:
break;
}
//Destoy Open Windows and any Child windows
case WM_DESTROY:
//Indicate to the system that we want to quit
PostQuitMessage(0);
break;
#if WRONG_DEFAULT_PROCESSING
default:
DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
#else
}
return DefWindowProc(hwnd, msg, wParam, lParam);
#endif

}


//Main Window Function
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
//Window handler
HWND hWnd = NULL;
//Create Windows ClassEX struct
#if GARBAGE_IN_WINDOWCLASS
WNDCLASSEX wc;
#else
WNDCLASSEX wc = {0};
#endif

//Create message struct
MSG Msg;

//Register our Window Class
wc.cbSize = sizeof(WNDCLASSEX); //Size of the structrure in bytes needed
wc.style = CS_HREDRAW | CS_VREDRAW; //Redraw if window width/height is ever changed. Also have a shadow effect here
wc.lpfnWndProc = WindowProc; //Use our window procedure function above
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW); //load default cursor
wc.hbrBackground = (HBRUSH)COLOR_WINDOW; //Background colour for our window
wc.lpszClassName = L"WindowClass"; //String of our class name
ATOM aReg = RegisterClassEx(&wc); //Finally register the window classEX structure

if (!aReg)
{
MessageBoxW(NULL, L"Registration failed.", wc.lpszClassName, MB_OK);
return -1;
}

//Create Our window to overlap also with a vertical scroller, width and height = 300.
hWnd = CreateWindow(L"WindowClass", L"My Window", WS_OVERLAPPEDWINDOW, 300, 300, 800, 600, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
DWORD err = GetLastError();
WCHAR msg[200] = {0};
wsprintfW(msg, L"Failed with last error: %d", err);
MessageBoxW(NULL, msg, L"CreateWindow", MB_OK);
return -2;
}

//Show our window, controls how the window is to be shown, default is "Normal".
ShowWindow(hWnd, nCmdShow);
//Update Our Window
UpdateWindow(hWnd);

//GetMessage waits until a posted message is available, PeekMessage doesn't wait
while(GetMessage(&Msg, NULL, 0, 0))
{
//Translates Virtual Key messages into character ones so they can be used
TranslateMessage(&Msg);
//Dispatches a message to the window procedure
DispatchMessage(&Msg);
}
return Msg.wParam;
}