Advertisement

this seems like it should work!!

Started by July 02, 2002 02:33 PM
8 comments, last by Lucidquiet 22 years, 6 months ago
WinClassMaker winClass // from main winClass is declarred // in the constructor the WNDCLASSEX // is filled out winClass.Register(); // Then next a method to register the // class with windows. void WinClassMaker::Register() { ::RegisterClassEx (&_class); } // but its like it never gets here. I''m a little lost here -- why won''t this work. I get an unexpected termination runtime error. And an assertion before showing and updating the window which fails... once again as if ::Register doesn''t do its job? What the hell''''s this for?
"Education is when you read the fine print; experience is what you get when you don't." -Pete Seegerwww.lucid-edge.net
You''ve declared a WNDCLASSEX variable in your class I suppose?

Post the source to your class and we''ll look at it.
Advertisement
Your window procedure is static, I presume? if not, search the forum for the dozens of threads on the subject of wrapping Win32 windowclasses.

Oh, and check out my article on the topic. It''s somewhere around here...

  /*-------------The Decleration:------------*/class WinClassMaker{public:	WinClassMaker (WNDPROC WinProcedure,		char const * className,		HINSTANCE hInst);	void Register();private:	WNDCLASSEX _class;};/*--------------------------------------------*/WinClassMaker::WinClassMaker (WNDPROC WinProcedure,					  char const * className,					  HINSTANCE hInst){_class.lpfnWndProc = WinProcedure;_class.hInstance = hInst;				_class.lpszClassName = className;		_class.cbSize = sizeof(WNDCLASSEX);_class.hCursor = ::LoadCursor (0, IDC_ARROW);_class.hbrBackground = reinterpret_cast<HBRUSH>	(COLOR_WINDOW + 1);_class.style = 0;_class.cbClsExtra = 0;_class.hIcon = 0;_class.hIconSm = 0;_class.lpszMenuName = 0;}void WinClassMaker::Register(){if(::RegisterClassEx (&_class) == 0)     throw "RegisterClass Failed";}/*-----------------------------------------------------_class being a member varialbe of WinClassMaker and declared as as a WNDCLASSEX.-----------------------------------------------------*//*-----------Class WinMaker----------*/class WinMaker{public:	WinMaker (char const * className,		HINSTANCE hInst);	HWND Create (char const * title);private:	HINSTANCE	_hInst;		// Program instance	char const	*_className;	// Name of window class	DWORD		_style;		// Window Style	DWORD		_exStyle;	// Window extended style	int		_x;		// Horizontal position	int		_y;      	// Vertical position	int		_width;		// Window width	int		_height;	// Window height	HWND		_hWndParent;	// Parent or owner	HMENU		_hMenu;		//Menu or child-windowID	void		*_data;		// window-creation data};/*-------------END DECLARATION--------------*/WinMaker::WinMaker (char const * className,		HINSTANCE hInst)	:	_style (WS_OVERLAPPEDWINDOW),		_exStyle (0),						_className (className),				_x (CW_USEDEFAULT),					_y (0),								_width (CW_USEDEFAULT),				_height (0),						_hWndParent (0),					_hMenu (0),							_data (0),							_hInst (hInst)					{}HWND WinMaker::Create (char const * title ){HWND hwnd = ::CreateWindowEx (_exStyle,_className,title,_style,_x,_y,_width,_height,_hWndParent,_hMenu,_hInst,_data);if (hwnd == 0)   throw "Window Creation Failed";	return hwnd;}/*----------Class Window-------------------*/class Window{public:	Window (HWND h);	void Display (int cmdShow);private:	HWND _h;};Window::Window (HWND h = 0)		: _h (h) {}void Window::Display (int cmdShow)	{		::ShowWindow (_h, cmdShow);		::UpdateWindow (_h);	}LRESULT CALLBACK WinProcedure (HWND hwnd,						   UINT message,						   WPARAM wParam,						   LPARAM lParam){	switch (message)	{	case WM_DESTROY:			::PostQuitMessage (0);			return 0;	}	return ::DefWindowProc (hwnd, message, wParam, lParam);}/*----------------And then there was WinMain----------*/int WINAPI WinMain (HINSTANCE hInst,					HINSTANCE hPrevInst,					LPSTR cmdParam,        int    cmdShow){	char className[] = "Winnie";	WinClassMaker winClass (WinProcedure, className, hInst);	winClass.Register();	WinMaker maker (className, hInst);	Window win = maker.Create ("Hello Windows!");	win.Display (cmdShow);	// Message Loop	MSG msg;	int status;	while ((status = ::GetMessage (& msg, 0, 0, 0)) != 0)	{		if(status == -1)			return -1;		::DispatchMessage (& msg);	}	return msg.wParam;}  


Well that''s it -- its only supposed to display a simple window but provide the wrapper for the windows API, I think it hangs up on WinClassMaker::Register.
"Education is when you read the fine print; experience is what you get when you don't." -Pete Seegerwww.lucid-edge.net
You will need to add a static window procedure. Actually, depending on whether you want this to be a generic window wrapper or a Win32 application wrapper, you might want to make this a singleton.
Singleton - just for one window for your application (you will need to add a static boolean or something to control class instantiations, and most likely declaring WinMain a friend, etc).
Generic - forget about encapsulating the WndProc among other things and declare a WNDCLASSEX variable in file scope or in WinMain.

WinMain and WndProc (or whatever) do not have class-level access to data members and functions in your class, thus the need to either make those functions static members or friends of the class. Blah blah
I'm sure there is much more, but I need to go to the can. Good luck

[edited by - dviator on July 3, 2002 2:37:50 AM]
Ignore the above posts. Your WinProcedure() function is fine.

Your problem lies in that you have not initialised all the members of your WNDCLASSEX structure. Simply pop:


  memset(&_class, 0, sizeof(WNDCLASSEX));  


at the top of your WinClassMaker constructor, before you start filling in the data members. I tested your code and it worked fine with that addition.
Advertisement
Hey,
Hey thanks!
That worked -- I''ve been pounding my head against a brick for days trying to figure that our -- and you spotted it.
If I worked with you or knew you I would buy you lunch-- or a beer or both -- shit!!!!
Thanks a lot!

Luke

ps. if you got a second -- tell can you explain in a little more detail why I needed memset() cause I''m not entirely clear on that -- though it did work like a charm!
"Education is when you read the fine print; experience is what you get when you don't." -Pete Seegerwww.lucid-edge.net
Memset is used, basically to make sure that there isn''t any ''trash data'' in your variable. See, when you say something like

int someNum;

the compiler tells the computer to allocate memory for that variable. Thing is, when it does that, it doesn''t check to see if that memory space is empty or it''s just marked as unsused. So there''s a chance that there will actually be data in there. The statment memset(&_class, 0, sizeof(WINCLASSEX)); tells the program to make sure that there isn''t valid data in there. It sets everthing to 0 (which is boolean false). It''s always a good habit to do this to every variable that you declare if there is ANY chance that it could be used before it''s initialized.

Not that sark couldn''t have written this answer himself but...
Time is one of life''s best teachers; unfortunately, he kills most of his students.Kvorak
I understand that. I also used ZeroMemory(); which worked fine. I suppose that this isn''t portable though. It is strickly a windows-based function. Of course, all in all the memset() also adds the functionality of providing an initialization, zero, false, in this case for the required memory, while ZeroMemory already has that build in. I suppose the question becomes whether or not to remain portable?

Luke

thanks for you help,


/*****************************
No one has control -- control is just a fantasy. And being human is difficult.
****************************/
"Education is when you read the fine print; experience is what you get when you don't." -Pete Seegerwww.lucid-edge.net
I believe ZeroMemory() is just a macro, it looks something like this:

#define ZeroMemory(var,size) memset(var,0,size)

So you''ll only be able to use ZeroMemory while you''re using the Win32 headers. It''s not like it''s difficult to define in your own programs.

Superpig
- saving pigs from untimely fates
- sleeps in a ham-mock at www.thebinaryrefinery.cjb.net

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

This topic is closed to new replies.

Advertisement