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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
public partial class Sector : UserControl { public Sector() { InitializeComponent();
this.SizeChanged += Sector_SizeChanged; }
private Geometry? ClipTemp { get; set; }
public double StartAngle { get { return (double)GetValue(StartAngleProperty); } set { SetValue(StartAngleProperty, value); } } public static readonly DependencyProperty StartAngleProperty = DependencyProperty.Register("StartAngle", typeof(double), typeof(Sector), new PropertyMetadata(90d, OnStartAngleCallback));
private static void OnStartAngleCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is Sector sector) { SetSector(sector); } }
public double SectorAngle { get { return (double)GetValue(SectorAngleProperty); } set { SetValue(SectorAngleProperty, value); } } public static readonly DependencyProperty SectorAngleProperty = DependencyProperty.Register("SectorAngle", typeof(double), typeof(Sector), new PropertyMetadata(90d, OnSectorAngleCallback));
private static void OnSectorAngleCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is Sector sector) { if (sector.SectorAngle >= 360) { sector.ClipTemp = sector.path.Clip; sector.path.Clip = null; } else { if (sector.path.Clip == null) { sector.path.Clip = sector.ClipTemp; } }
SetSector(sector); } }
public double RotationAngle { get { return (double)GetValue(RotationAngleProperty); } set { SetValue(RotationAngleProperty, value); } } public static readonly DependencyProperty RotationAngleProperty = DependencyProperty.Register("RotationAngle", typeof(double), typeof(Sector), new PropertyMetadata(0d, OnRotationAngleCallback));
private static void OnRotationAngleCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is Sector sector) { SetSector(sector); } }
private void Sector_SizeChanged(object sender, SizeChangedEventArgs e) { var vSize = e.NewSize;
ellipse.Center = new Point(vSize.Width / 2, vSize.Height / 2); ellipse.RadiusX = vSize.Width / 2; ellipse.RadiusY = vSize.Height / 2;
figure.StartPoint = new Point(vSize.Width / 2, vSize.Height / 2);
arc.Size = new Size(vSize.Width / 2, vSize.Height / 2); line2.Point = new Point(vSize.Width / 2, vSize.Height / 2);
SetSector(this); }
private static void SetSector(Sector sector) { double leftAngle = sector.RotationAngle - sector.SectorAngle / 2;
double rightAngle = sector.RotationAngle + sector.SectorAngle / 2;
double centerX = sector.Width / 2; double centerY = sector.Height / 2; double radius = sector.Width / 2; double startAngle = sector.StartAngle; sector.line1.Point = CalcCirclePoint(centerX, centerY, radius, startAngle, leftAngle); sector.arc.Point = CalcCirclePoint(centerX, centerY, radius, startAngle, rightAngle); }
private static Point CalcCirclePoint(double centerX, double centerY, double radius, double startAngle, double angle) { double angleRadians = (angle - startAngle) * Math.PI / 180; double x = centerX + radius * Math.Cos(angleRadians); double y = centerY + radius * Math.Sin(angleRadians); return new Point(x, y); } }
|