UIControl.4.Render.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using System.Drawing;
  2. using System.Windows.Forms;
  3. using Microsoft.Drawing;
  4. namespace Microsoft.Windows.Forms
  5. {
  6. partial class UIControl
  7. {
  8. private Sprite m_Sprite;
  9. /// <summary>
  10. /// 获取精灵
  11. /// </summary>
  12. protected virtual Sprite Sprite
  13. {
  14. get
  15. {
  16. this.CheckDisposed();
  17. if (this.m_Sprite == null)
  18. this.m_Sprite = new Sprite(this);
  19. return this.m_Sprite;
  20. }
  21. }
  22. private int m_UpdateSuspendCount;
  23. /// <summary>
  24. /// 获取刷新操作是否被挂起
  25. /// </summary>
  26. public bool UpdateSuspended
  27. {
  28. get
  29. {
  30. return this.m_UpdateSuspendCount != 0 || (this.m_UIParent != null && this.m_UIParent.UpdateSuspended);
  31. }
  32. }
  33. /// <summary>
  34. /// 渲染控件
  35. /// </summary>
  36. /// <param name="e">数据</param>
  37. protected virtual void RenderSelf(PaintEventArgs e)
  38. {
  39. //准备
  40. Graphics g = e.Graphics;
  41. Rectangle rect = this.ClientRectangle;
  42. //渲染
  43. this.Sprite.BackColor = this.BackColor;
  44. this.Sprite.BeginRender(g);
  45. this.Sprite.RenderBackColor(rect);
  46. this.Sprite.RenderBorder(rect);
  47. this.Sprite.EndRender();
  48. }
  49. /// <summary>
  50. /// 渲染子控件
  51. /// </summary>
  52. /// <param name="e">数据</param>
  53. protected virtual void RenderChildren(PaintEventArgs e)
  54. {
  55. foreach (IUIControl control in this.UIControls)
  56. {
  57. if (control.Visible)
  58. using (new TranslateGraphics(e.Graphics, control.Location))
  59. control.RenderCore(e);
  60. }
  61. }
  62. /// <summary>
  63. /// 渲染控件和子控件
  64. /// </summary>
  65. /// <param name="e">数据</param>
  66. void IUIControl.RenderCore(PaintEventArgs e)
  67. {
  68. this.RenderSelf(e);
  69. this.RenderChildren(e);
  70. this.RaisePaintEvent(this, e);
  71. }
  72. /// <summary>
  73. /// 挂起刷新 UI
  74. /// </summary>
  75. public void BeginUpdate()
  76. {
  77. this.m_UpdateSuspendCount++;
  78. }
  79. /// <summary>
  80. /// 恢复刷新 UI
  81. /// </summary>
  82. public void EndUpdate()
  83. {
  84. this.EndUpdate(false);
  85. }
  86. /// <summary>
  87. /// 恢复刷新 UI,可以选择强制刷新
  88. /// </summary>
  89. /// <param name="forceUpdate">若要执行刷新为 true,否则为 false</param>
  90. public void EndUpdate(bool forceUpdate)
  91. {
  92. this.m_UpdateSuspendCount--;
  93. this.InvalidateCore(this.ClientRectangle, false, forceUpdate);
  94. }
  95. /// <summary>
  96. /// 使控件工作区无效
  97. /// </summary>
  98. public void Invalidate()
  99. {
  100. this.InvalidateCore(this.ClientRectangle, false, false);
  101. }
  102. /// <summary>
  103. /// 使控件工作区无效
  104. /// </summary>
  105. /// <param name="invalidateChildren">使控件所在的 Win32 窗口的子控件无效为 true,否则为 false</param>
  106. public void Invalidate(bool invalidateChildren)
  107. {
  108. this.InvalidateCore(this.ClientRectangle, invalidateChildren, false);
  109. }
  110. /// <summary>
  111. /// 使控件矩形无效
  112. /// </summary>
  113. /// <param name="rc">无效矩形</param>
  114. public void Invalidate(Rectangle rc)
  115. {
  116. this.InvalidateCore(rc, false, false);
  117. }
  118. /// <summary>
  119. /// 使控件矩形无效
  120. /// </summary>
  121. /// <param name="rc">无效矩形</param>
  122. /// <param name="invalidateChildren">使控件所在的 Win32 窗口的子控件无效为 true,否则为 false</param>
  123. public void Invalidate(Rectangle rc, bool invalidateChildren)
  124. {
  125. this.InvalidateCore(rc, invalidateChildren, false);
  126. }
  127. /// <summary>
  128. /// 使控件矩形无效,可以选择是否强制刷新
  129. /// </summary>
  130. /// <param name="rc">无效矩形</param>
  131. /// <param name="invalidateChildren">使控件所在的 Win32 窗口的子控件无效为 true,否则为 false</param>
  132. /// <param name="forceUpdate">强制刷新为 true,否则为false</param>
  133. protected void InvalidateCore(Rectangle rc, bool invalidateChildren, bool forceUpdate)
  134. {
  135. if (forceUpdate || !this.UpdateSuspended)
  136. {
  137. IUIControl parent = this;
  138. IUIWindow window = null;
  139. while (parent != null && (window = parent as IUIWindow) == null)
  140. {
  141. rc.Offset(parent.Left, parent.Top);
  142. parent = parent.UIParent;
  143. }
  144. if (window == null)
  145. return;
  146. window.Invalidate(rc, invalidateChildren);
  147. }
  148. }
  149. /// <summary>
  150. /// 重绘所在 Win32 窗口的无效区域
  151. /// </summary>
  152. public void Update()
  153. {
  154. this.UpdateCore(false);
  155. }
  156. /// <summary>
  157. /// 重绘所在 Win32 窗口的无效区域,可以选择是否强制刷新
  158. /// </summary>
  159. /// <param name="forceUpdate">强制刷新为 true,否则为false</param>
  160. protected void UpdateCore(bool forceUpdate)
  161. {
  162. if (forceUpdate || !this.UpdateSuspended)
  163. {
  164. IUIControl parent = this;
  165. IUIWindow window = null;
  166. while (parent != null && (window = parent as IUIWindow) == null)
  167. parent = parent.UIParent;
  168. if (window == null)
  169. return;
  170. window.Update();
  171. }
  172. }
  173. /// <summary>
  174. /// 立即刷新所在 Win32 窗口和其子控件
  175. /// </summary>
  176. public void Refresh()
  177. {
  178. this.RefreshCore(false);
  179. }
  180. /// <summary>
  181. /// 立即刷新所在 Win32 窗口和其子控件,可以选择是否强制刷新
  182. /// </summary>
  183. /// <param name="forceUpdate">强制刷新为 true,否则为false</param>
  184. protected void RefreshCore(bool forceUpdate)
  185. {
  186. if (forceUpdate || !this.UpdateSuspended)
  187. {
  188. IUIControl parent = this;
  189. IUIWindow window = null;
  190. Rectangle rc = this.ClientRectangle;
  191. while (parent != null && (window = parent as IUIWindow) == null)
  192. {
  193. rc.Offset(parent.Left, parent.Top);
  194. parent = parent.UIParent;
  195. }
  196. if (window == null)
  197. return;
  198. window.Invalidate(rc, true);
  199. window.Update();
  200. }
  201. }
  202. }
  203. }