WPF 自定义标尺控件

直接上代码

  1  public class Ruler : System.Windows.Controls.Control
  2     {
  3         public static readonly DependencyProperty StartValueProperty = DependencyProperty.Register("StartValue", typeof(double), typeof(Ruler), new UIPropertyMetadata(120.0));
  4 
  5         public double StartValue
  6         {
  7             get { return (double)GetValue(StartValueProperty); }
  8 
  9             set { SetValue(StartValueProperty, value); }
 10         }
 11 
 12         public static readonly DependencyProperty EndValueProperty = DependencyProperty.Register("EndValue", typeof(double), typeof(Ruler), new UIPropertyMetadata(240.0));
 13 
 14         public double EndValue
 15         {
 16             get { return (double)GetValue(EndValueProperty); }
 17 
 18             set { SetValue(EndValueProperty, value); }
 19         }
 20 
 21         /// <summary>
 22         /// 当前值
 23         /// </summary>
 24         public static readonly DependencyProperty CurrentValueProperty = DependencyProperty.Register("CurrentValue", typeof(double), typeof(Ruler), new UIPropertyMetadata(180.0));
 25 
 26         public double CurrentValue
 27         {
 28             get { return (double)GetValue(CurrentValueProperty); }
 29 
 30             set
 31             {
 32                 SetValue(CurrentValueProperty, value);
 33                 PaintPath();
 34             }
 35         }
 36 
 37         /// <summary>
 38         /// 步长
 39         /// </summary>
 40         public static readonly DependencyProperty IntervalProperty = DependencyProperty.Register("Interval", typeof(double), typeof(Ruler), new UIPropertyMetadata(30.0));
 41 
 42         public double Interval
 43         {
 44             get { return (double)GetValue(IntervalProperty); }
 45 
 46             set { SetValue(IntervalProperty, value); }
 47         }
 48 
 49         /// <summary>
 50         /// 间隔步长
 51         /// </summary>
 52         public static readonly DependencyProperty SpanIntervalProperty = DependencyProperty.Register("SpanInterval", typeof(double), typeof(Ruler), new UIPropertyMetadata(5.0));
 53 
 54         public double SpanInterval
 55         {
 56             get { return (double)GetValue(SpanIntervalProperty); }
 57 
 58             set { SetValue(SpanIntervalProperty, value); }
 59         }
 60 
 61         /// <summary>
 62         /// 中线标记
 63         /// </summary>
 64         public static readonly DependencyProperty MiddleMaskProperty = DependencyProperty.Register("MiddleMask", typeof(int), typeof(Ruler), new UIPropertyMetadata(2));
 65 
 66         public int MiddleMask
 67         {
 68             get { return (int)GetValue(MiddleMaskProperty); }
 69 
 70             set { SetValue(MiddleMaskProperty, value); }
 71         }
 72 
 73         /// <summary>
 74         /// 当前值的图形坐标点
 75         /// </summary>
 76         public static readonly DependencyProperty CurrentGeometryProperty = DependencyProperty.Register("CurrentGeometry", typeof(Geometry), typeof(Ruler),new PropertyMetadata(Geometry.Parse("M 257,0 257,25 264,49 250,49 257,25")));
 77 
 78         public Geometry CurrentGeometry
 79         {
 80             get { return (Geometry)GetValue(CurrentGeometryProperty); }
 81 
 82             set { SetValue(CurrentGeometryProperty, value); }
 83         }
 84          
 85         /// <summary>
 86         /// 构造函数
 87         /// </summary>
 88         static Ruler()
 89         {
 90             DefaultStyleKeyProperty.OverrideMetadata(typeof(Ruler), new FrameworkPropertyMetadata(typeof(Ruler)));
 91         }
 92 
 93         public override void OnApplyTemplate()
 94         {
 95             base.OnApplyTemplate();
 96 
 97             PaintPath();
 98         }
 99 
100         protected override void OnRender(DrawingContext drawingContext)
101         {
102             RenderOptions.SetEdgeMode(this, EdgeMode.Aliased);
103 
104             double nextLineValue = 0d;
105 
106             //计算出每格子占用宽度多少 
107 
108             var one_Width = ActualWidth / ((EndValue - StartValue) / Interval);
109 
110             for (int i = 0; i <= ((EndValue - StartValue) / Interval); i++) //需要画多少个大格子
111             {
112                 var numberText = new FormattedText((StartValue + (i * Interval)).ToString(), CultureInfo.CurrentUICulture, FlowDirection.LeftToRight, new Typeface("Source Han Sans CN"), 10, Brushes.White); //当前数值
113 
114                 drawingContext.DrawText(numberText, new Point(i * one_Width - 8, 0));//每个大格子的数值和坐标 -8是为了居中
115 
116                 drawingContext.DrawLine(new Pen(new SolidColorBrush(Colors.White), 1), new Point(i * one_Width, 25), new Point(i * one_Width, ActualHeight - 2));
117 
118                 Console.WriteLine($"{(StartValue + (i * Interval))}的坐标" + (i * one_Width).ToString());
119 
120                 var cnt = Interval / SpanInterval;
121 
122                 for (int j = 1; j <= cnt; j++) //每个大格子中间有多少个小格子
123                 {
124                     Console.WriteLine($"{(j * (one_Width / cnt) + nextLineValue)}的坐标" + ((one_Width / cnt) + nextLineValue).ToString());
125 
126                     if (j % MiddleMask == 0)//每第二个格子的线加长
127                     {
128                         drawingContext.DrawLine(new Pen(new SolidColorBrush(Colors.White), 1), new Point(j * (one_Width / cnt) + nextLineValue, ActualHeight - 2), new Point(j * (one_Width / cnt) + nextLineValue, ActualHeight - 10));
129                     }
130                     else
131                     {
132                         drawingContext.DrawLine(new Pen(new SolidColorBrush(Colors.White), 1), new Point(j * (one_Width / cnt) + nextLineValue, ActualHeight - 2), new Point(j * (one_Width / cnt) + nextLineValue, ActualHeight - 5));
133                     }
134                 }
135 
136                 nextLineValue = i * one_Width;
137             }
138 
139             // PaintCurrentValue(drawingContext);
140         }
141 
142         /// <summary>
143         /// 画当前值 静态
144         /// </summary>
145         /// <param name="drawingContext"></param>
146         //private void PaintCurrentValue(DrawingContext drawingContext)
147         //{
148 
149         //    //计算当前值的横坐标
150         //    var d_Value = CurrentValue - 120d;
151 
152         //    //每5个数值的占用宽度为21
153         //    var one_Value = 460d / 120d;
154 
155         //    var x_Point = one_Value * d_Value + 8d;
156 
157         //    //画当前值的三角形 
158 
159         //    drawingContext.DrawGeometry(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ED538A")),
160         //        new Pen(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ED538A")), 1),
161         //         Geometry.Parse($"M {x_Point},0 {x_Point},25 {x_Point + 7},49 {x_Point - 7},49 {x_Point},25"));
162         //}
163 
164         /// <summary>
165         /// 动态计算当前值图形坐标点
166         /// </summary>
167         private void PaintPath()
168         {
169             //计算当前值的横坐标
170             var d_Value = CurrentValue - 120d;
171 
172             //每5个数值的占用宽度为21
173             var one_Value = ActualWidth / (EndValue - StartValue);
174 
175             double x_Point = one_Value * d_Value + (((double)Parent.GetValue(ActualWidthProperty) - ActualWidth) / 2d); //这里要加上父容器-当前容器宽度的一半
176 
177             //画当前值的三角形 
178             CurrentGeometry = Geometry.Parse($"M {x_Point},0 {x_Point},25 {x_Point + 7},49 {x_Point - 7},49 {x_Point},25");
179         }
180     }

 

效果

 

 

 前端代码

<Grid Background="#58607B" Width="514" Height="51" Margin="0,10,0,0">
    <Path x:Name="Layout_Controls_Path" Stroke="#ED538A" StrokeThickness="1" Fill="#ED538A" Data="{Binding ElementName=Layout_Controls_Ruler, Path=CurrentGeometry,Mode=TwoWay}" />
    <control:Ruler x:Name="Layout_Controls_Ruler" Width="489" HorizontalAlignment="Center"/>
</Grid>

本文原创出处:http://www.cnblogs.com/SFlyers/

欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利!

 

posted @ 2021-01-22 14:52  SFlyers  阅读(759)  评论(3编辑  收藏  举报