Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用WinForms(.NET)绘制圆角矩形?_C#_.net_Winforms - Fatal编程技术网

C# 如何使用WinForms(.NET)绘制圆角矩形?

C# 如何使用WinForms(.NET)绘制圆角矩形?,c#,.net,winforms,C#,.net,Winforms,使用C#绘制矩形,我需要在每个边上绘制圆弧首先我绘制矩形,然后我需要单击按钮,它将在边上绘制圆弧,我该怎么做?首先绘制四条线,然后在四个角上绘制圆弧。绘制一个圆角矩形 尝试: C#中的graphics类没有用于绘制圆角矩形的内置方法,但是有几种方法可以实现这一效果。答案中的链接提供了关于从何处开始的良好建议,此外,我建议您查看本文: 首先,我们创建一个GraphicsPath, 然后我们调用StartFigure,这样 我们可以开始向路径添加边。 此代码的其余部分用于顶部 左角和顶部的线 圆角

使用C#绘制矩形,我需要在每个边上绘制圆弧首先我绘制矩形,然后我需要单击按钮,它将在边上绘制圆弧,我该怎么做?

首先绘制四条线,然后在四个角上绘制圆弧。

绘制一个圆角矩形

尝试:


C#中的graphics类没有用于绘制圆角矩形的内置方法,但是有几种方法可以实现这一效果。答案中的链接提供了关于从何处开始的良好建议,此外,我建议您查看本文:

首先,我们创建一个GraphicsPath, 然后我们调用StartFigure,这样 我们可以开始向路径添加边。 此代码的其余部分用于顶部 左角和顶部的线 圆角矩形。如果我们应该 为了使这个角变圆,我们添加了一个 弧-否则


以上所有内容都适用于绘图,但如果您想将图形路径转换为自定义控件的区域,我认为您应该使用CreateRoundRectRgn函数(来自gdi32)为右上角、左下角和右下角边绘制适当的曲线(左上角边已根据半径正确绘制)。快速浏览一下(instanceofTom的答案中的站点)

我知道这篇文章很老了,但在搜索如何在C#中制作圆角矩形时,它是热门文章,我遇到了一些问题。AddArc方法是不准确的,因此,如果使用接受答案中的代码,您将得到一个时髦的圆角矩形。左上角正确,右上角和左下角变形,右下角太小。我已经对代码中的一些内容进行了调整,以补偿AddArc的不精确性,我相信我有一个有效的解决方案来创建一个合适的圆角矩形。此版本还可以将矩形分为左上半部分和右下半部分,这便于为3d效果进行明暗着色

设置窗口区域以及创建左上/右下路径以使用明暗笔进行着色跟踪的示例用法:

        Region = new Region(RoundedRectangles.RoundedRectangle.Create(new Rectangle(0, 0, Size.Width, Size.Height), 8, RoundedRectangles.RoundedRectangle.RectangleCorners.TopRight | RoundedRectangles.RoundedRectangle.RectangleCorners.TopLeft));
        TopLeftPath = RoundedRectangles.RoundedRectangle.Create(new Rectangle(0, 0, Size.Width, Size.Height), 8, RoundedRectangles.RoundedRectangle.RectangleCorners.TopRight | RoundedRectangles.RoundedRectangle.RectangleCorners.TopLeft, RoundedRectangles.RoundedRectangle.WhichHalf.TopLeft);
        BottomRightPath = RoundedRectangles.RoundedRectangle.Create(new Rectangle(0, 0, Size.Width-1, Size.Height-1), 8, RoundedRectangles.RoundedRectangle.RectangleCorners.TopRight | RoundedRectangles.RoundedRectangle.RectangleCorners.TopLeft, RoundedRectangles.RoundedRectangle.WhichHalf.BottomRight);
最后是代码:

