2015-11-07 93 views
0

我试图做一个定制的PictureBox看起来像这样 -绘制图片框C#

enter image description here

到目前为止,我所做的是 - enter image description here

使用此代码 -

protected void UpdateRegion() 
    { 
     var path = new GraphicsPath(); 
     Point[] points = 
     { 
      new Point(0, 0), 
      new Point(0, ClientSize.Height-80), 
      new Point(80 , ClientSize.Height), 
      new Point(ClientSize.Width-80, ClientSize.Height), 
      new Point(ClientSize.Width, ClientSize.Height-80), 
      new Point(ClientSize.Width , 0) 
     }; 
     path.AddPolygon(points); 
     path.FillMode = FillMode.Winding; 
     this.Region = new Region(path); 
    } 
+2

您将需要添加一个弧。 – TaW

+0

@TaW,我该如何使用它? – DeaDViruS

+1

查看我的答案的例子。我已经清理了一下你的代码。 – TaW

回答

3

在这里你去:

enter image description here

 GraphicsPath path = new GraphicsPath(); 
     path.FillMode = FillMode.Winding; 

     int cut = 80; 
     Rectangle cr = panel1.ClientRectangle; 

     Point[] points = 
     { 
      new Point(0, cr.Height - cut), 
      new Point(0, 0), 
      new Point(cr.Width, 0), 
      new Point(cr.Width, cr.Height - cut), 
      new Point(cr.Width - cut, cr.Height), 
      new Point(cut, cr.Height), 
      new Point(0, cr.Height - cut), 
     }; 
     path.AddPolygon(points); 

     Rectangle arcRect = new Rectangle(0, cr.Height - 2 * cut, 2 * cut, 2 * cut); 
     path.AddArc(arcRect, 90f, 90f); 

的电弧由边界矩形,这在我们的情况下,具有切口的大小的两倍限定。它从x轴开始顺时针旋转90°,并且(至少)90°以上。

您可以add it to a GraphicsPathdraw it with a Graphics对象。

下面是从MSDN报价:

如果有图中的前一个直线或曲线,直线加 到前一段的端点连接到的 弧开始。

沿着由指定矩形包围的椭圆的周长来追踪圆弧。圆弧的起点由 决定,从椭圆的x轴(在0度 角度)处顺时针测量开始角度的度数。端点 类似地通过从起点开始顺时针测量 扫描角度中的度数来定位。如果扫描角度是大于360度或小于-360度的 ,则电弧分别被精确地360度或-360度扫过 。

我已经添加了弧的边界矩形,仅供演示使用。该代码不包括它。

对于其他角落的圆角切割,您需要更改&展开点数组并添加更多/其他弧。

其它角弧采取这些矩形:

Rectangle arcRectTL = new Rectangle(0, 0, 2 * cut, 2 * cut); 
Rectangle arcRectTR = new Rectangle(cr.Width - 2 * cut, 0, 2 * cut, 2 * cut); 
Rectangle arcRectBR = new Rectangle(cr.Width - 2*cut, cr.Height - 2*cut, 2*cut, 2*cut); 

起始角度分别为:180°, 270° and 0°

尺寸和清扫角度保持不变。

+0

这个代码也可以工作,如果我需要在左上角有一个曲线,右边的按钮和其他边缘是正常的。 – DeaDViruS

+0

当然,只需添加更多的切口和更多的弧线! – TaW

+0

谢谢,我会研究更多关于如何使用弧的方法。 – DeaDViruS