lixiaojun
4 天以前 fba4613d6b8dcbcaea6c7dc83bda14ed49d2f6de
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
namespace Yw.WinFrmUI.HydroL2d
{
    /// <summary>
    /// 管网面板
    /// </summary>
    public partial class NetworkPanel : Panel
    {
        public NetworkPanel()
        {
            InitializeComponent();
 
            #region 防止闪烁
 
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.DoubleBuffer, true);
            SetStyle(ControlStyles.UserPaint, true);
 
            #endregion
 
            //设置允许的最小尺寸
            this.MinimumSize = new Size(10, 10);
        }
 
        #region 初始化
 
        protected Network _network = null;//管网
        protected RectangleF _networkBounds;//管网边界
        protected RectangleF _clientRectf;//工作区域矩形
 
        /// <summary>
        /// 是否初始化
        /// </summary>
        public bool Initialized => _network != null;
 
        /// <summary>
        /// 初始化
        /// </summary>
        public virtual void Initial(Network network)
        {
            _network = network;
            _networkBounds = _network.GetBounds();
            _clientRectf = GetClientRectangleF();
            CreateCache();
            ZoomAll();
        }
 
        #endregion
 
        #region 缓存与显示
 
        private Bitmap _cache = null;//缓存<依据工作区域创建的空图片>
        private object _cacheLocker = new();//缓存锁
 
        /// <summary>
        /// 创建缓存
        /// </summary>
        protected virtual void CreateCache()
        {
            if (!Initialized)
            {
                return;
            }
            var img = new Bitmap((int)_clientRectf.Width, (int)_clientRectf.Height);
            lock (_cacheLocker)
            {
                if (_cache != null)
                {
                    _cache.Dispose();
                }
                _cache = img;
            }
        }
 
        /// <summary>
        /// 绘制缓存
        /// </summary>
        protected virtual void DrawCache()
        {
            if (!Initialized)
            {
                return;
            }
            if (_cache == null)
            {
                return;
            }
            var dispRect = GetDispRectangleF();
            using (var g = Graphics.FromImage(_cache))
            {
                g.Clear(Color.White);
                g.PageUnit = GraphicsUnit.Pixel;
                g.InterpolationMode = InterpolationMode.High;
                g.SmoothingMode = SmoothingMode.AntiAlias;
                g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                g.PageScale = _zoom;
                g.TranslateTransform(_dxo, _dyo);
                g.ScaleTransform(1, -1);
 
                _network.Draw(g, dispRect);
            }
        }
 
        /// <summary>
        /// 应用缓存
        /// </summary>
        protected virtual void ApplyCache(Graphics g)
        {
            if (!Initialized)
            {
                return;
            }
            lock (_cacheLocker)
            {
                if (_cache != null)
                {
                    g.DrawImage(_cache, _clientRectf);
                }
            }
 
        }
 
        #endregion
 
        #region 缩放与偏移
 
        private float _zoomMin = 0.001f;//最小缩放层级
        private float _zoomMax = 30000f;//最大缩放层级
        private float _zoom = 1f;//当前缩放层级
        private float _dxo = 0f;//x偏移量
        private float _dyo = 0f;//y偏移量
 
        /// <summary>
        /// 缩放等级
        /// </summary>
        public float Zoom
        {
            get => _zoom;
            private set
            {
                if (_networkBounds.IsEmpty)
                {
                    _zoom = 1f;
                    return;
                }
 
                float sizeMax = Math.Max(_networkBounds.Height, _networkBounds.Width);
                float sizeNew = sizeMax * value;
 
                if (sizeNew > _zoomMax)
                {
                    _zoom = _zoomMax / sizeMax;
                }
                else if (sizeNew < _zoomMin)
                {
                    _zoom = _zoomMin / sizeMax;
                }
                else
                {
                    _zoom = value;
                }
            }
        }
 
        /// <summary>
        /// 自适应
        /// </summary>
        public void ZoomAll()
        {
            if (!Initialized)
            {
                return;
            }
            var networkBounds = _network.GetBounds();
            if (networkBounds.IsEmpty)
            {
                return;
            }
            this.Zoom = networkBounds.Width / networkBounds.Height < this.Width / this.Height ?
                this.Height / networkBounds.Height :
                this.Width / networkBounds.Width;
 
 
            _dxo = -networkBounds.X;
            _dyo = networkBounds.Bottom;
 
            this.Zoom *= 0.9f;
 
            _dxo += this.Width * 0.5f / this.Zoom - networkBounds.Width * 0.5f;
            _dyo += this.Height * 0.5f / this.Zoom - networkBounds.Height * 0.5f;
        }
 
 
        private void ZoomToPoint(MouseEventArgs e)
        {
            float scale = e.Delta > 0 ? this.Zoom * 1.5f : this.Zoom / 1.5f;
            var pt = ClientToDispPoint(e.Location);
            this.Zoom = scale;
            _dxo = -pt.X + e.X / this.Zoom;
            _dyo = pt.Y + e.Y / this.Zoom;
        }
 
