Sign in to follow this  

Could somebody check my basic window code

This topic is 3720 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites

This topic is 3720 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this