namespace Yw.WinFrmUI.HydroL2d { /// /// 管网面板 /// 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偏移量 /// /// 是否初始化 /// public bool Initialized => _network != null; /// /// 初始化 /// 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); } /// /// 界面尺寸发生改变 /// /// protected override void OnResize(EventArgs eventargs) { base.OnResize(eventargs); ZoomAll(); } /// /// 释放资源 /// protected virtual void DisposeResources() { if (_cache != null) { _cache.Dispose(); } } /// /// 自适应 /// 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(); } } }