schangxiang@126.com
2025-11-04 f5ed29dc26c7cd952d56ec5721a2efc43cd25992
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
using System;
using System.Collections.Generic;
using System.Linq;
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;
 
namespace XHandler.Controls
{
    /// <summary>
    /// 按照步骤 1a 或 1b 操作,然后执行步骤 2 以在 XAML 文件中使用此自定义控件。
    ///
    /// 步骤 1a) 在当前项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
    /// 元素中:
    ///
    ///     xmlns:MyNamespace="clr-namespace:XHandler.Controls"
    ///
    ///
    /// 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
    /// 元素中:
    ///
    ///     xmlns:MyNamespace="clr-namespace:XHandler.Controls;assembly=XHandler.Controls"
    ///
    /// 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用,
    /// 并重新生成以避免编译错误:
    ///
    ///     在解决方案资源管理器中右击目标项目,然后依次单击
    ///     “添加引用”->“项目”->[浏览查找并选择此项目]
    ///
    ///
    /// 步骤 2)
    /// 继续操作并在 XAML 文件中使用控件。
    ///
    ///     <MyNamespace:TipsChannel/>
    ///
    /// </summary>
    public class TipsChannel : Grid
    {
        private int mark = 0;//sizechange执行次数
        public List<int> chs = new List<int>();
        public event EventHandler SelectedChangedEvent;
 
 
        static TipsChannel()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(TipsChannel), new FrameworkPropertyMetadata(typeof(TipsChannel)));
        }
 
        /// <summary>
        /// 通道数, 目前仅支持8通道
        /// </summary>
        public int TotalChannels
        {
            get { return (int)GetValue(TotalChannelsProperty); }
            set { SetValue(TotalChannelsProperty, value); }
        }
        public static readonly DependencyProperty TotalChannelsProperty =
            DependencyProperty.Register("TotalChannels", typeof(int), typeof(TipsChannel), new PropertyMetadata(0, OnTotalChannelChanged));
 
        #region 仅在第一次初始化界面时,设置界面的通道数目
        private static void OnTotalChannelChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            //TipsChannel tipsChannel = sender as TipsChannel;
            //int count = (int)e.NewValue;
            //tipsChannel.SetTotalChannelCount(count);
        }
        #endregion
 
        ///// <summary>
        ///// 选中通道集合
        ///// </summary>
        //public string Selection
        //{
        //    get { return (string)GetValue(SelectionProperty); }
        //    set { SetValue(SelectionProperty, value); }
        //}
        //public static readonly DependencyProperty SelectionProperty =
        //    DependencyProperty.Register("Selection", typeof(string), typeof(TipsChannel), new PropertyMetadata(""));
 
        /// <summary>
        /// 已被选中通道集合
        /// </summary>
        public List<int> SelectedChannels
        {
            get { return (List<int>)GetValue(SelectedChannelsProperty); }
            set { SetValue(SelectedChannelsProperty, value);}
        }
        public static readonly DependencyProperty SelectedChannelsProperty =
            DependencyProperty.Register("SelectedChannels", typeof(List<int>), typeof(TipsChannel), new PropertyMetadata(new List<int> (),OnSelectedChanged));
 
        #region 被选择的通道数变化事件
        private static void OnSelectedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            TipsChannel tipsChannel = sender as TipsChannel;
            tipsChannel.SetSelectedChannels();
        }
        #endregion
 
        #region 重新设置被选择的通道
        public void SetSelectedChannels()
        {
            foreach (var child in Children)
            {
                if (child is Ellipse)
                {
                    Ellipse ellipse = (Ellipse)child;
                    //判断是什么
                    if (SelectedChannels.Contains((int)ellipse.Tag))
                    {
                        ellipse.Fill = FindResource("blueBrush") as SolidColorBrush;
                        ellipse.Stroke = FindResource("blueBrush") as SolidColorBrush;
                    }
                    else
                    {
                        ellipse.Fill = Brushes.White;
                        ellipse.Stroke = new SolidColorBrush(Color.FromRgb(0x61, 0x64, 0x67));
                    }
                }
            }
        }
        #endregion
 
        #region 所有通道是否开启选择编辑
        /// <summary>
        /// 可编辑
        /// </summary>
        public bool CanEdit
        {
            get { return (bool)GetValue(CanEditProperty); }
            set { SetValue(CanEditProperty, value); }
        }
        public static readonly DependencyProperty CanEditProperty =
            DependencyProperty.Register("CanEdit", typeof(bool), typeof(TipsChannel), new PropertyMetadata(false));
        #endregion
 
        Rectangle MouseRect;
        Point startPoint;
        bool isMouseDown = false;
 
        #region 构造函数
        public TipsChannel()
        {
            InitializeComponent();
        }
        #endregion
 
        #region 初始化函数
        void InitializeComponent()
        {
            Initialized += TipsChannel_Initialized;
            //SizeChanged += TipsChannel_SizeChanged;
            PreviewMouseDown += TipsChannel_PreviewMouseDown;
            PreviewMouseMove += TipsChannel_PreviewMouseMove;
            PreviewMouseUp += TipsChannel_PreviewMouseUp;
            MouseLeave += TipsChannel_MouseLeave;
        }
        #endregion
 
        #region 鼠标离开控件显示区域后,更改鼠标按下状态标志
        private void TipsChannel_MouseLeave(object sender, MouseEventArgs e)
        {
            isMouseDown = false;
        }
        #endregion
 
        #region 鼠标抬起控件显示区域后,更改鼠标按下状态标志
        private void TipsChannel_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            isMouseDown = false;
        }
        #endregion
 
        #region 鼠标被按下后,在控件内选择通道事件
        private void TipsChannel_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (!CanEdit)
                return;
            if (isMouseDown)
            {
                Point curPos = e.GetPosition(this);
                if ((Math.Abs(curPos.X - startPoint.X) > SystemParameters.MinimumHorizontalDragDistance)
                    || (Math.Abs(curPos.Y - startPoint.Y) > SystemParameters.MinimumVerticalDragDistance))
                {
                    Rect rect = GetCorrectRect(startPoint, curPos);
 
                    foreach (var child in Children)
                    {
                        if (child is Ellipse)
                        {
                            Ellipse ellipse = (Ellipse)child;
                            GeneralTransform generalTransform1 = ellipse.TransformToAncestor(this);
                            Point currentPoint = generalTransform1.Transform(new Point(0, 0));
                            Rect childRect = new Rect(currentPoint.X, currentPoint.Y, ellipse.ActualWidth, ellipse.ActualHeight);
                            if (childRect.IntersectsWith(rect))
                            {
                                ellipse.Fill = FindResource("blueBrush") as SolidColorBrush;
                                ellipse.Stroke = FindResource("blueBrush") as SolidColorBrush;
                                if (SelectedChannels.Contains((int)ellipse.Tag))
                                {
 
                                }
                                else
                                {
                                    SelectedChannels.Add((int)ellipse.Tag);
                                }
                            }
                        }
                    }
                }
            }
        }
        #endregion
 
        #region 获取当前鼠标选中的矩形区域
        Rect GetCorrectRect(Point start, Point end)
        {
            if (end.X < start.X)
            {
                double tmp = start.X;
                start.X = end.X;
                end.X = tmp;
            }
            if (end.Y < start.Y)
            {
                double tmp = start.Y;
                start.Y = end.Y;
                end.Y = tmp;
            }
            return new Rect(start.X, start.Y, end.X - start.X, end.Y - start.Y);
        }
        #endregion
 
        #region 鼠标按下后事件
        private void TipsChannel_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (!CanEdit)
                return;
 
            startPoint = e.GetPosition(this);
            HitTestResult result = VisualTreeHelper.HitTest(this, startPoint);
            if (result.VisualHit is Ellipse)
            {
                var a = (Ellipse)result.VisualHit;
                //if (SelectedChannels.Contains((int)a.Tag))
                //{
                //    SelectedChannels.Remove((int)a.Tag);
                //}
                //else
                //{
                //    SelectedChannels.Add((int)a.Tag);
                //}
                isMouseDown = true;
 
                ResetSelection(a);
            }
        }
        #endregion
 
        #region 重新设置当前选中的区域内的通道
        private void ResetSelection(Ellipse e)
        {
            foreach (var child in Children)
            {
                if (child is Ellipse)
                {
                    Ellipse ellipse = (Ellipse)child;
                    //判断是什么
                    if (ellipse.Tag == e.Tag)//当前选中的看看是否已选中,如果选中则变为不选中,如果不选中则变为选中
                    {
                        if (SelectedChannels.Contains((int)e.Tag))
                        {
                            ellipse.Fill = Brushes.White;
                            ellipse.Stroke = new SolidColorBrush(Color.FromRgb(0x61, 0x64, 0x67));
                            SelectedChannels.Remove((int)e.Tag);
 
                        }
                        else
                        {
                            ellipse.Fill = FindResource("blueBrush") as SolidColorBrush;
                            ellipse.Stroke = FindResource("blueBrush") as SolidColorBrush;
                            SelectedChannels.Add((int)e.Tag);
                        }
                    }
                    //ellipse.Fill = Brushes.White;
                    //ellipse.Stroke = new SolidColorBrush(Color.FromRgb(0x61, 0x64, 0x67));
                }
            }
            chs = SelectedChannels;
            if (SelectedChangedEvent != null)
            {
                SelectedChangedEvent(this, EventArgs.Empty);
            }
        }
        #endregion
 
        //#region 控件显示外观大小变化事件
        //private void TipsChannel_SizeChanged(object sender, SizeChangedEventArgs e)
        //{
        //    if(mark>=1)
        //    {
        //        return;
        //    }
        //    this.ColumnDefinitions.Clear();
        //    this.RowDefinitions.Clear();
        //    this.Children.Clear();
 
        //    AddItems();
        //    mark++;
        //}
        //#endregion
 
        #region 控件初始化事件, 根据设置的通道总数,构建界面通道显示
        private void TipsChannel_Initialized(object sender, EventArgs e)
        {
            //if (mark >= 1)
            //{
            //    return;
            //}
            this.ColumnDefinitions.Clear();
            this.RowDefinitions.Clear();
            this.Children.Clear();
 
            AddItems();
            mark++;
        }
        #endregion
 
        #region 初始化控件内的通道数目
        private void AddItems()
        {
            if (TotalChannels > 8)
                TotalChannels = 8;
            this.RowDefinitions.Add(new RowDefinition());
            this.RowDefinitions.Add(new RowDefinition());
            for (int i = 0; i < TotalChannels; i++)
            {
                this.ColumnDefinitions.Add(new ColumnDefinition());
            }
 
            for (int i = 0; i < TotalChannels; i++)
            {
                Ellipse ellipse = new Ellipse()
                {
                    Margin = new Thickness(4, 2, 4, 2),
                    Width = 24,
                    Height = 24,
                    StrokeThickness = 1,
                    Fill = FindResource("blueBrush") as SolidColorBrush,
                    Stroke = FindResource("blueBrush") as SolidColorBrush,
                    Tag = i + 1
                };
                this.Children.Add(ellipse);
                if (!SelectedChannels.Contains(i + 1))
                {
                    SelectedChannels.Add(i + 1);
                }
                Grid.SetRow(ellipse, 0);
                Grid.SetColumn(ellipse, i);
 
                TextBlock tb = new TextBlock()
                {
                    Text = (i + 1).ToString(),
                    FontSize = 14,
                    HorizontalAlignment = HorizontalAlignment.Center,
                };
                this.Children.Add(tb);
                Grid.SetRow(tb, 1);
                Grid.SetColumn(tb, i);
            }
        }
        #endregion
 
        #region 重新构建通道显示
        public void ResetChannelItems()
        {
            SelectedChannels.Clear();
            this.ColumnDefinitions.Clear();
            this.RowDefinitions.Clear();
            this.Children.Clear();
 
            int currentChannelTotal = TotalChannels;//当前通道总数量
            this.RowDefinitions.Add(new RowDefinition());
            this.RowDefinitions.Add(new RowDefinition());
 
            for (int i = 0; i < currentChannelTotal; i++)
            {
                this.ColumnDefinitions.Add(new ColumnDefinition());
            }
 
            for (int i = 0; i < currentChannelTotal; i++)
            {
                #region 构建通道示意圆圈
                Ellipse ellipse = new Ellipse()
                {
                    Margin = new Thickness(4, 2, 4, 2),
                    Width = 24,
                    Height = 24,
                    StrokeThickness = 1,
                    Fill = FindResource("blueBrush") as SolidColorBrush,
                    Stroke = FindResource("blueBrush") as SolidColorBrush,
                    Tag = i + 1
                };
                this.Children.Add(ellipse);
                if (!SelectedChannels.Contains(i + 1))
                {
                    SelectedChannels.Add(i + 1);
                }
                Grid.SetRow(ellipse, 0);
                Grid.SetColumn(ellipse, i);
                #endregion
 
                #region 构建通道编号文字
                TextBlock tb = new TextBlock()
                {
                    Text = (i + 1).ToString(),
                    FontSize = 14,
                    HorizontalAlignment = HorizontalAlignment.Center,
                };
                this.Children.Add(tb);
                Grid.SetRow(tb, 1);
                Grid.SetColumn(tb, i);
                #endregion
            }
        }
        #endregion
 
        #region 
        public void SetTotalChannelCount(int count)
        {
            mark = 0;
            this.ColumnDefinitions.Clear();
            this.RowDefinitions.Clear();
            this.Children.Clear();
            TotalChannels = count;
            AddItems();
            chs = SelectedChannels;
 
        }
        #endregion
    }
}