943,976 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 953
  • C++ RSS
Feb 21st, 2009
0

GDI - hwnd and OOP

Expand Post »
Hi Guys,
I'm trying to write a simple game of drawing rectangles and circles in a window. I've created a different class for each shape: RectClass and CircClass. Each of these classes has it own DrawShape() function which suppose to draw the shapes in the window with given coordinates argument.
As far as I can tell drawing with GDI is accomplished using the InvalidateRect() function which sends the WM_PAINT message. And when handling the message, depend on some global parameter, you call a specific drawing function. Something like this:

//global parameters
#define DRAW_RECT 1
#define DRAW_CIRC 2

int DrawMode;

//drawing functions of each class - dispatch the WM_PAINT nessage
RectClass::DrawShape(RECT rct)
{
	DrawMode = DRAW_RECT;
	InvalidateRect(hwnd, &rct, TRUE);
}

CircClass::DrawShape(RECT rct)
{
	DrawMode = DRAW_CIRC;
	InvalidateRect(hwnd, &rct, TRUE);
}

//in the main file
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
	
	switch (message)
	{
		.
		.
		.
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		switch (DrawMode)
		{
		case DRAW_RECT:
			DrawingARect(hdc, RectClassObj.getCoordinates());
			break;
		case DRAW_CIRC:
			DrawingACircle(hdc, CircClassObj.getCoordinates());
			break;
		}
		EndPaint(hWnd, &ps);
	break;
		.
		.
		.
	}
}

However if I want to implement my idea like this, I need to use the hwnd handler within the class member functions (as shown in the code above). But the handler is created in the main program file and I do not have access to it in my classes.

Is there another way to draw using GDI with OOP?
Is there a way to get the HWND handle into the classes? I couldn't find a place in the main program to copy a HWND handle to the classes.

Thank you in advance,
GadiK
Reputation Points: 18
Solved Threads: 0
Light Poster
GadiK is offline Offline
32 posts
since Dec 2008
Feb 21st, 2009
0

Re: GDI - hwnd and OOP

pass it as a parameter to DrawingARect() and to the drawing functions in your class.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Feb 22nd, 2009
0

Re: GDI - hwnd and OOP

pass it as a parameter to DrawingARect() and to the drawing functions in your class.
Of course!! How silly of me.
But I guess what really troubles me is the use of the global parameter DrawMode to distinguish between rectangles an circles. I think what I proposed is not good object oriented programming. I was actually hoping that someone would show me a different way to draw from within the class member functions. A way where each object could take control of the window and draw in it. Maybe there's some other command besides InvalidateRect() ?
Reputation Points: 18
Solved Threads: 0
Light Poster
GadiK is offline Offline
32 posts
since Dec 2008
Feb 22nd, 2009
0

Re: GDI - hwnd and OOP

You might use a pure virtual base class and a vector of classes. Something like this might work.

C++ Syntax (Toggle Plain Text)
  1. class DrawingObject
  2. {
  3. ...
  4. virtual void Draw() = 0; // implemented by subclass
  5. };
  6.  
  7. class Circle : public DrawingObject
  8. {
  9. ...
  10. virtual Draw() ; // implenentation of base class
  11. };
  12.  
  13. class Rectangle : public DrawingObject
  14. {
  15. ...
  16. virtual Draw() ; // implenentation of base class
  17. };
  18.  
  19. // creation of this vector is not shown here
  20. vector<DrawingObject> theList;
  21.  
  22. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  23. {
  24. int wmId, wmEvent;
  25. PAINTSTRUCT ps;
  26. HDC hdc;
  27.  
  28. switch (message)
  29. {
  30. .
  31. .
  32. .
  33. case WM_PAINT:
  34. hdc = BeginPaint(hWnd, &ps);
  35. for(int i = 0; i < theList.size(); i++)
  36. theList[i].Draw();
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: please help me with this
Next Thread in C++ Forum Timeline: Help Request writing my first Do While





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC