Could somebody check my basic window code

Started by
7 comments, last by nobodynews 16 years, 6 months ago
So, I just completed my very first piece of windows code. I was wondering if somebody could check this piece of code, because the problem is: I can run the program, compiling doesn't give my any warnings or errors and I can see the program running in task manager. However, I can't see the program on my screen. It just doesn't show up! help me? Please!?
#include <windows.h>
HWND			hMainWindowHandle;

const int		WINDOW_POSX = 150;
const int		WINDOW_POSY = 150;
const int		WINDOW_WIDTH = 500;
const int		WINDOW_HEIGHT = 400;

bool			InitWindowsApp(HINSTANCE hInstanceHandle, int nShowCmdHandle);
int				WindowMessageLoop();

LRESULT CALLBACK WndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	if (!InitWindowsApp(hInstance, nShowCmd)) {
		MessageBox(0, "initializing failed", "Error", MB_OK);
		return 0;
	}

	return WindowMessageLoop();
}

bool InitWindowsApp (HINSTANCE hInstanceHandle, int nShowCmdHandle)
{
	WNDCLASS	WindowClass;

	WindowClass.style			= CS_HREDRAW | CS_VREDRAW;
	WindowClass.lpfnWndProc		= WndProc;
	WindowClass.cbClsExtra		= 0;
	WindowClass.cbWndExtra		= 0;
	WindowClass.hInstance		= hInstanceHandle;
	WindowClass.hIcon			= LoadIcon(0, IDI_APPLICATION);
	WindowClass.hCursor			= LoadCursor(0, IDC_ARROW);
	WindowClass.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	WindowClass.lpszMenuName	= 0;
	WindowClass.lpszClassName	= "wndClassName";

	if (!RegisterClass(&WindowClass)) {
		MessageBox(0, "Registering class failed", "Error", MB_OK);
		return 0;
	}

	hMainWindowHandle = CreateWindow("wndClassName",
								 "wndClassName",
								 WS_OVERLAPPEDWINDOW,
								 WINDOW_POSX,
								 WINDOW_POSY,
								 WINDOW_WIDTH,
								 WINDOW_HEIGHT,
								 0,
								 0,
								 hInstanceHandle,
								 0);

	if (!hMainWindowHandle) {
		MessageBox(0, "Creating Window Failed", "Error", MB_OK);
		return false;
	}

	return true;
}

int WindowMessageLoop()
{
	MSG msg;
	ZeroMemory(&msg, sizeof(msg));
	while (true) {
		if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
			if (msg.message == WM_QUIT) {
				break;
			}

			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else {
			//game code goes here
		}
	}
	return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
	switch (msg) {
		case WM_LBUTTONDOWN:
			MessageBox(0, "You clicked the left mouse button!", "Information", MB_OK);
			return 0;
		case WM_KEYDOWN:
			if ( wParam == VK_ESCAPE ) {
				DestroyWindow(hMainWindowHandle);
				return 0;
			}
		case WM_DESTROY:
			PostQuitMessage(0);
			return 0;
	}

	return DefWindowProc(hWnd, msg, wParam, lParam);
}
Thanks in advance, Max Henger
Advertisement
I'm not sure if this is the problem but I don't see a ShowWindow anywhere. Try adding:

ShowWindow(hMainWindowHandle, SW_SHOW);

After you create the window.
Best regards, Omid
Never knew I even learned of my stupid mistakes ;)

Thanks alot, that worked :P
Also note this piece of code:
case WM_KEYDOWN:   if ( wParam == VK_ESCAPE ) {      DestroyWindow(hMainWindowHandle);      return 0;case WM_DESTROY:  PostQuitMessage(0);}

You forgot a break statement here. This will terminate your application no matter which key you press (it continues to the next label WM_DESTROY and call PostQuitMessage(0)). Probably not what you want.
Quote:
bool InitWindowsApp (HINSTANCE hInstanceHandle, int nShowCmdHandle)


The name hInstanceHandle is redundant. That's what the 'h' stands for. As for nShowCmd, that's not even a handle.
Ra
Thanks guys! Learning something every day. I'll go over my code and check for stupid variable names and coding inconsistensies (sp?)!
Quote:Original post by WanMaster
Also note this piece of code:
case WM_KEYDOWN:   if ( wParam == VK_ESCAPE ) {      DestroyWindow(hMainWindowHandle);      return 0;case WM_DESTROY:  PostQuitMessage(0);}

You forgot a break statement here. This will terminate your application no matter which key you press (it continues to the next label WM_DESTROY and call PostQuitMessage(0)). Probably not what you want.


There may be no break, but the return will take care of that.
Quote:Original post by Zahlman
There may be no break, but the return will take care of that.

Not exactly.

If a key other than escape is pressed, the absence of a break (the return is inside the statement block body of the if; don't look at WanMaster's quote - look at the original post) causes the logic to fall through to the WM_DESTROY label and PostQuitMessage call.
Quote:Original post by Oluseyi
Quote:Original post by Zahlman
There may be no break, but the return will take care of that.

Not exactly.

If a key other than escape is pressed, the absence of a break (the return is inside the statement block body of the if; don't look at WanMaster's quote - look at the original post) causes the logic to fall through to the WM_DESTROY label and PostQuitMessage call.


Perhaps he thought the if statement looked like this:
case WM_KEYDOWN:   if ( wParam == VK_ESCAPE )      DestroyWindow(hMainWindowHandle);      return 0;

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

This topic is closed to new replies.

Advertisement