0

我正在创建一个带有四舍五入的选择性角的按钮。在网上搜索后,我发现了一些片段可以将按钮的所有角都修圆。但我只需要修整按钮的一两个角。这就是我希望我的按钮看起来的样子。

在此处输入图像描述

所以,在这里我希望按钮 A 的左上角是圆形的,B 的右上角,C 的左下角和 D 的右下角。

这是我发现的用于修整按钮所有角的代码。

Dim p As New Drawing2D.GraphicsPath 
p.StartFigure() 
p.AddArc(New Rectangle(0, 0, 40, 40), 180, 90) 
p.AddLine(40, 0, Button1.Width - 40, 0) 
p.AddArc(New Rectangle(Button1.Width - 40, 0, 40, 40), -90, 90) 
p.AddLine(Button1.Width, 40, Button1.Width, Button1.Height - 40) 
p.AddArc(New Rectangle(Button1.Width - 40, Button1.Height - 40, 40, 40), 0, 90) 
p.AddLine(Button1.Width - 40, Button1.Height, 40, Button1.Height) 
p.AddArc(New Rectangle(0, Button1.Height - 40, 40, 40), 90, 90) 
p.CloseFigure() 
Button1.Region = New Region(p)

问题是我对图形不太了解,所以我不能调整它来只修整一个特定的角落。您能否帮助我理解绘图逻辑或调整此代码段以获得所需的结果。

4

1 回答 1

3

尝试这个:

Public Sub RoundCorners(ByVal leftTop As Boolean, ByVal rightTop As Boolean, ByVal rightBottom As Boolean, ByVal leftBottom As Boolean)
  Dim p As New Drawing2D.GraphicsPath 
  p.StartFigure() 
  p.AddArc(New Rectangle(0, 0, IIF(leftTop,40,1), IIF(leftTop,40,1)), 180, 90) 
  p.AddArc(New Rectangle(Button1.Width - IIF(rightTop,40,1), 0, IIF(rightTop,40,1), IIF(rightTop,40,1)), -90, 90) 
  p.AddArc(New Rectangle(Button1.Width - IIF(rightBottom,40,1), Button1.Height - IIF(rightBottom,40,1), IIF(rightBottom,40,1), IIF(rightBottom,40,1)), 0, 90) 
  p.AddArc(New Rectangle(0, Button1.Height - IIF(leftBottom,40,1), IIF(leftBottom,40,1), IIF(leftBottom,40,1)), 90, 90) 
  p.CloseFigure() 
  Button1.Region = New Region(p)
End Sub

用法

RoundCorners(true,true,true,true) //Round all corners
RoundCorners(true,false,false,false) //Round left-top corner

更新

RoundCorners如果我们以这种方式实现该方法,那就更好了:

Public Sub RoundCorners(ByVal leftTop As Int32, ByVal rightTop As Int32, ByVal rightBottom As Int32, ByVal leftBottom As Int32)
  If leftTop <= 0 Then leftTop = 1
  If rightTop <= 0 Then rightTop = 1
  If rightBottom <= 0 Then rightBottom = 1
  If leftBottom <= 0 Then leftBottom = 1
  Dim p As New Drawing2D.GraphicsPath 
  p.StartFigure() 
  p.AddArc(New Rectangle(0, 0, leftTop, leftTop), 180, 90) 
  p.AddArc(New Rectangle(Button1.Width - rightTop, 0, rightTop, rightTop), -90, 90) 
  p.AddArc(New Rectangle(Button1.Width - rightBottom, Button1.Height - rightBottom, rightBottom, rightBottom), 0, 90) 
  p.AddArc(New Rectangle(0, Button1.Height - leftBottom, leftBottom, leftBottom), 90, 90) 
  p.CloseFigure() 
  Button1.Region = New Region(p)
End Sub

用法

RoundCorners(40,40,40,40)   //Round all corners
RoundCorners(40,0,0,0)      //Round left-top corners
于 2013-10-01T07:19:31.767 回答