Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Daaark

Problem bringing up a window in Win32

This topic is 6190 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

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

Share this post


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

Share this post


Link to post
Share on other sites


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?|
---------

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Windows Game Programming For Dummies was written by Andre LaMothe also, but I would reccommend the Tricks Of The Windows Game Programming Gurus anyway.

Share this post


Link to post
Share on other sites
Both are good books, but TOTWGPG is definetly better and more informative. He expands a lot more in that book.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!