lixiaojun
2024-08-16 2779e00649911e53903b7ff15c55eae9cfda6333
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
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);
 
        }
 
 
        private Network _network = null;//管网
        private Bitmap _cache = null;//绘制缓存
        private float _zoomMin = 1;//最小缩放层级
        private float _zoomMax = 3200;//最大缩放层级
        private float _zoom = 1;//当前缩放层级
        private float _dxo = 0;//x偏移量
        private float _dyo = 0;//y偏移量
 
        /// <summary>
        /// 是否初始化
        /// </summary>
        public bool Initialized => _network != null;
 
 
 
 
        /// <summary>
        /// 初始化
        /// </summary>
        public virtual void Initial(Network network)
        {
            _network = network;
            ZoomAll();
        }
 
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (!Initialized)
            {
                return;
            }
            try
            {
                e.Graphics.PageUnit = GraphicsUnit.Pixel;
                e.Graphics.InterpolationMode = InterpolationMode.High;
                e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
                e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
                e.Graphics.PageScale = _zoom;
                e.Graphics.TranslateTransform(_dxo, _dyo);
                e.Graphics.ScaleTransform(1, -1);
            }
            catch
            {
                return;
            }
            _network.Draw(e.Graphics);
        }
 
        /// <summary>
        /// 界面尺寸发生改变
        /// </summary>
        /// <param name="eventargs"></param>
        protected override void OnResize(EventArgs eventargs)
        {
            base.OnResize(eventargs);
            ZoomAll();
        }
 
        /// <summary>
        /// 释放资源
        /// </summary>
        protected virtual void DisposeResources()
        {
            if (_cache != null)
            {
                _cache.Dispose();
            }
 
        }
 
 
        /// <summary>
        /// 自适应
        /// </summary>
        public void ZoomAll()
        {
            if (!Initialized)
            {
                return;
            }
 
            float w = Width;
            float h = Height;
            var networkBounds = _network.GetBounds();
            if (networkBounds.IsEmpty)
            {
                return;
            }
            _zoom = networkBounds.Width / networkBounds.Height < w / h ?
                h / networkBounds.Height :
                w / networkBounds.Width;
 
 
            _dxo = -networkBounds.X;
            _dyo = networkBounds.Bottom;
 
            _zoom *= 0.95f;
 
            _dxo += w * 0.5f / _zoom - networkBounds.Width * 0.5f;
            _dyo += h * 0.5f / _zoom - networkBounds.Height * 0.5f;
 
            Invalidate();
        }
 
 
 
 
 
 
 
 
 
 
 
 
 
    }
}