GDI - hwnd and OOP

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 24
Reputation: GadiK is an unknown quantity at this point 
Solved Threads: 0
GadiK GadiK is offline Offline
Newbie Poster

GDI - hwnd and OOP

 
0
  #1
Feb 21st, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,440
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1473
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: GDI - hwnd and OOP

 
0
  #2
Feb 21st, 2009
pass it as a parameter to DrawingARect() and to the drawing functions in your class.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 24
Reputation: GadiK is an unknown quantity at this point 
Solved Threads: 0
GadiK GadiK is offline Offline
Newbie Poster

Re: GDI - hwnd and OOP

 
0
  #3
Feb 22nd, 2009
Originally Posted by Ancient Dragon View Post
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() ?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,440
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1473
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: GDI - hwnd and OOP

 
0
  #4
Feb 22nd, 2009
You might use a pure virtual base class and a vector of classes. Something like this might work.

  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();
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC