2016-03-19 17 views
2

Özel döngüsel ilerleme çubuğumu WinForm'da istiyorum. Ama sonuç düşündüğüm şeye uymuyor. Şekli bu resimde aynı şekilde nasıl çizebilirim? Sorunumda net olmak için iki resim yükledim.WinForm'da GraphicsPath kullanarak dairesel bir ilerleme çubuğu nasıl çizilir?

Target shape

My code result

Benim kod, bunun:

void Form1_Paint(object sender, PaintEventArgs e) 
    { 
     int angle = 120; 

     e.Graphics.SmoothingMode = SmoothingMode.HighQuality; 

     Rectangle outerRect = new Rectangle(50, 50, 100, 100); 
     Rectangle innerRect = new Rectangle(70, 70, 60, 60); 

     int innerRadius = innerRect.Width/2; 
     int outerRadius = outerRect.Width/2; 

     Point innerCenter = new Point(innerRect.X + innerRadius, innerRect.Y + innerRadius); 
     Point outerCenter = new Point(outerRect.X + outerRadius, outerRect.Y + outerRadius); 

     GraphicsPath outerCircle = new GraphicsPath(); 
     outerCircle.AddEllipse(outerRect); 

     GraphicsPath innerCircle = new GraphicsPath(); 
     innerCircle.AddEllipse(innerRect); 

     GraphicsPath progPath = new GraphicsPath(); 

     Point p1 = new Point(outerRect.X + outerRadius, outerRect.Y); 
     Point p2 = new Point(innerRect.X + innerRadius, innerRect.Y); 


     Point inner = new Point((int)(innerRadius * Math.Cos(angle * Math.PI/180) + innerCenter.X), 
           (int)(innerRadius * Math.Sin(angle * Math.PI/180) + innerCenter.Y)); 
     Point outer = new Point((int)(outerRadius * Math.Cos(angle * Math.PI/180) + outerCenter.X), 
           (int)(outerRadius * Math.Sin(angle * Math.PI/180) + outerCenter.Y)); 

     progPath.AddLine(p1, p2); 
     progPath.AddArc(innerRect, -90, angle); 
     progPath.AddLine(inner, outer); 
     progPath.AddArc(outerRect, angle - 90,-angle); 

     progPath.Widen(Pens.Black); 
     e.Graphics.DrawPath(Pens.Black, progPath); 

    } 

cevap

4

Daha sonra, bir GraphicsPath oluşturmak AddArc yöntemi kullanarak yoluna 2 yaylar ekleyebilirsiniz:

  • Başlangıç ​​açısı 0 dış yayve tarama açısı 120 derece.
  • sonra GraphicsPath.CloseFigure kullanılarak yolunu kapatmak başlangıç ​​açısı 270 + 120 ve kıvrılma açısının -120 derece ters yönde
  • iç yay.

Bu şekilde yol olarak kalın bir arkınız olacaktır.

Yolu Graphics.FillPath yöntemini kullanarak doldurabilirsiniz. Ayrıca GraphicsPath.drawPath yöntemini kullanarak sınırları çizebilirsiniz.

Sonucu

enter image description here

Kod

private void Form1_Paint(object sender, PaintEventArgs e) 
{ 
    var g = e.Graphics; 
    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; 

    var center = new Point(100, 100); 
    var innerR = 30; 
    var thickness = 20; 
    var startAngle = 270; 
    var arcLength = 120; 
    var outerR = innerR + thickness; 
    var outerRect = new Rectangle 
        (center.X - outerR, center.Y - outerR, 2 * outerR, 2 * outerR); 
    var innerRect = new Rectangle 
        (center.X - innerR, center.Y - innerR, 2 * innerR, 2 * innerR); 

    using (var p = new GraphicsPath()) 
    { 
     p.AddArc(outerRect, startAngle, arcLength); 
     p.AddArc(innerRect, startAngle + arcLength, -arcLength); 
     p.CloseFigure(); 
     e.Graphics.FillPath(Brushes.Green, p); 
     e.Graphics.DrawPath(Pens.Black, p); 
    } 
} 
+0

Teşekkür @Reza. Bu yöntem benim yöntemimden daha basit. –

İlgili konular