Click here to Skip to main content
15,881,089 members
Articles / Desktop Programming / WPF

ScanX - A Registry Cleaner

Rate me:
Please Sign up or sign in to vote.
4.95/5 (92 votes)
29 Jan 2012CPOL11 min read 170.4K   12.5K   208  
C#/WPF - ScanX: Creating a commercial quality Registry cleaner.
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows.Media;
using RegScanHarness.Helpers.Util;

namespace RegScanHarness.Helpers.CircularProgressBar
{
 /// <summary>
  /// A pie piece shape
  /// </summary>
  public class PiePiece : Canvas
  {
    #region events

    public delegate void PiePieceClicked(PiePiece sender);

    public event PiePieceClicked PiePieceClickedEvent;

    #endregion

    /// <summary>
    /// The radius of this pie piece
    /// </summary>
    public double Radius
    {
      get { return (double)GetValue(RadiusProperty); }
      set { SetValue(RadiusProperty, value); }
    }

    public static readonly DependencyProperty RadiusProperty =
       DependencyProperty.Register("Radius", typeof(double), typeof(PiePiece), new PropertyMetadata(OnDependencyPropertyChanged));

    /// <summary>
    /// The fill color of this pie piece
    /// </summary>
    public Brush Fill
    {
      get { return (Brush)GetValue(FillProperty); }
      set { SetValue(FillProperty, value); }
    }

    public static readonly DependencyProperty FillProperty =
       DependencyProperty.Register("Fill", typeof(Brush), typeof(PiePiece), new PropertyMetadata(OnDependencyPropertyChanged));

    /// <summary>
    /// The fill color of this pie piece
    /// </summary>
    public Brush Stroke
    {
      get { return (Brush)GetValue(StrokeProperty); }
      set { SetValue(StrokeProperty, value); }
    }

    public static readonly DependencyProperty StrokeProperty =
       DependencyProperty.Register("Stroke", typeof(Brush), typeof(PiePiece), new PropertyMetadata(OnDependencyPropertyChanged));

    /// <summary>
    /// The inner radius of this pie piece
    /// </summary>
    public double InnerRadius
    {
      get { return (double)GetValue(InnerRadiusProperty); }
      set { SetValue(InnerRadiusProperty, value); }
    }

    public static readonly DependencyProperty InnerRadiusProperty =
       DependencyProperty.Register("InnerRadius", typeof(double), typeof(PiePiece), new PropertyMetadata(OnDependencyPropertyChanged));

    /// <summary>
    /// The wedge angle of this pie piece in degrees
    /// </summary>
    public double WedgeAngle
    {
      get { return (double)GetValue(WedgeAngleProperty); }
      set { SetValue(WedgeAngleProperty, value); }
    }

    public static readonly DependencyProperty WedgeAngleProperty =
       DependencyProperty.Register("WedgeAngle", typeof(double), typeof(PiePiece), new PropertyMetadata(OnDependencyPropertyChanged));


    /// <summary>
    /// The rotation, in degrees, from the Y axis vector of this pie piece.
    /// </summary>
    public double RotationAngle
    {
      get { return (double)GetValue(RotationAngleProperty); }
      set { SetValue(RotationAngleProperty, value); }
    }

    public static readonly DependencyProperty RotationAngleProperty =
       DependencyProperty.Register("RotationAngle", typeof(double), typeof(PiePiece), new PropertyMetadata(OnDependencyPropertyChanged));

    /// <summary>
    /// The Y coordinate of centre of the circle from which this pie piece is cut.
    /// </summary>
    public double CentreY
    {
      get { return (double)GetValue(CentreYProperty); }
      set { SetValue(CentreYProperty, value); }
    }

    public static readonly DependencyProperty CentreYProperty =
       DependencyProperty.Register("CentreY", typeof(double), typeof(PiePiece), new PropertyMetadata(OnDependencyPropertyChanged));

    /// <summary>
    /// The Y coordinate of centre of the circle from which this pie piece is cut.
    /// </summary>
    public double CentreX
    {
      get { return (double)GetValue(CentreXProperty); }
      set { SetValue(CentreXProperty, value); }
    }

    public static readonly DependencyProperty CentreXProperty =
       DependencyProperty.Register("CentreX", typeof(double), typeof(PiePiece), new PropertyMetadata(OnDependencyPropertyChanged));


