C++ GDI+ how to draw rectangle with border radius(C++GDI+如何绘制带边框半径的矩形)
本文介绍了C++GDI+如何绘制带边框半径的矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这就是我使用GDI+绘制矩形的方式。
Graphics g(hdc);
SolidBrush blueColor((Color(255, 74, 134, 232)));
g.FillRectangle(&blueColor, x, y, width, height);
DeleteObject(&blueColor);
现在我想添加一些border-radius
,如何才能做到这一点?
推荐答案
您可以通过GraphicsPath::AddArc及相关接口实现该功能。
一些为我工作的代码:
void GetRoundRectPath(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 DrawRoundRect(Graphics* pGraphics, Rect r, Color color, int radius, int width)
{
int dia = 2*radius;
// set to pixel mode
int oldPageUnit = pGraphics->SetPageUnit(UnitPixel);
// define the pen
Pen pen(color, 1);
pen.SetAlignment(PenAlignmentCenter);
// get the corner path
GraphicsPath path;
// get path
GetRoundRectPath(&path, r, dia);
// draw the round rect
pGraphics->DrawPath(&pen, &path);
// if width > 1
for(int i=1; i<width; i++)
{
// left stroke
r.Inflate(-1, 0);
// get the path
GetRoundRectPath(&path, r, dia);
// draw the round rect
pGraphics->DrawPath(&pen, &path);
// up stroke
r.Inflate(0, -1);
// get the path
GetRoundRectPath(&path, r, dia);
// draw the round rect
pGraphics->DrawPath(&pen, &path);
}
// restore page unit
pGraphics->SetPageUnit((Unit)oldPageUnit);
}
更多引用:RoundRect,WINAPI Edit control with custom border
这篇关于C++GDI+如何绘制带边框半径的矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:C++GDI+如何绘制带边框半径的矩形


基础教程推荐
猜你喜欢
- C++,'if' 表达式中的变量声明 2021-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31