如何在特定边界内绘制具有可变宽度边框的圆角矩形
我有一个方法可以绘制带边框的圆角矩形。 边框可以是任何宽度,所以我遇到的问题是,当边框很厚时,它会延伸超过给定的边界,因为它是从路径的中心绘制的。
我如何包含边框的宽度,使其完全适合给定的边界?
这是我用来绘制圆角矩形的代码。
private void DrawRoundedRectangle(Graphics gfx, Rectangle Bounds, int CornerRadius, Pen DrawPen, Color FillColor)
{
GraphicsPath gfxPath = new GraphicsPath();
DrawPen.EndCap = DrawPen.StartCap = LineCap.Round;
gfxPath.AddArc(Bounds.X, Bounds.Y, CornerRadius, CornerRadius, 180, 90);
gfxPath.AddArc(Bounds.X + Bounds.Width - CornerRadius, Bounds.Y, CornerRadius, CornerRadius, 270, 90);
gfxPath.AddArc(Bounds.X + Bounds.Width - CornerRadius, Bounds.Y + Bounds.Height - CornerRadius, CornerRadius, CornerRadius, 0, 90);
gfxPath.AddArc(Bounds.X, Bounds.Y + Bounds.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90);
gfxPath.CloseAllFigures();
gfx.FillPath(new SolidBrush(FillColor), gfxPath);
gfx.DrawPath(DrawPen, gfxPath);
}
I have a method that draws a rounded rectangle with a border. The border can be any width, so the problem I'm having is the border is extending past the given bounds when it's thick because it's drawn from the center of a path.
How would I include the width of the border so that it fits perfectly in the given bounds?
Here's the code I'm using to draw the rounded rectangle.
private void DrawRoundedRectangle(Graphics gfx, Rectangle Bounds, int CornerRadius, Pen DrawPen, Color FillColor)
{
GraphicsPath gfxPath = new GraphicsPath();
DrawPen.EndCap = DrawPen.StartCap = LineCap.Round;
gfxPath.AddArc(Bounds.X, Bounds.Y, CornerRadius, CornerRadius, 180, 90);
gfxPath.AddArc(Bounds.X + Bounds.Width - CornerRadius, Bounds.Y, CornerRadius, CornerRadius, 270, 90);
gfxPath.AddArc(Bounds.X + Bounds.Width - CornerRadius, Bounds.Y + Bounds.Height - CornerRadius, CornerRadius, CornerRadius, 0, 90);
gfxPath.AddArc(Bounds.X, Bounds.Y + Bounds.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90);
gfxPath.CloseAllFigures();
gfx.FillPath(new SolidBrush(FillColor), gfxPath);
gfx.DrawPath(DrawPen, gfxPath);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,伙计们,我明白了! 只需缩小边界以考虑笔的宽度即可。 我有点知道这就是答案,我只是想知道是否有办法在路径内部画一条线。 不过这效果很好。
Alright guys, I figured it out! Just need to shrink the bounds to take into account the width of the pen. I kind of knew this was the answer I was just wondering if there was a way to draw a line on the inside of a path. This works good though.