【问题标题】:How to make one side rounded button in c#?如何在c#中制作一侧圆形按钮?
【发布时间】:2024-09-01 02:30:01
【问题描述】:

我找到了代码:

GraphicsPath GetRoundPath(RectangleF Rect, int radius, float width=0)

    {
        //Fix radius to rect size
        radius = (int) Math.Max(( Math.Min(radius, Math.Min(Rect.Width, Rect.Height)) - width),1);
        float r2 = radius / 2f;
        float w2 = width / 2f;            
        GraphicsPath GraphPath = new GraphicsPath();

        //Top-Left Arc
        GraphPath.AddArc(Rect.X + w2, Rect.Y + w2, radius, radius, 180, 90);

        //Top-Right Arc
        GraphPath.AddArc(Rect.X + Rect.Width - radius - w2, Rect.Y + w2, radius, radius, 270, 90);

        //Bottom-Right Arc
        GraphPath.AddArc(Rect.X + Rect.Width - w2 - radius,
                           Rect.Y + Rect.Height - w2 - radius, radius, radius, 0, 90);
        //Bottom-Left Arc
        GraphPath.AddArc(Rect.X + w2, Rect.Y - w2 + Rect.Height - radius, radius, radius, 90, 90);

        //Close line ( Left)           
        GraphPath.AddLine(Rect.X + w2, Rect.Y + Rect.Height - r2 - w2, Rect.X + w2,Rect.Y + r2 + w2);
                   
        //GraphPath.CloseFigure();            
        
        return GraphPath;
    }

谁能解释一下我应该如何改变它以使按钮只有一侧圆形?

【问题讨论】:

  • 你能给我们看一个你想要的示例输出按钮吗?
  • 你要舍入哪一边?
  • 这是我从这种“问题”中得到的:我找到了一个可以满足我需求的代码。我不明白我找到的代码。请改变它让我做我想做的事。抱歉,SO 不是免费的代码编写服务。我们在这里帮助其他开发人员解决问题。你没有问题,你有一份你想免费完成的工作(当然,一份很小的工作,但仍然是一份工作)。
  • ibb.co/wr2H01h 我需要创建这样的按钮。
  • 我只需要解释一下,如果你使用addarc函数,如何留下正常的角落。

标签: c# button


【解决方案1】:

经过一些研究,我设法为未来的用户制作了它。 这是仅右下角圆角的代码:

 radius = (int)Math.Max((Math.Min(radius, Math.Min(Rect.Width, Rect.Height)) - width), 1);
            float r2 = radius / 2f;
            float w2 = width / 2f;
            GraphicsPath GraphPath = new GraphicsPath();

        GraphPath.AddLine(Rect.X + w2, Rect.Y + w2, Rect.X, Rect.Y);
        GraphPath.AddLine(Rect.X + Rect.Width, Rect.Y, Rect.X + Rect.Width - w2, Rect.Y);
        GraphPath.AddArc(Rect.X + Rect.Width - w2 - radius,
                           Rect.Y + Rect.Height - w2 - radius, radius, radius, 0, 90);
        GraphPath.AddLine(Rect.X + w2, Rect.Y - w2 + Rect.Height, Rect.X - w2, radius);

        GraphPath.AddLine(Rect.X + w2, Rect.Y + Rect.Height + r2 + w2 + Rect.Y, Rect.X + w2, Rect.Y + Rect.Y);

        return GraphPath;

左上角

GraphPath.AddLine(Rect.X + w2, Rect.Y + w2, Rect.X, Rect.Y);
GraphPath.AddArc(Rect.X + Rect.Width - radius - w2, Rect.Y + w2, radius, radius, 270, 90);
GraphPath.AddLine(Rect.X + Rect.Width-w2 ,Rect.Y + Rect.Height, Rect.X+Rect.Width,Rect.Y+Rect.Height-w2);
GraphPath.AddLine(Rect.X + w2, Rect.Y - w2 + Rect.Height, Rect.X - w2, radius);
GraphPath.AddLine(Rect.X + w2, Rect.Y + Rect.Height + r2 + w2 + Rect.Y, Rect.X + w2, Rect.Y + Rect.Y);  
return GraphPath;

右上角

GraphPath.AddArc(Rect.X + w2, Rect.Y + w2, radius, radius, 180, 90);
GraphPath.AddLine(Rect.X + Rect.Width, Rect.Y, Rect.X + Rect.Width - w2, Rect.Y);
GraphPath.AddLine(Rect.X + Rect.Width - w2, Rect.Y + Rect.Height, Rect.X + Rect.Width, Rect.Y + Rect.Height - w2);
GraphPath.AddLine(Rect.X + w2, Rect.Y - w2 + Rect.Height, Rect.X - w2, radius);
GraphPath.AddLine(Rect.X + w2, Rect.Y + Rect.Height - r2 - w2, Rect.X + w2, Rect.Y + r2 + w2);
return GraphPath;

【讨论】:

    最近更新 更多