    /// <summary>
    /// The value that this pie piece represents.
    /// </summary>
    public double PieceValue { get; set; }

    private static void OnDependencyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
      PiePiece source = d as PiePiece;
      source.AddPathToCanvas();
    }


    public PiePiece()
    {
      AddPathToCanvas();
      this.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(PiePiece_MouseLeftButtonUp);
    }

    /// <summary>
    /// Handle left mouse button click event, hit testing to see if the pie piece was hit.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void PiePiece_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
      /*IEnumerable<UIElement> hits = VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(null), this);

      Path path = (Path)hits.First(element => element is Path);
      if (path != null)
      {
        if (PiePieceClickedEvent != null)
        {
          PiePieceClickedEvent(this);
        }
      }*/
    }

    private void AddPathToCanvas()
    {
      this.Children.Clear();

      String tooltip = (WedgeAngle / 360.00).ToString("#0.##%");

      Path path = ConstructPath();
      ToolTipService.SetToolTip(path, tooltip);
      this.Children.Add(path);
    }

    /// <summary>
    /// Constructs a path that represents this pie segment
    /// </summary>
    /// <returns></returns>
    private Path ConstructPath()
    {
      if (WedgeAngle >= 360)
      {
        Path path = new Path()
        {
          Fill = this.Fill,
          Stroke = this.Stroke,
          StrokeThickness = 1,
          Data = new GeometryGroup()
          {
            FillRule = System.Windows.Media.FillRule.EvenOdd,
            Children = new GeometryCollection()
            {
              new EllipseGeometry()
              {
                Center = new Point(CentreX, CentreY),
                RadiusX = Radius,
                RadiusY = Radius
              },
              new EllipseGeometry()
              {
                Center = new Point(CentreX, CentreY),
                RadiusX = InnerRadius,
                RadiusY = InnerRadius
              }
            },
           
          }
        };
        return path;
      }

      

      Point startPoint = new Point(CentreX, CentreY);

      Point innerArcStartPoint = Utils.ComputeCartesianCoordinate(RotationAngle, InnerRadius);
      Point innerArcEndPoint = Utils.ComputeCartesianCoordinate(RotationAngle + WedgeAngle, InnerRadius);
      Point outerArcStartPoint = Utils.ComputeCartesianCoordinate(RotationAngle, Radius);
      Point outerArcEndPoint = Utils.ComputeCartesianCoordinate(RotationAngle + WedgeAngle, Radius);
      innerArcStartPoint.Offset(CentreX, CentreY);
      innerArcEndPoint.Offset(CentreX, CentreY);
      outerArcStartPoint.Offset(CentreX, CentreY);
      outerArcEndPoint.Offset(CentreX, CentreY);

      bool largeArc = WedgeAngle > 180.0;      
      Size outerArcSize = new Size(Radius, Radius);
      Size innerArcSize = new Size(InnerRadius, InnerRadius);

      PathFigure figure = new PathFigure()
      {
        StartPoint = innerArcStartPoint,
        Segments = new PathSegmentCollection()
              {
                  new LineSegment()
                  {
                      Point = outerArcStartPoint
                  },
                  new ArcSegment()
                  {
                      Point = outerArcEndPoint,
                      Size = outerArcSize,
                      IsLargeArc = largeArc,
                      SweepDirection = SweepDirection.Clockwise,
                      RotationAngle = 0
                  },
                  new LineSegment()
                  {
                      Point = innerArcEndPoint
                  },
                  new ArcSegment()
                  {
                      Point = innerArcStartPoint,
                      Size = innerArcSize,
                      IsLargeArc = largeArc,
                      SweepDirection = SweepDirection.Counterclockwise,
                      RotationAngle = 0
                  }
              }
      };      
      
      return new Path()
      {
        Fill = this.Fill,
        Stroke = this.Stroke,
        StrokeThickness = 1,
        Data = new PathGeometry()
        {
          Figures = new PathFigureCollection()
                    {
                        figure
                    }
        }
      };
    }


  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Network Administrator vtdev.com
Canada Canada
Network and programming specialist. Started in C, and have learned about 14 languages since then. Cisco programmer, and lately writing a lot of C# and WPF code, (learning Java too). If I can dream it up, I can probably put it to code. My software company, (VTDev), is on the verge of releasing a couple of very cool things.. keep you posted.

Comments and Discussions