Problem bringing up a window in Win32

Started by
13 comments, last by Daaark 22 years, 8 months ago
I keep copying out the first program in the WINDOWS GAME PROGRAMMING FOR DUMMIES BOOK, and whenever I compile, the program ends right away instead of showing a window!!! Can someone tell me what's wrong?

#define WIN32_LEAN_AND_MEAN
#include 
#include 
#include 
#include 

#define WINDOW_CLASS_NAME "WNDCLASS1"

HWND main_window_handle = NULL;

LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
	PAINTSTRUCT ps;
	HDC hdc;

	switch(msg)
	{
		case WM_CREATE:
		{
			return(0);
		} break;
		case WM_PAINT:
		{
			hdc = BeginPaint(hwnd,&ps);
			EndPaint(hwnd,&ps);
			return (0);
		} break;
		case WM_DESTROY:
		{
			PostQuitMessage(0);
			return(0);
		} break;
		default :break;
	}

	return(DefWindowProc(hwnd,msg,wparam,lparam));
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
				   LPSTR lpCmdLine, int nShowCmd)
{
	WNDCLASS winclass;
	HWND hwnd;
	MSG msg;
	
	winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
	winclass.lpfnWndProc = WindowProc;
	winclass.cbClsExtra = 0;
	winclass.cbWndExtra = 0;
	winclass.hInstance = hInstance;
	winclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
	winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	winclass.lpszMenuName = NULL;
	winclass.lpszClassName = WINDOW_CLASS_NAME;

	if (!RegisterClass(&winclass)) return 0;
	if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME,"TEST",WS_OVERLAPPED |WS_VISIBLE,
		0,0,320,200,NULL,NULL,hInstance,NULL))) return(0);
	
	main_window_handle = hwnd;

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

        //main game processing goes here
	}

	return(msg.wParam);

}
  
I hope that formats right --------- |GOT MALK?| --------- Edited by - felisandria on August 2, 2001 11:07:34 AM
Advertisement
see if this helps...
your #includes should be...
#include
#include
#include
first use WNDCLASSEX instead of WNDCLASS ... thats what I learned
second you''ll need to add a few member flags to your windows class...
winclass.cbSize = sizeof(WNDCLASSEX); // before style
winclass.hCursor = LoadCursor(NULL,IDC_CURSOR); // after hIcon
winclass.hIconSm = LoadIcon(NULL,IDI_APPLICATION); // last member

then change these...
when you register your window make it this
if(!RegisterClassEx(&winclass))
return(0);
when you create your window make it this
if (!(hwnd = CreateWindowEx(NULL,
WINDOW_CLASS_NAME,
"TEST",
WS_OVERLAPPEDWINDOW | WS_VISIBLE, //you had WS_OVERLAPPED.. I''m not sure but use this instead//
0,0,
320,200,
NULL,
NULL,
hInstance,
NULL)))
return(0);

if this doesn''t help or is useless crap... e-mail me @ ratt1233@mindspring.com
"Man has got to know... his limitations..."
Get rid of the ''default'' statement in the WndProc() function.


I commented out the default statement.. same thing happens,

and ratt1233, The includes you wrote (and I wrote) aren''t showing up on the screen... I had windows.h, windowsx.h, math.h, and stdio.h






---------
|GOT MALK?|
---------
you can e-mail me your code... I''ll se what I can do..
"Man has got to know... his limitations..."
This is a working version with minimal changes.

#define WIN32_LEAN_AND_MEAN
#include "windows.h"
//#include "windowsx.h" //You don''t need those right now
//#include "stdio.h"
//#include "math.h"
// replace the " with the brackets (the browser interprets them as html-tags)

#define WINDOW_CLASS_NAME "WNDCLASS1"
HWND main_window_handle = NULL;

LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{ PAINTSTRUCT ps;
HDC hdc;
switch(msg)
{
case WM_CREATE:
{
return(0);
} break;
case WM_PAINT:
{
hdc = BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return (0);
} break;
case WM_DESTROY:
{
PostQuitMessage(0);
return(0);
} break;
default:
{
return(DefWindowProc(hwnd,msg,wparam,lparam)); // NEW: I think it''s better to let the DefWindowProc handle all messages you don''t want to mess with
} break; // In your version default messages were unhandled producing errors
}
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASSEX winclass; // NEW: WNDCLASSEX
HWND hwnd;
MSG msg;

//NEW
ZeroMemory(&winclass, sizeof(winclass)); // This inits all values of the structure with zeros
winclass.cbSize = sizeof(winclass); // Important for version checking for windows
//NEW

winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hInstance;
winclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;


if (!RegisterClassEx(&winclass)) // NEW: RegisterClassEx
return 0; // Your version failed here, because you forgot the two lines above
if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME,"TEST",WS_OVERLAPPED |WS_VISIBLE,
0,0,320,200,NULL,NULL,hInstance,NULL)))
return(0);
main_window_handle = hwnd;
while(1)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if(msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
//main game processing goes here
}
return(msg.wParam);
}

(To quit your app press "alt+F4" because it lacks the x-button

baumep
baumep
Thanks baumep!

I''ll go try and get that to work

I guess I should toss WINDOWS GAME PROGRAMMING FOR DUMMIES in my trash I thought I would pick this stuff up quicker. I''ve programmed in Qbasic, and C with DJGPP/ALLEGRO before. That was easy This stuff is imposibble
I recomend Tricks of the Windows Game Programming Gurus by Andre Lamothe
"Man has got to know... his limitations..."
Windows Game Programming For Dummies was written by Andre LaMothe also, but I would reccommend the Tricks Of The Windows Game Programming Gurus anyway.

I know only that which I know, but I do not know what I know.
Both are good books, but TOTWGPG is definetly better and more informative. He expands a lot more in that book.

This topic is closed to new replies.

Advertisement