tags:

views:

221

answers:

1

I have a rounded rectangle that I make like so

dc.RoundRect(textBorder, CPoint(20, 20));

Later on I draw a line through it about 1/3 of the way down.

dc.LineTo(textBorder.right, textBorder.top + 15);

Now I would like to fill just the part above the line with a solid color. In other words I need to fill a partially rounded rectangle, because the top of the rectangle is rounded, but the bottom of it is truncated by the line. Is there an easy way to do this?

+1  A: 

Have you tried using a combination of CreateRoundRectRegion and then FillRgn to fill the non-rectangular area?

This the example given in the docs for CreateRoundRectRegion:

CRgn   rgnA, rgnB, rgnC;

VERIFY(rgnA.CreateRoundRectRgn( 50, 50, 150, 150, 30, 30 ));
VERIFY(rgnB.CreateRoundRectRgn( 200, 75, 250, 125, 50, 50 ));
VERIFY(rgnC.CreateRectRgn( 0, 0, 50, 50 ));

int nCombineResult = rgnC.CombineRgn( &rgnA, &rgnB, RGN_OR );
ASSERT( nCombineResult != ERROR && nCombineResult != NULLREGION );

CBrush brA, brB, brC;
VERIFY(brA.CreateSolidBrush( RGB(255, 0, 0) ));  
VERIFY(pDC->FillRgn( &rgnA, &brA));      // rgnA Red Filled

VERIFY(brB.CreateSolidBrush( RGB(0, 255, 0) ));  
VERIFY(pDC->FillRgn( &rgnB, &brB));      // rgnB Green Filled
VERIFY(brC.CreateSolidBrush( RGB(0, 0, 255) ));  // rgnC Blue
VERIFY(pDC->FrameRgn( &rgnC, &brC, 2, 2 ));

In general, when you want to do something with non-rectangular areas you have to start looking into regions.

Karim
I haven't tried that but I don't see how that fixes my problem. Won't that create rounded edges on the bottom of the region?
FranticPedantic
Now that you have edited it I see there is a CombineRgn function. That might help.
FranticPedantic
I think this is the right idea, but the code is a little confusing. What you want to do is create a region that is the intersection of a regular rectangle with your rounded one.
Mark Ransom
Yeah, this code is directly from the documentation and is not meant to be a direct solution for the problem you're having. Just a step on the path to getting you there.
Karim