using System.Drawing; using System.Windows.Forms; using Microsoft.Drawing; namespace Microsoft.Windows.Forms { partial class UIControl { private Sprite m_Sprite; /// /// 获取精灵 /// protected virtual Sprite Sprite { get { this.CheckDisposed(); if (this.m_Sprite == null) this.m_Sprite = new Sprite(this); return this.m_Sprite; } } private int m_UpdateSuspendCount; /// /// 获取刷新操作是否被挂起 /// public bool UpdateSuspended { get { return this.m_UpdateSuspendCount != 0 || (this.m_UIParent != null && this.m_UIParent.UpdateSuspended); } } /// /// 渲染控件 /// /// 数据 protected virtual void RenderSelf(PaintEventArgs e) { //准备 Graphics g = e.Graphics; Rectangle rect = this.ClientRectangle; //渲染 this.Sprite.BackColor = this.BackColor; this.Sprite.BeginRender(g); this.Sprite.RenderBackColor(rect); this.Sprite.RenderBorder(rect); this.Sprite.EndRender(); } /// /// 渲染子控件 /// /// 数据 protected virtual void RenderChildren(PaintEventArgs e) { foreach (IUIControl control in this.UIControls) { if (control.Visible) using (new TranslateGraphics(e.Graphics, control.Location)) control.RenderCore(e); } } /// /// 渲染控件和子控件 /// /// 数据 void IUIControl.RenderCore(PaintEventArgs e) { this.RenderSelf(e); this.RenderChildren(e); this.RaisePaintEvent(this, e); } /// /// 挂起刷新 UI /// public void BeginUpdate() { this.m_UpdateSuspendCount++; } /// /// 恢复刷新 UI /// public void EndUpdate() { this.EndUpdate(false); } /// /// 恢复刷新 UI,可以选择强制刷新 /// /// 若要执行刷新为 true,否则为 false public void EndUpdate(bool forceUpdate) { this.m_UpdateSuspendCount--; this.InvalidateCore(this.ClientRectangle, false, forceUpdate); } /// /// 使控件工作区无效 /// public void Invalidate() { this.InvalidateCore(this.ClientRectangle, false, false); } /// /// 使控件工作区无效 /// /// 使控件所在的 Win32 窗口的子控件无效为 true,否则为 false public void Invalidate(bool invalidateChildren) { this.InvalidateCore(this.ClientRectangle, invalidateChildren, false); } /// /// 使控件矩形无效 /// /// 无效矩形 public void Invalidate(Rectangle rc) { this.InvalidateCore(rc, false, false); } /// /// 使控件矩形无效 /// /// 无效矩形 /// 使控件所在的 Win32 窗口的子控件无效为 true,否则为 false public void Invalidate(Rectangle rc, bool invalidateChildren) { this.InvalidateCore(rc, invalidateChildren, false); } /// /// 使控件矩形无效,可以选择是否强制刷新 /// /// 无效矩形 /// 使控件所在的 Win32 窗口的子控件无效为 true,否则为 false /// 强制刷新为 true,否则为false protected void InvalidateCore(Rectangle rc, bool invalidateChildren, bool forceUpdate) { if (forceUpdate || !this.UpdateSuspended) { IUIControl parent = this; IUIWindow window = null; while (parent != null && (window = parent as IUIWindow) == null) { rc.Offset(parent.Left, parent.Top); parent = parent.UIParent; } if (window == null) return; window.Invalidate(rc, invalidateChildren); } } /// /// 重绘所在 Win32 窗口的无效区域 /// public void Update() { this.UpdateCore(false); } /// /// 重绘所在 Win32 窗口的无效区域,可以选择是否强制刷新 /// /// 强制刷新为 true,否则为false protected void UpdateCore(bool forceUpdate) { if (forceUpdate || !this.UpdateSuspended) { IUIControl parent = this; IUIWindow window = null; while (parent != null && (window = parent as IUIWindow) == null) parent = parent.UIParent; if (window == null) return; window.Update(); } } /// /// 立即刷新所在 Win32 窗口和其子控件 /// public void Refresh() { this.RefreshCore(false); } /// /// 立即刷新所在 Win32 窗口和其子控件,可以选择是否强制刷新 /// /// 强制刷新为 true,否则为false protected void RefreshCore(bool forceUpdate) { if (forceUpdate || !this.UpdateSuspended) { IUIControl parent = this; IUIWindow window = null; Rectangle rc = this.ClientRectangle; while (parent != null && (window = parent as IUIWindow) == null) { rc.Offset(parent.Left, parent.Top); parent = parent.UIParent; } if (window == null) return; window.Invalidate(rc, true); window.Update(); } } } }