Windowing again....

Started by
17 comments, last by ANSI2000 21 years, 10 months ago
Ok so my ObjectFactory will have a private memeber

Object *object;

object = new Object();

handle = CreateWindowEx(...., object);

so when do I pass the handle to the object? And what about properties x, y, width, height, caption etc...

I guess I can give teh object it''s properties when I instantiate it... And once CreateWindowEx has returned a handle call a setHandle method from the object?
Advertisement
Ok got it working now. Here is what I have so far...


    namespace G3DGUI{	class Object	{	public:		Object() {};		~Object() {};		void setProperties(HWND pHandle, int pX, int pY, int pWidth, int pHeight, char *pCaption, char *pMenu = NULL)		{			handle = pHandle;			x = pX;			y = pY;			width = pWidth;			height = pHeight;			strcpy(caption, pCaption);			//strcpy(menu, pMenu);		}		//virtual LRESULT winProc(UINT Msg, WPARAM wParam, LPARAM lParam) = 0;		LRESULT winProc(UINT Msg, WPARAM wParam, LPARAM lParam)		{			switch (Msg)			{						case WM_CREATE:				break;						case WM_DESTROY:				PostQuitMessage(0);				break;						default:				return(DefWindowProc(handle, Msg, wParam, lParam));			}			return 0;				}		void update() { UpdateWindow(handle); }		void show()	{ ShowWindow(handle, SW_SHOW); }		void hide()	{ ShowWindow(handle, SW_HIDE); }		void focus() { SetFocus(handle); }		void destroy() { DestroyWindow(handle); }	private:		HWND handle;		int x, y, width, height;		unsigned int style, styleEx;		char caption[128];		char menu[128];	};	class ObjectFactory	{	public:		ObjectFactory(HINSTANCE pInstance)		{			instance = pInstance;		}		~ObjectFactory() {};		Object *createObject(HWND pParent, char *pClassName, int pX, int pY, int pWidth, int pHeight, unsigned int pStyle, unsigned int pStyleEx, char *pCaption, char *pMenu = NULL)		{			windowClass.lpfnWndProc = defaultProc;			windowClass.hInstance = instance;			windowClass.lpszClassName = pClassName;			windowClass.cbSize = sizeof(WNDCLASSEX);			windowClass.cbClsExtra = 0;			windowClass.cbWndExtra = 0;			windowClass.style = 0;			windowClass.hCursor = LoadCursor(0, IDC_ARROW);			windowClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);			windowClass.hIcon = LoadIcon(0, IDI_APPLICATION);			windowClass.hIconSm = LoadIcon(0, IDI_APPLICATION);			windowClass.lpszMenuName = pMenu;			registerClass();						object = new Object();						HWND hWnd;						hWnd = CreateWindowEx(pStyleEx, pClassName, pCaption, pStyle,								  pX, pY, pWidth, pHeight, pParent,								  NULL, instance, (LPSTR)object);						object->setProperties(hWnd, pX, pY, pWidth, pHeight, pCaption);						object->update();			object->show();						return(object);		}		static LRESULT CALLBACK defaultProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)		{			Object *thisObject = (Object*)GetWindowLong(hWnd, GWL_USERDATA);				if(!thisObject)			{				if(Msg == WM_CREATE)				{					LPCREATESTRUCT lpcs = (LPCREATESTRUCT)lParam;					thisObject = (Object*)lpcs->lpCreateParams;					SetWindowLong(hWnd, GWL_USERDATA, (LONG)thisObject);										return(thisObject->winProc(Msg, wParam, lParam));				}				else					return(DefWindowProc(hWnd, Msg, wParam, lParam)); // should never be called			}			else			{				return(thisObject->winProc(Msg, wParam, lParam));			}		}	protected:		bool registerClass()		{			if(!RegisterClassEx(&windowClass))				return(false);			else				return(true);		}			private:		HINSTANCE instance;		WNDCLASSEX windowClass;		Object *object;	};}  


I dont like this section of code though...

object = new Object();

HWND hWnd;

hWnd = CreateWindowEx(styleEx, className, caption, style, x, y, width, height, parent, NULL, instance, (LPSTR)object);

object->setProperties(hWnd, x, y, width, height, caption);

object->update();
object->show();

return(object);

Next I will abstarct the Object, ObjectFactory classes. Also any other improvements to make?

[edited by - ANSI2000 on June 10, 2002 2:52:37 PM]
I started thinking of subclassing...

The only problem I see here is... The point of a factory is to create instances of different kinds of objects. The problem arise when you abstract the object to overide the winProc...

Well this can be fixed by createing specialized Factories, but like I think isn''t a factory suposed to make different kinds of "Objects"?
That''s sort of why I was hesitant to call it a factory. Because the class responsible for creating gui objects is a mold of sorts, with its WNDCLASS determining the type of objects it creates, it really isn''t capable of creating various types of objects.

If you desire a factory pattern, such a factory would probably contain such "molds" for use when instantiating various types of objects. But each WindowMaker mold would only be responsible for creating one type using its encapsulated WNDCLASS, which includes the WndProc.

"Don''t be afraid to dream, for out of such fragile things come miracles."
So what I have so far is it ok? Or do I need to do more? Change stuff add stuff?

I dont like this to much :/ It will require to subclass every time you want to make a different "object"

[edited by - ANSI2000 on June 10, 2002 4:48:24 PM]
I''m not too crazy about it either, which is why I stay away from it.

As for subclassing, it''s not always necessary. For style differences, you simply give a basic WindowMaker class methods which are used to enable or change particular style properties for the windows it creates. The only time you would really have to subclass is when you need fundamentally different message handlers, (hence a change in your WndProc as well).

"Don''t be afraid to dream, for out of such fragile things come miracles."
So as for things to add, I''d simply include a simple style setting interface for your WindowMaker. For inspiration when creating this, take a look at that code I gave you a while back (which you hopefully still have).

Plus, I wouldn''t have your basic Object post a quit message when it''s destroyed. You probably want to reserve that for a MainWindow class.

"Don''t be afraid to dream, for out of such fragile things come miracles."
I dont think I still have that...

Do you still have it? If so e-mail it to me at voodoo@videotron.ca

Actually I rember liking that code much better then this pattern we have now...

As for teh PostQuit, I put that there for the hell of it... It''s just a proc I copied and pasted from another app...
Email is sent.

"Don''t be afraid to dream, for out of such fragile things come miracles."

This topic is closed to new replies.

Advertisement