领胜LDS 键盘AOI检测项目
xcd
2020-06-24 d6c577e17ee7bb5331dd51d803f9b42441b0f0e5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
 
namespace Bro.Common.ImageCanvas
{
    public class CenteredCrossHairCanvas : Canvas
    {
        static CenteredCrossHairCanvas()
        {
            IsHitTestVisibleProperty.OverrideMetadata(typeof(CenteredCrossHairCanvas), new FrameworkPropertyMetadata(false));
            BackgroundProperty.OverrideMetadata(typeof(CenteredCrossHairCanvas), new FrameworkPropertyMetadata(new SolidColorBrush(Colors.Transparent)));
        }
 
        public static readonly DependencyProperty ShowProperty =
                DependencyProperty.Register("Show", typeof(bool), typeof(CenteredCrossHairCanvas), new PropertyMetadata(true, PropertyChangedCallback));
        public bool Show { get { return (bool)GetValue(ShowProperty); } set { SetValue(ShowProperty, value); } }
 
        public static readonly DependencyProperty ScaleProperty =
                DependencyProperty.Register("Scale", typeof(double), typeof(CenteredCrossHairCanvas), new PropertyMetadata(.95, PropertyChangedCallback));
        public double Scale { get { return (double)GetValue(ScaleProperty); } set { SetValue(ScaleProperty, value); } }
 
        public static readonly DependencyProperty HorizontalLinesProperty =
                DependencyProperty.Register("HorizontalLines", typeof(int), typeof(CenteredCrossHairCanvas), new PropertyMetadata(1, PropertyChangedCallback));
        public int HorizontalLines { get { return (int)GetValue(HorizontalLinesProperty); } set { SetValue(HorizontalLinesProperty, value); } }
 
        public static readonly DependencyProperty VerticalLinesProperty =
                DependencyProperty.Register("VerticalLines", typeof(int), typeof(CenteredCrossHairCanvas), new PropertyMetadata(3, PropertyChangedCallback));
        public int VerticalLines { get { return (int)GetValue(VerticalLinesProperty); } set { SetValue(VerticalLinesProperty, value); } }
 
        public static readonly DependencyProperty StrokeBrushProperty =
            DependencyProperty.Register("StrokeBrush", typeof(Brush), typeof(CenteredCrossHairCanvas), new PropertyMetadata(new SolidColorBrush(Colors.Black), PropertyChangedCallback));
        public Brush StrokeBrush { get { return (Brush)GetValue(StrokeBrushProperty); } set { SetValue(StrokeBrushProperty, value); } }
 
        public static readonly DependencyProperty StrokeThicknessProperty =
                DependencyProperty.Register("StrokeThickness", typeof(double), typeof(CenteredCrossHairCanvas), new PropertyMetadata(1.0, PropertyChangedCallback));
        public double StrokeThickness { get { return (double)GetValue(StrokeThicknessProperty); } set { SetValue(StrokeThicknessProperty, value); } }
 
        public static readonly DependencyProperty StrokeDashStyleProperty =
                DependencyProperty.Register("StrokeDashStyle", typeof(DoubleCollection), typeof(CenteredCrossHairCanvas), new PropertyMetadata(new DoubleCollection { }, PropertyChangedCallback));
        public DoubleCollection StrokeDashStyle { get { return (DoubleCollection)GetValue(StrokeDashStyleProperty); } set { SetValue(StrokeDashStyleProperty, value); } }
 
        protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
        {
            base.OnRenderSizeChanged(sizeInfo);
            Redraw();
        }
 
        private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            (dependencyObject as CenteredCrossHairCanvas)?.Redraw();
        }
 
        private void Redraw()
        {
            Children.Clear();
            if (!Show) return;
            try
            {
                if (Math.Abs(ActualHeight) < 1 || Math.Abs(ActualWidth) < 1) return;
 
                for (var i = 1; i <= HorizontalLines; i++)
                {
                    var horizontalLine = new Line
                    {
                        Stroke = StrokeBrush,
                        StrokeDashArray = StrokeDashStyle,
                        X1 = 0,
                        X2 = ActualWidth,
                        Y1 = (ActualHeight * i) / (HorizontalLines + 1),
                        Y2 = (ActualHeight * i) / (HorizontalLines + 1),
                        StrokeThickness = StrokeThickness / Scale,
                    };
                    Children.Add(horizontalLine);
                }
                for (var i = 1; i <= VerticalLines; i++)
                {
                    var verticalLine = new Line
                    {
                        Stroke = StrokeBrush,
                        StrokeDashArray = StrokeDashStyle,
                        Y1 = 0,
                        Y2 = ActualHeight,
                        X1 = (ActualWidth * i) / (VerticalLines + 1),
                        X2 = (ActualWidth * i) / (VerticalLines + 1),
                        StrokeThickness = StrokeThickness / Scale,
                    };
                    Children.Add(verticalLine);
                }
            }
            catch { }
        }
    }
}