日期:2014-05-18 浏览次数:20963 次
public class Pie : Shape { protected override System.Windows.Media.Geometry DefiningGeometry { get { StreamGeometry geometry = new StreamGeometry(); geometry.FillRule = FillRule.EvenOdd; using (StreamGeometryContext context = geometry.Open()) { DrawGeometry(context); } geometry.Freeze(); return geometry; } } private void DrawGeometry(StreamGeometryContext context) { var startPoint = new Point(CenterX, CenterY); var endPoint = GetEndPoint(); context.BeginFigure(startPoint, true, true); context.LineTo(new Point(CenterX, CenterY - Radius), true, true); context.ArcTo(endPoint, new Size(Radius, Radius), 0, Angle > 180, SweepDirection.Clockwise, true, true); } private Point GetEndPoint() { Point result = new Point(); double a = Angle / 180.0f * Math.PI; result.X = CenterX + Radius * Math.Sin(a); result.Y = CenterY - Radius * Math.Cos(a); return result; } protected override void OnRender(DrawingContext dc) { //dc.DrawRectangle(Brushes.Yellow, null, new Rect(RenderSize)); base.OnRender(dc); } public double Radius { get { return (double)GetValue(RadiusProperty); } set { SetValue(RadiusProperty, value); } } public static readonly DependencyProperty RadiusProperty = DependencyProperty.Register("Radius", typeof(double), typeof(Pie), new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure)); public double Angle { get { return (double)GetValue(AngleProperty); } set { SetValue(AngleProperty, value); } } public static readonly DependencyProperty AngleProperty = DependencyProperty.Register("Angle", typeof(double), typeof(Pie), new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure)); public double CenterX { get { return (double)GetValue(CenterXProperty); } se