自学内容网 自学内容网

wpf 环形进度条,自定义控件

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static System.Net.WebRequestMethods;

namespace DiaControlLibiray
{
    /// <summary>
    /// CircleProgress.xaml 的交互逻辑
    /// </summary>
    public partial class CircleProgress : UserControl
    {
        public int cycleWidth
        {
            get { return (int)GetValue(cycleWidthProperty); }
            set { SetValue(cycleWidthProperty, value); }
        }

        // Using a DependencyProperty as the backing store for cycleWidth.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty cycleWidthProperty =
            DependencyProperty.Register("cycleWidth", typeof(int), typeof(CircleProgress), new PropertyMetadata(300, new PropertyChangedCallback(OnPropertyChanged)));

        public System.Drawing. Color cycleColor
        {
            get { return (System.Drawing.Color)GetValue(cycleColorProperty); }
            set { SetValue(cycleColorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for cycleColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty cycleColorProperty =
            DependencyProperty.Register("cycleColor", typeof(System.Drawing.Color), typeof(CircleProgress), new PropertyMetadata(System.Drawing.Color.Blue   ,new PropertyChangedCallback(OnPropertyChanged)));


        public int cycleHeight
        {
            get { return (int)GetValue(cycleHeightProperty); }
            set { SetValue(cycleHeightProperty, value); }
        }

        // Using a DependencyProperty as the backing store for cycleHeight.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty cycleHeightProperty =
            DependencyProperty.Register("cycleHeight", typeof(int), typeof(CircleProgress), new PropertyMetadata(300, new PropertyChangedCallback(OnPropertyChanged)));


        public int Percent
        {
            get { return (int)GetValue(PercentProperty); }
            set { SetValue(PercentProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Percent.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PercentProperty =
            DependencyProperty.Register("Percent", typeof(int), typeof(CircleProgress), new PropertyMetadata(60, new PropertyChangedCallback(OnPropertyChanged)));
        public CircleProgress()
        {
            InitializeComponent();
            //cycleProgressPath.Fill = new SolidColorBrush(System.Windows.Media.Color.FromRgb(cycleColor.R,cycleColor.G,cycleColor.B));
            //cycleProgressPath.Stroke = new SolidColorBrush(Colors.Red);
            Update();
        }
        public static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as CircleProgress).Update();
        }
        public void Update()
        {
            double Angle = Percent * 3.6 * Math.PI / 180;
            txtPercent.Text = Percent + "%";
            string data = "M{0} {1} A{2} {2} 0 0 1 {3} {4} ";
            if (Percent > 50) data = "M{0} {1} A{2} {2} 0 1 1 {3} {4} ";
            if (Percent >= 100)
            {

                data = "M{0} {1} A{2} {2} 0 1 1 {3} {4} Z";
                Angle = 270;
            }

            double endpointX = Math.Round(cycleWidth / 2 + Math.Sin(Angle) * cycleWidth / 2, 2);
            double endpointY = 0;
            //if(Percent<90)
            endpointY = Math.Round(cycleHeight / 2 - Math.Cos(Angle) * cycleHeight / 2, 2);
            //if(Percent>90&&Percent<180)
            //    endpointY= Math.Round(cycleHeight / 2+Math.Sin(Angle) *(cycleHeight/2),2);
            data = string.Format(data, cycleWidth / 2, 0, cycleWidth / 2, endpointX, endpointY);
            cycleProgressPath.Data = Geometry.Parse(data);
            http:// hi.csdn.net/attachment/201201/0_1325414042qvFL.gif & pos_id = c8oIyUjE
        }

    }
}

 
  <Path Stroke="#419f8b"   
        x:Name="cycleProgressPath"
        VerticalAlignment="Center"
        HorizontalAlignment="Center"
       
        Width="310"
        Height="310"
        StrokeThickness="10"
        Data="">
      
  </Path>
  <TextBlock  x:Name="txtPercent" Text="60%" FontSize="33" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>


原文地址:https://blog.csdn.net/sunjay117/article/details/140693849

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!