如何绘制圆角矩形具有特定范围内可变宽度的边界 [英] How to draw rounded rectangle with variable width border inside of specific bounds

查看:125
本文介绍了如何绘制圆角矩形具有特定范围内可变宽度的边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绘制一个圆角矩形边框的方法。边框可以是任何的宽度,所以我遇到的问题是边界延伸超过指定的界限时,因为它是从路径中心绘制的厚。



如何将包括边框的宽度,以便它在给定的范围完全吻合?



下面是我用来绘制圆角矩形的代码。

 私人无效DrawRoundedRectangle(图形GFX,矩形范围,诠释CornerRadius,笔DrawPen,颜色填充颜色)
{
GraphicsPath的gfxPath =新的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(新SolidBrush(填充颜色),gfxPath);
gfx.DrawPath(DrawPen,gfxPath);
}


解决方案

好的球员,我想它出来了!只需要缩小范围,考虑到钢笔的宽度。我有点知道这是否有借鉴的路径内部的线一种方法,我只是想知道答案。这工作虽然不错。

 私人无效DrawRoundedRectangle(图形GFX,矩形范围,诠释CornerRadius,笔DrawPen,颜色填充颜色)
{
INT strokeOffset = Convert.ToInt32(Math.Ceiling(DrawPen.Width));
边界= Rectangle.Inflate(边界,-strokeOffset,-strokeOffset);

DrawPen.EndCap = DrawPen.StartCap = LineCap.Round;

GraphicsPath的gfxPath =新的GraphicsPath();
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(新SolidBrush(填充颜色),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);
}

解决方案

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.

private void DrawRoundedRectangle(Graphics gfx, Rectangle Bounds, int CornerRadius, Pen DrawPen, Color FillColor)
{
    int strokeOffset = Convert.ToInt32(Math.Ceiling(DrawPen.Width));
    Bounds = Rectangle.Inflate(Bounds, -strokeOffset, -strokeOffset);

    DrawPen.EndCap = DrawPen.StartCap = LineCap.Round;

    GraphicsPath gfxPath = new GraphicsPath();
    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);
}

这篇关于如何绘制圆角矩形具有特定范围内可变宽度的边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