        #endregion
 
        #region 坐标转换
 
        protected PointF ClientToDispPoint(PointF cp)
        {
            return new PointF(-_dxo + cp.X / _zoom, _dyo - cp.Y / _zoom);
        }
 
        #endregion
 
        #region 边框绘制
 
        /// <summary>
        /// 边框颜色
        /// </summary>
        [Browsable(true)]
        [Description("自定义边框颜色")]
        public Color CustomBorderColor
        {
            get { return _customBorderColor; }
            set { _customBorderColor = value; }
        }
        private Color _customBorderColor = Color.Gray;
 
        /// <summary>
        /// 边框宽度
        /// </summary>
        [Browsable(true)]
        [Description("自定义边框宽度")]
        public int CustomBorderWidth
        {
            get { return _customBorderWidth; }
            set { _customBorderWidth = value; }
        }
        private int _customBorderWidth = 1;
 
        /// <summary>
        /// 边框是否显示
        /// </summary>
        [Browsable(true)]
        [Description("自定义边框可见性")]
        [DefaultValue(false)]
        public bool CustomBorderVisible
        {
            get { return _customBorderVisible; }
            set
            {
                _customBorderVisible = value;
            }
        }
        private bool _customBorderVisible = false;
 
        /// <summary>
        /// 绘制自定义边框
        /// </summary>
        protected virtual void DrawCustomBorder(Graphics g)
        {
            if (this.CustomBorderVisible)
            {
                ControlPaint.DrawBorder(g,
                                                    this.ClientRectangle,
                                                    this.CustomBorderColor, this.CustomBorderWidth, ButtonBorderStyle.Solid,
                                                    this.CustomBorderColor, this.CustomBorderWidth, ButtonBorderStyle.Solid,
                                                    this.CustomBorderColor, this.CustomBorderWidth, ButtonBorderStyle.Solid,
                                                    this.CustomBorderColor, this.CustomBorderWidth, ButtonBorderStyle.Solid);
            }
        }
 
 
        #endregion
 
        #region 获取矩形区域
 
        /// <summary>
        /// 获取工作区域
        /// </summary>
        /// <returns></returns>
        protected virtual RectangleF GetClientRectangleF()
        {
            return new RectangleF(0, 0, this.Width, this.Height);
        }
 
        /// <summary>
        /// 获取显示区域
        /// </summary>
        protected virtual RectangleF GetDispRectangleF()
        {
            if (_clientRectf.IsEmpty)
            {
                return default;
            }
            if (!Initialized)
            {
                return default;
            }
            var pt = ClientToDispPoint(new PointF(_clientRectf.X, _clientRectf.Height));
            var width = _clientRectf.Width / _zoom;
            var height = _clientRectf.Height / _zoom;
            return new RectangleF(pt, new SizeF(width, height));
        }
 
        #endregion
 
        #region 鼠标左键按下拖动
 
        /// <summary>
        /// 当鼠标左键按下时允许拖动
        /// </summary>
        [Browsable(true)]
        [Description("当鼠标左键按下时允许拖动")]
        [DefaultValue(true)]
        public bool AllowMoveWhenMouseLeftDown
        {
            get => _allowMoveWhenMouseLeftDown;
            set => _allowMoveWhenMouseLeftDown = value;
        }
        private bool _allowMoveWhenMouseLeftDown = true;
 
        protected bool _hasMouseLeftDown = false;//鼠标左键是否按下
        protected Point _mouseLeftDownMovePoint;//鼠标左键按下移动点
 
