Remove ellipse from canvas

I am trying to draw ellipses on a canvas on left mouseclick, which must be removed on right mouseclick. Below is the essence of my code. This code is removing the RECT rcTarget[] but not the Ellipse. Should I try to place the ellipse on the RECT? How to do that? Or should I follow a complete other approach?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
static RECT rcTarget[6];

LRESULT CALLBACK ChildWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {

case WM_LBUTTONDOWN:  {
rcTarget[n].left = X[n];
rcTarget[n].top = Y[n];
rcTarget[n].right = X[n]+12;
rcTarget[n].bottom = Y[n]+12;
Ellipse(hdc2[n], rcTarget[n].left, rcTarget[n].top, rcTarget[n].right, rcTarget[n].bottom);
}

case WM_RBUTTONDOWN:  {
InvalidateRect(hwnd, &rcTarget[i], TRUE);
UpdateWindow(hwnd);

}
}
Or should I follow a complete other approach?

My question first: what does your application on "unhiding" your canvas? (An other window was in the foreground of your canvas, partially or completely, and now it is shown again -- how acts your program in this situation?)

Another approach? Yes, keep it simple and so on... I would redraw the complete graphic on an empty canvas, just without the elllipse in question. For sure you have a log of all elements shown, use it for the redraw.
This is a fairly common homework assignment. The purpose is to have you maintain a list of graphical primitives that are displayed on the canvas every redraw (WM_PAINT).

LMB is easy: add a primative at the clicked coordinate.

RMB takes more work: you need to identify the primative you clicked on. Do this by running through your list and choosing the topmost item that covers the point where you clicked. None of this is graphical; you do it all with your internal list and the x,y position of the click.

If you can identify an item in the list, remove it and invalidate the window so that WM_PAINT gets sent to your apps message loop again.

Hope this helps.
I am thinking of doing it like this:
RECT lprcUpdate = { 10, 10, 500, 500 };
HRGN hrgnUpdate;
UINT flags;
DeleteObject(hdcx[i]);
RedrawWindow(hwnd, &lprcUpdate, hrgnUpdate, flags);

Is this the right code for in the WM_RBUTTONDOWN case?
Yes, that looks fine. Make sure you properly find the object in your list by comparing the click coordinates and the object coordinates. Remember, you are supposed to be maintaining the list yourself, in addition to the object handle for Win GDI.
Topic archived. No new replies allowed.