Archived

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

Spintwo

Window->Not Work?

Recommended Posts

Why doesn''t this work?
#include "declare.h"
#include <windows.h>
///////////////////////////////////

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)
{


	WNDCLASSEX	winclass = 
	{
		winclass.cbSize = sizeof(WNDCLASSEX),
		CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW,
		WindowProc,
		0,
		0,
		hinstance,
		LoadIcon(NULL, IDI_APPLICATION),
		LoadCursor(NULL, IDC_ARROW),
		GetStockObject(BLACK_BRUSH),
		NULL,
		"WINCLASS1",
		LoadIcon(NULL, IDI_APPLICATION)
	};

	if (!RegisterClassEx(&winclass))
		return(0);

	if (!(hwnd = CreateWindowEx(NULL,
			"WINCLASS1",
			"cPong",
			WS_POPUP | WS_VISIBLE,
			0,0,
			SCREENWIDTH,
			SCREENHEIGHT,
			NULL,
			NULL,
			hinstance,
			NULL)))
		return(0);

	while(GetMessage(&msg,NULL,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return(msg.wparam);

}
(declare.h)
#ifndef _DECLARE_VARIABLES_
#define _DECLARE_VARIABLES_
////////////////////////////////////////

#define	WIN32_LEAN_AND_MEAN
#define SCREENWIDTH 640
#define SCREENHEIGHT 480
////////////////////////////////////////

#endif

Charles Hwang -Also/Previously known as Tazel Personal >>[MonkeyHumor-Under Developement | E-mail] Programming >>[NeXe|NeHe|SDL] Resource >>[Google|Dev-C++|GDArticles|C++.com|MSDN]

Share this post


Link to post
Share on other sites
I cannot stress how that is a BAD way to ask a question - not only do I not know what the problem is, but I don''t even know if it''s compile time, run time, being caused by something else on your computer, etc. Next time tell us!

Anyways:

default:break; }
return (DefWindowProc(hwnd, msg, wparam, lparam));

These two lines are wrong. You want to do the call to DefWindowProc for the default: case, and you generally return zero yourself. So change them too:

default:
return (DefWindowProc(hwnd, msg, wparam, lparam));
}
return 0;

Share this post


Link to post
Share on other sites
Does that even compile? Well after you do what UberGeek said take a look at this also:


#include <windows.h>


///////////////////////////////////

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);
}
return 0;
}


int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance,
LPSTR lpcmdline, int nshowcmd)
{
HWND hwnd; //<--not declared

MSG msg;//<- same


WNDCLASSEX winclass =
{
winclass.cbSize = sizeof(WNDCLASSEX),
CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW,
WindowProc,
0,
0,
hinstance,
LoadIcon(NULL, IDI_APPLICATION),
LoadCursor(NULL, IDC_ARROW),
(HBRUSH)GetStockObject(BLACK_BRUSH),//<- U need to cast this

NULL,
"WINCLASS1",
LoadIcon(NULL, IDI_APPLICATION)
};

if (!RegisterClassEx(&winclass))
return(0);

if (!(hwnd = CreateWindowEx(NULL,
"WINCLASS1",
"cPong",
WS_POPUP | WS_VISIBLE,
0,0,
SCREENWIDTH,
SCREENHEIGHT,
NULL,
NULL,
hinstance,
NULL)))
return(0);

while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return(msg.wParam); //<- spelling error


}


EDIT: BTW some info on what the problem is would really make things easier...

[edited by - FtMonkey on October 9, 2003 10:28:23 PM]

Share this post


Link to post
Share on other sites
That''s not related to your callback MSG is a separate struct which is defined like this...

typedef struct tagMSG { // msg
HWND hwnd;
UINT message;
WPARAM wParam;
LPARAM lParam;
DWORD time;
POINT pt;
} MSG;

Share this post


Link to post
Share on other sites
ok. Now, I changed my main message handling loop to a PeekMessage and it compiles ok but I get the following Linker error:

cannot open Debug/cPong.exe for writing
Error executing link.exe.

WTH??!!
(source included below)

#include "declare.h"
#include <windows.h>
///////////////////////////////////

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));
}

}


}


int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance,
LPSTR lpcmdline, int nshowcmd)
{

HWND hwnd;
MSG msg;

WNDCLASSEX winclass =
{
winclass.cbSize = sizeof(WNDCLASSEX),
CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW,
WindowProc,
0,
0,
hinstance,
LoadIcon(NULL, IDI_APPLICATION),
LoadCursor(NULL, IDC_ARROW),
(HBRUSH)GetStockObject(BLACK_BRUSH),
NULL,
"WINCLASS1",
LoadIcon(NULL, IDI_APPLICATION)
};

if (!RegisterClassEx(&winclass))
return(0);

if (!(hwnd = CreateWindowEx(NULL,
"WINCLASS1",
"cPong",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0,0,
SCREENWIDTH,
SCREENHEIGHT,
NULL,
NULL,
hinstance,
NULL)))
return(0);

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

}
return(msg.wParam);


}



Charles Hwang -Also/Previously known as Tazel
Personal >>[MonkeyHumor-Under Developement | E-mail]
Programming >>[NeXe|NeHe|SDL]
Resource >>[Google|Dev-C++|GDArticles|C++.com|MSDN]

Share this post


Link to post
Share on other sites

WNDCLASSEX winclass = {
winclass.cbSize = sizeof(WNDCLASSEX), CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW,


shouldn''t this be

WNDCLASSEX winclass = {
sizeof(WNDCLASSEX),
CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW,

Share this post


Link to post
Share on other sites
Make sure your program isn't still running on the desktop if it isn't is probably still on memory so press CTRL-ALT-DELETE press the processes tab and look if your program isn't still running if it is end it and it should compile...You probably didn't quit the program as you should have and it stayed resident (undead) or there is a bug in your code (it doesn't look like it though).


[edited by - FtMonkey on October 9, 2003 11:45:20 PM]

Share this post


Link to post
Share on other sites
Thanks monkey. I would have thought of that, actually I did, and when I deleted it from the applications I thought it was gone. Didn''t think to check the processes though! And Lemurion, it works either way, I just did it to clear up ambiguities.


Charles Hwang -Also/Previously known as Tazel
Personal >>[MonkeyHumor-Under Developement | E-mail]
Programming >>[NeXe|NeHe|SDL]
Resource >>[Google|Dev-C++|GDArticles|C++.com|MSDN]

Share this post


Link to post
Share on other sites
You are welcome, yea that's something your going to run into a lot even though you quit the program it stays in the processes most of the time because you forgot to free something from memory or something like that, so just quitting the program won't do much for you in this case since it stays resident in memory

[edited by - FtMonkey on October 10, 2003 1:04:39 AM]

Share this post


Link to post
Share on other sites