[img]https:/ /i.sstatic.net/rEAT0uuk.png[/img]
Приведенный ниже код создает выходные данные «Только линия» и «Только заливка», но как я могу получить что-то, что выглядит ближе к ожидаемому изображению на крайний правый? (создано вручную в MS Paint)
Код: Выделить всё
void GetRoundRectPath2(GraphicsPath* pPath, Rect r, int dia)
{
// diameter can't exceed width or height
if (dia > r.Width) dia = r.Width;
if (dia > r.Height) dia = r.Height;
// define a corner
Rect Corner(r.X, r.Y, dia, dia);
// begin path
pPath->Reset();
// top left
pPath->AddArc(Corner, 180, 90);
// tweak needed for radius of 10 (dia of 20)
if (dia == 20)
{
Corner.Width += 1;
Corner.Height += 1;
r.Width -= 1; r.Height -= 1;
}
// top right
Corner.X += (r.Width - dia - 1);
pPath->AddArc(Corner, 270, 90);
// bottom right
Corner.Y += (r.Height - dia - 1);
pPath->AddArc(Corner, 0, 90);
// bottom left
Corner.X -= (r.Width - dia - 1);
pPath->AddArc(Corner, 90, 90);
// end path
pPath->CloseFigure();
}
void FillRoundRect2(Graphics* pGraphics, Brush* pBrush, Rect r, Color border, int radius)
{
int dia = 2 * radius;
// set to pixel mode
int oldPageUnit = pGraphics->SetPageUnit(UnitPixel);
// define the pen
Pen pen(border, 1);
pen.SetAlignment(PenAlignmentCenter);
// get the corner path
GraphicsPath path;
// get path
GetRoundRectPath2(&path, r, dia);
// fill
pGraphics->FillPath(pBrush, &path);
// draw the border last so it will be on top
pGraphics->DrawPath(&pen, &path);
// restore page unit
pGraphics->SetPageUnit((Unit)oldPageUnit);
}
Graphics graphics(dc);
graphics.SetSmoothingMode(SmoothingModeAntiAlias);
// Line only
SolidBrush sbrA(Gdiplus::Color(0,255,0,0)); // Full Transparent
FillRoundRect2(&graphics, &sbrA, Gdiplus::Rect(10, 10, 40, 40), Gdiplus::Color(50, 255, 0, 0), 5);
// Fill only
SolidBrush sbr(Gdiplus::Color(50, 255, 0, 0));
FillRoundRect2(&graphics, &sbr, Gdiplus::Rect(10, 10, 40, 40), Gdiplus::Color(0, 255, 0, 0), 5);
Я нашел подход, который работает довольно хорошо. Сначала я создаю область, размер которой точно соответствует прямоугольнику с закругленными углами, который я хочу нарисовать с помощью SetClip. Затем я сместил координаты, переданные в FillRoundRect2, слева и сверху, на -1 и увеличил ширину и высоту на 2. Это означает, что края с псевдонимами теперь выходят за пределы области рисования.
Код: Выделить всё
Region myRegion(Rect(10, 10, 40, 40));
graphics.SetClip(&myRegion, CombineModeReplace);
SolidBrush sbr(Gdiplus::Color(50, 255, 0, 0));
FillRoundRect2(&graphics, &sbr, Gdiplus::Rect(10-1, 10-1, 40+2, 40+2), Gdiplus::Color(0, 255, 0, 0), 7);

Подробнее здесь: https://stackoverflow.com/questions/785 ... ased-using