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

pass it as a parameter to DrawingARect() and to the drawing functions in your class.

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() ?

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

class DrawingObject
{
...
    virtual void Draw() = 0; // implemented by subclass
};

class Circle : public DrawingObject
{
...
    virtual Draw() ; // implenentation of base class
};

class Rectangle : public DrawingObject
{
...
    virtual Draw() ; // implenentation of base class
};

// creation of this vector is not shown here
vector<DrawingObject> theList;

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);
            for(int i = 0; i < theList.size(); i++)
                  theList[i].Draw();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.