Assistance needed - resolved

Hello, I am making a paint tool and so far so good. I have been able to make it draw lines, ellipses, and rectangles all based on mouse position. The only problem I am having now is when I resize the window, everything drawn on the canvas disappears. From what I understand, I should store the shapes I've drawn in a vector with their respective positions, and then somehow redraw them whenever the window has been resized. I've been struggling with this problem for couple of hours now and I can't seem to be able to do it on my own.

Here's the Canvas.h I've been given with the assignment:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class CCanvas
{
public:
	CCanvas();
	~CCanvas();
	bool Initialise(HWND hwnd, int Width, int Height);
	BackBuffer* GetBackBuffer();
	bool Draw(HDC _hdc);
	void Save(HWND _hwnd);
	void AddShape(Shape*);

	void SetStartX(int StartX);
	void SetEndX(int EndX);
	void SetStartY(int StartX);
	void SetEndY(int StartX);

private:
	BackBuffer* BackBuffer; // A canvas has a backbuffer.
	vector<Shape*> shapes;
	POINT Start, End;
};


And here is where I stored my drawing code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
case WM_LBUTTONUP:
		hdc = GetDC(hwnd);

		if (isDrawingLine)
		{
			End.x = static_cast<int>LOWORD(lParam);
			End.y = static_cast<int>HIWORD(lParam);

			aLine.SetStartX(Start.x);
			aLine.SetStartY(Start.y);
			aLine.SetEndX(End.x);
			aLine.SetEndY(End.y);
			
			aLine.Draw(hdc);
		
			ReleaseDC(hwnd, hdc);
		}
		if (isDrawingBox)
		{
			End.x = static_cast<int>LOWORD(lParam);
			End.y = static_cast<int>HIWORD(lParam);

			myRec.SetStartX(Start.x);
			myRec.SetStartY(Start.y);
			myRec.SetEndX(End.x);
			myRec.SetEndY(End.y);

			myRec.Draw(hdc);
		
			ReleaseDC(hwnd, hdc);
		}
		if (isDrawEllipse)
		{
			End.x = static_cast<int>LOWORD(lParam);
			End.y = static_cast<int>HIWORD(lParam);

			myEllipse.SetStartX(Start.x);
			myEllipse.SetStartY(Start.y);
			myEllipse.SetEndX(End.x);
			myEllipse.SetEndY(End.y);
			myEllipse.Draw(hdc);

			ReleaseDC(hwnd, hdc);
		}


So basically I am storing the starting point of each shape in the WM_LBUTTONDOWN case, tracking the ending point in the WM_MOUSEMOVE case, and then finally drawing it in the BUTTONUP case (which I know this isn't the place to draw it but I don't know where else I should do it!). I guess my question is, how can I possibly store the shapes along with their coordinates and re-draw them whenever the window has been resized?
Last edited on
Please help anyone...?
You shouldn't draw in the mouse event (like WM_LBUTTONUP) anyway...

In WM_LBUTTONUP you do something like

myCanvas.AddShape(new aLine(...));

provided that aLine, myRec, etc. inherite from Shape.

After you called AddShape(...) you call the InvalidateRect(...) function. See:

https://msdn.microsoft.com/en-us/library/windows/desktop/dd145002(v=vs.85).aspx

Within the WM_PAINT event you call the Draw(...) function of your myCanvas which draws all shapes.

It looks like your CCanvas is exactly designed for that...
Topic archived. No new replies allowed.