C++ – Filling a Partially Rounded Rectangle with GDI+

cdrawinggdi+mfc

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?

Best Answer

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.

Related Topic