using System;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace RoundedRectangles
{
public abstract class RoundedRectangle
{
    [Flags]
    public enum RectangleCorners
    {
        None = 0, TopLeft = 1, TopRight = 2, BottomLeft = 4, BottomRight = 8,
        All = TopLeft | TopRight | BottomLeft | BottomRight
    }

    public enum WhichHalf
    {
        TopLeft,
        BottomRight,
        Both
    }

    static void Corner(GraphicsPath path, int x1, int y1, int x2, int y2, int x3, int y3)
    {
        path.AddLine(x1, y1, x2, y2);
        path.AddLine(x2, y2, x3, y3);
    }

    public static GraphicsPath Create(int x, int y, int width, int height, int radius, RectangleCorners corners, WhichHalf half)
    {
        if (radius <= 0)
        {
            GraphicsPath rectp = new GraphicsPath();
            rectp.AddRectangle(new Rectangle(x, y, width, height));
            return rectp;
        }

        int dia = radius * 2;

        Rectangle TLarc = new Rectangle(x, y, dia, dia);
        Rectangle TRarc = new Rectangle(x + width - dia - 1, y, dia, dia);
        Rectangle BRarc = new Rectangle(x + width - dia - 1, y + height - dia - 1, dia, dia);
        Rectangle BLarc = new Rectangle(x, y + height - dia - 1, dia, dia);

        Rectangle TLsquare = new Rectangle(x, y, radius, radius);
        Rectangle TRsquare = new Rectangle(x + width - radius, y, radius, radius);
        Rectangle BRsquare = new Rectangle(x + width - radius, y + height - radius, radius, radius);
        Rectangle BLsquare = new Rectangle(x, y + height - radius, radius, radius);

        GraphicsPath p = new GraphicsPath();
        p.StartFigure();

        if (half == WhichHalf.Both || half == WhichHalf.TopLeft)
        {
            if (corners.HasFlag(RectangleCorners.BottomLeft))
                p.AddArc(BLarc, 135, 45);
            else
                p.AddLine(BLsquare.Left, BLsquare.Bottom, BLsquare.Left, BLsquare.Top);

            p.AddLine(BLsquare.Left, BLsquare.Top - 1, TLsquare.Left, TLsquare.Bottom + 1);

            if (corners.HasFlag(RectangleCorners.TopLeft))
                p.AddArc(TLarc, 180, 90);
            else
                Corner(p, TLsquare.Left, TLsquare.Bottom, TLsquare.Left, TLsquare.Top, TLsquare.Right, TLsquare.Top);

            p.AddLine(TLsquare.Right + 1, TLsquare.Top, TRsquare.Left - 1, TRsquare.Top);

            if (corners.HasFlag(RectangleCorners.TopRight))
                p.AddArc(TRarc, -90, 45);
        }

        if (half == WhichHalf.Both || half == WhichHalf.BottomRight)
        {
            if (corners.HasFlag(RectangleCorners.TopRight))
                p.AddArc(TRarc, -45, 45);
            else
                p.AddLine(TRsquare.Right, TRsquare.Top, TRsquare.Right, TRsquare.Bottom);

            p.AddLine(TRsquare.Right, TRsquare.Bottom + 1, BRsquare.Right, BRsquare.Top - 1);

            if (corners.HasFlag(RectangleCorners.BottomRight))
                p.AddArc(BRarc, 0, 90);
            else
                Corner(p, BRsquare.Right, BRsquare.Top, BRsquare.Right, BRsquare.Bottom, BRsquare.Left, BRsquare.Bottom);

            p.AddLine(BRsquare.Left - 1, BRsquare.Bottom, BLsquare.Right + 1, BLsquare.Bottom);

            if (corners.HasFlag(RectangleCorners.BottomLeft))
                p.AddArc(BLarc, 90, 45);
            else
                p.AddLine(BLsquare.Right, BLsquare.Bottom, BLsquare.Left, BLsquare.Bottom);
        }

        return p;
    }

    public static GraphicsPath Create(Rectangle rect, int radius, RectangleCorners c, WhichHalf which_half)
    { return Create(rect.X, rect.Y, rect.Width, rect.Height, radius, c, which_half); }

    public static GraphicsPath Create(Rectangle rect, int radius, RectangleCorners c)
    { return Create(rect.X, rect.Y, rect.Width, rect.Height, radius, c, WhichHalf.Both); }

    public static GraphicsPath Create(Rectangle rect, int radius)
    { return Create(rect.X, rect.Y, rect.Width, rect.Height, radius, RectangleCorners.All, WhichHalf.Both); }

}
使用系统;
使用系统图;
使用System.Drawing.Drawing2D;
命名空间圆形矩形
{
公共抽象类RoundedRectangle
{
[旗帜]
公共枚举矩形角
{
无=0,左上=1,右上=2,左下=4,右下=8,
全部=左上|右上|左下|右下
}
公共枚举
{
左上角,
右下角,
二者都
}
静态空心角(图形路径,int-x1,int-y1,int-x2,int-y2,int-x3,int-y3)
{
路径AddLine(x1,y1,x2,y2);
路径AddLine(x2,y2,x3,y3);
}
公共静态图形创建(整数x,整数y,整数宽度,整数高度,整数半径,矩形角,其中一半)
{

如果(半径使用Pen的LineJoin属性

Pen myPen = new Pen(Brushes.Black);

myPen.Width = 8.0f;

// Set the LineJoin property
myPen.LineJoin = System.Drawing.Drawing2D.LineJoin.Round;

// Draw the rectangle
e.Graphics.DrawRectangle(myPen, new Rectangle(50, 50, 200, 200));

互联网上最好的例子伟大的链接!这帮了我很大的忙。这个链接不再是活动的,这个答案应该被修改。@TomNeyland,我会检查Jay Riggs答案中的链接。我在第一个链接中使用了代码,它像一个符咒一样工作!这没有帮助。链接是404。这是上面链接的工作版本,以防其他人使用查找此页面:第一个链接(圆角矩形、字体度量…)为纯金。单击“下载源代码”可从图形库中获得一组很棒的扩展方法(例如FillRoundedRectangle和DrawRoundedRectangle).这是一篇很棒的文章,但你真正需要的只是下载中的一个.cs文件。第一个链接太棒了!正是我需要的。