suti
suti

Reputation: 181

Draw Shapes in WinAPI C++

I'm studying Drawing Shapes by WinAPI C++ I tried to draw 2 ellipse with some codes on WM_PAINT:

PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
HPEN hPen = CreatePen(PS_DASHDOTDOT, 2, NULL);SelectObject(hdc, hPen);
Ellipse(hdc, 100, 200, 400, 400);
Ellipse(hdc, 300, 300, 500, 510);   

DeleteObject(hPen);
EndPaint(hWnd, &ps);

But the output is:

enter image description here The result I expect is neither shapes is front of the other. And the border is dash dot dot. Can anyone show me my mistake? I appreciate for your help.

Upvotes: 4

Views: 5087

Answers (2)

Adrian McCarthy
Adrian McCarthy

Reputation: 48038

One appears on top of the other because you're not just drawing the outline, but filling it as well. To stop filling it, you can select a "hollow brush", sometimes called a "null brush".

HBRUSH hbrOld = SelectObject(hdc, GetStockObject(HOLLOW_BRUSH));
// draw your ellipses here

You can only create dotted or dashed pens with widths of 1 or 0. You used 2, so the command failed.

Also, you should select the pen back out of the DC before you delete it.

Upvotes: 3

HerrJoebob
HerrJoebob

Reputation: 2313

Ellipse() (like all GDI shape functions) fills the shape using the current brush, which is why your output looks like that. For details on that see setting pen and brush colors.

If you want just the ellipse with no fill, first select a null brush:

SelectObject( hdc, GetStockObject( NULL_BRUSH ) );

Upvotes: 4

Related Questions