Window Not Displaying

Started by
1 comment, last by TheNobleOne 18 years, 6 months ago
Hello I am doing the tutorials at C-Unit for DirectX 9 until my book I am ordering arrives. Problems is that the Win32 tutorial window is not displaying. Not shure why to me the Win32 code looks fine. Maybe I am missing something.

#include<windows.h>
#define WINDOW_TITLE "Creating a Window"
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	case WM_PAINT:
		ValidateRect(hWnd, 0);
		return 0;
	}

	return DefWindowProc(hWnd, message, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	WNDCLASSEX wcex;
	wcex.cbSize = sizeof(WNDCLASSEX);
	wcex.style = CS_HREDRAW|CS_VREDRAW|CS_OWNDC;
	wcex.lpfnWndProc = WndProc;
	wcex.cbClsExtra = 0;
	wcex.cbWndExtra = 0;
	wcex.hInstance = hInstance;
	wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wcex.hCursor = LoadIcon(NULL, IDC_ARROW);
	wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName = NULL;
	wcex.lpszClassName = "MyWindowClass";
	wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

	// Register the class
	RegisterClassEx(&wcex);

	HWND hWnd = CreateWindow(WINDOW_TITLE, WINDOW_TITLE, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
		WINDOW_WIDTH, WINDOW_HEIGHT, 0, 0, hInstance, 0);

	// adjust to acctual window size
	RECT rect = {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT};

	AdjustWindowRect(&rect, GetWindowLong(hWnd, GWL_STYLE), false);
	SetWindowPos(hWnd, 0, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER|SWP_NOMOVE);

	ShowWindow(hWnd, SW_SHOW);
	UpdateWindow(hWnd);

	MSG msg;

	while(1)
	{
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			if(msg.message == WM_QUIT)
			{
				break;
			}
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{
			// Render here
		}
	}

	return 0;
}

My JournalComputer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter. (Eric Raymond)"C makes it easy to shoot yourself in the foot. C++ makes itharder, but when you do, it blows away your whole leg."-- Bjarne Stroustrup
Advertisement
Hello,

A few list of things that will help you:

1) some functions returns a value (for example, CreateWindow() returns a HWND); this value can be set to a particular value (in this case: NULL) to tell you 'something bad happened'. Therefore, it is good to have a look to these return values, because it really help when you have to spot an error.

2) the first argument of CreateWindow() is a window class name, not a window title. In your case, it should be "MyWindowClass".

3) wcex.hCursor should be set to the result of LoadCursor(), not LoadIcon(). In your case, the result is rather fun but is probably not intended :)

HTH,
Thanks a bunch the Window class name is what did it. However, that was a spot I copied from the tutorial hummph oh well. Thanks again.
My JournalComputer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter. (Eric Raymond)"C makes it easy to shoot yourself in the foot. C++ makes itharder, but when you do, it blows away your whole leg."-- Bjarne Stroustrup

This topic is closed to new replies.

Advertisement