        /// <summary>
        /// 判断鼠标左键是否按下
        /// </summary>
        protected virtual bool HasMouseLeftDown(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _hasMouseLeftDown = true;
                _mouseLeftDownMovePoint = e.Location;
                return true;
            }
            return false;
        }
 
        /// <summary>
        /// 鼠标左键按下移动
        /// </summary>
        /// <param name="e"></param>
        protected virtual bool MouseLeftDownMove(MouseEventArgs e)
        {
            if (_hasMouseLeftDown)
            {
                if (this.AllowMoveWhenMouseLeftDown)
                {
                    if (this.Initialized)
                    {
                        var pt = new PointF(e.X - _mouseLeftDownMovePoint.X, e.Y - _mouseLeftDownMovePoint.Y);
                        _dxo += pt.X / _zoom;
                        _dyo += pt.Y / _zoom;
                        _mouseLeftDownMovePoint = e.Location;
                        return true;
                    }
                }
            }
            return false;
        }
 
        /// <summary>
        /// 判断鼠标左键是否弹起
        /// </summary>
        protected virtual bool HasMouseLeftUp(MouseEventArgs e)
        {
            if (_hasMouseLeftDown)
            {
                _hasMouseLeftDown = false;
                return true;
            }
            return false;
        }
 
        #endregion
 
        #region 鼠标右键按下拖动
 
        /// <summary>
        /// 当鼠标右键按下时允许拖动
        /// </summary>
        [Browsable(true)]
        [Description("当鼠标右键按下时允许拖动")]
        [DefaultValue(true)]
        public bool AllowMoveWhenMouseRightDown
        {
            get => _allowMoveWhenMouseRightDown;
            set => _allowMoveWhenMouseRightDown = value;
        }
        private bool _allowMoveWhenMouseRightDown = true;
 
        protected bool _hasMouseRightDown = false;//鼠标右键是否按下
        protected Point _mouseRightDownMovePoint;//鼠标右键按下移动点
 
        /// <summary>
        /// 判断鼠标右键是否按下
        /// </summary>
        protected virtual bool HasMouseRightDown(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                _hasMouseRightDown = true;
                _mouseRightDownMovePoint = e.Location;
                return true;
            }
            return false;
        }
 
        /// <summary>
        /// 鼠标右键按下移动
        /// </summary>
        protected virtual bool MouseRightDownMove(MouseEventArgs e)
        {
            if (_hasMouseRightDown)
            {
                if (this.AllowMoveWhenMouseRightDown)
                {
                    if (this.Initialized)
                    {
                        var pt = new PointF(e.X - _mouseRightDownMovePoint.X, e.Y - _mouseRightDownMovePoint.Y);
                        _dxo += pt.X / _zoom;
                        _dyo += pt.Y / _zoom;
                        _mouseRightDownMovePoint = e.Location;
                        return true;
                    }
                }
            }
            return false;
        }
 
        /// <summary>
        /// 判断鼠标左键是否弹起
        /// </summary>
        protected virtual bool HasMouseRightUp(MouseEventArgs e)
        {
            if (_hasMouseRightDown)
            {
                _hasMouseRightDown = false;
                return true;
            }
            return false;
        }
 
        #endregion
 
        #region 鼠标左键双击恢复自适应
 
        /// <summary>
        /// 当鼠标双击时允许自适应
        /// </summary>
        [Browsable(true)]
        [Description("当鼠标双击时允许自适应")]
        [DefaultValue(true)]
        public bool AllowZoomAllWhenMouseLeftDoubleClick
        {
            get => _allowZoomAllWhenMouseLeftDoubleClick;
            set => _allowZoomAllWhenMouseLeftDoubleClick = value;
        }
        private bool _allowZoomAllWhenMouseLeftDoubleClick = true;
 
        /// <summary>
        /// 鼠标双击时自适应
        /// </summary>
        protected virtual bool ZoomAllWhenMouseLeftDoubleClick(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (e.Clicks > 1)
                {
                    if (this.AllowZoomAllWhenMouseLeftDoubleClick)
                    {
                        if (this.Initialized)
                        {
                            ZoomAll();
                            return true;
                        }
                    }
                }
            }
            return false;
        }
 
        #endregion
 
        #region 鼠标悬停
 
        /// <summary>
        /// 允许鼠标悬停
        /// </summary>
        [Browsable(true)]
        [Description("允许鼠标悬停")]
        [DefaultValue(true)]
        public bool AllowMouseHover
        {
            get => _allowMouseHover;
            set => _allowMouseHover = value;
        }
        private bool _allowMouseHover = true;
        private List<Parter> _lastHoverList = null;//最后一次悬停列表
 
        /// <summary>
        /// 悬停
        /// 如果发生改变就返回true
        /// </summary>
        protected virtual bool Hover(MouseEventArgs e)
        {
            if (this.AllowMouseHover)
            {
                if (this.Initialized)
                {
                    var pt = ClientToDispPoint(e.Location);
                    var hoverList = _network.Hover(pt);
                    if (hoverList == null || hoverList.Count < 1)
                    {
                        if (_lastHoverList == null || _lastHoverList.Count < 1)
                        {
                            return false;
                        }
                    }
                    _lastHoverList = hoverList;
                    return true;
                }
            }
            if (_lastHoverList == null || _lastHoverList.Count < 1)
            {
                return false;
            }
            return true;
        }
 
        #endregion
 
        #region 鼠标左键点击选择
 
        /// <summary>
        /// 允许鼠标左键单击选择
        /// </summary>
        [Browsable(true)]
        [Description("允许鼠标左键单击选择")]
        [DefaultValue(true)]
        public bool AllowMouseLeftSingleClickSelect
        {
            get => _allowMouseLeftSingleClickSelect;
            set => _allowMouseLeftSingleClickSelect = value;
        }
        private bool _allowMouseLeftSingleClickSelect = true;
        private List<Parter> _lastMouseLeftClickSelectList = null;//最后一次鼠标左键单击选择列表
 
 
        /// <summary>
        /// 当鼠标左键单击时选择
        /// </summary>
        protected virtual bool SelectWhenMouseLeftSingleClick(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (e.Clicks == 1)
                {
                    if (this.Initialized)
                    {
                        var pt = ClientToDispPoint(e.Location);
                        var selectList = _network.Select(pt);
                        if (selectList == null || selectList.Count < 1)
                        {
                            if (_lastMouseLeftClickSelectList == null || _lastMouseLeftClickSelectList.Count < 1)
                            {
                                return false;
                            }
                        }
                        _lastMouseLeftClickSelectList = selectList;
                        return true;
                    }
                }
            }
            if (_lastMouseLeftClickSelectList == null || _lastMouseLeftClickSelectList.Count < 1)
            {
                return false;
            }
            return true;
        }
 
        #endregion
 
        #region 鼠标滚轮滚动缩放
 
        /// <summary>
        /// 当鼠标滚轮滚动时允许缩放
        /// </summary>
        [Browsable(true)]
        [Description("当鼠标滚轮滚动时允许缩放")]
        [DefaultValue(true)]
        public bool AllowZoomWhenMouseWheelRoll
        {
            get => _allowZoomWhenMouseWheelRoll;
            set => _allowZoomWhenMouseWheelRoll = value;
        }
        private bool _allowZoomWhenMouseWheelRoll = true;
 
        /// <summary>
        /// 当鼠标滚轮滚动时缩放
        /// </summary>
        protected virtual bool ZoomWhenMouseWheelRoll(MouseEventArgs e)
        {
            if (e.Delta != 0)
            {
                if (this.AllowZoomWhenMouseWheelRoll)
                {
                    if (this.Initialized)
                    {
                        ZoomToPoint(e);
                        return true;
                    }
                }
            }
            return false;
        }
 
        #endregion
 
        #region 释放资源
 
        /// <summary>
        /// 释放资源
        /// </summary>
        protected virtual void DisposeResources()
        {
            if (_cache != null)
            {
                _cache.Dispose();
            }
 
        }
 
        #endregion
 
 
 
        /// <summary>
        /// 重绘
        /// </summary>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            DrawCustomBorder(e.Graphics);
            DrawCache();
            ApplyCache(e.Graphics);
 
        }
 
        /// <summary>
        /// 界面尺寸发生改变
        /// </summary>
        /// <param name="eventargs"></param>
        protected override void OnResize(EventArgs eventargs)
        {
            base.OnResize(eventargs);
            _clientRectf = GetClientRectangleF();
            ZoomAll();
            CreateCache();
            this.Invalidate();
        }
 
        /// <summary>
        /// 鼠标按下
        /// </summary>
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            HasMouseLeftDown(e);
            HasMouseRightDown(e);
        }
 
        /// <summary>
        /// 鼠标移动
        /// </summary>
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            var hoverResult = Hover(e);
            var hasMouseLeftDownMove = MouseLeftDownMove(e);
            var hasMouseRightDownMove = MouseRightDownMove(e);
            if (hoverResult || hasMouseLeftDownMove || hasMouseRightDownMove)
            {
                this.Invalidate();
            }
        }
 
        /// <summary>
        /// 鼠标弹起
        /// </summary>
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            var hasMouseLeftUp = HasMouseLeftUp(e);
            var hasMouseRightUp = HasMouseRightUp(e);
            var mouseLeftSingleClickSelectResult = SelectWhenMouseLeftSingleClick(e);
            if (hasMouseLeftUp || hasMouseRightUp || mouseLeftSingleClickSelectResult)
            {
                this.Invalidate();
            }
        }
 
        //鼠标滚轮滚动
        protected override void OnMouseWheel(MouseEventArgs e)
        {
            base.OnMouseWheel(e);
            var zoomWhenMouseWheelRollResult = ZoomWhenMouseWheelRoll(e);
            if (zoomWhenMouseWheelRollResult)
            {
                this.Refresh();
            }
        }
 
        /// <summary>
        /// 鼠标双击
        /// </summary>
        protected override void OnMouseDoubleClick(MouseEventArgs e)
        {
            base.OnMouseDoubleClick(e);
            var zoomAllWhenMouseLeftDoubleClickResult = ZoomAllWhenMouseLeftDoubleClick(e);
            if (zoomAllWhenMouseLeftDoubleClickResult)
            {
                this.Invalidate();
            }
        }
 
    }
}