ProgressBarCellRenderer.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. //#########################################################################################
  2. //★★★★★★★ http://www.cnpopsoft.com [华普软件] ★★★★★★★
  3. //★★★★★★★ 华普软件 - VB & C#.NET 专业论文与源码荟萃! ★★★★★★★
  4. //#########################################################################################
  5. /*
  6. * Copyright ?2005, Mathew Hall
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. *
  12. * - Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  25. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  28. * OF SUCH DAMAGE.
  29. */
  30. using System;
  31. using System.Drawing;
  32. using System.Drawing.Drawing2D;
  33. using System.Windows.Forms;
  34. using XPTable.Events;
  35. using XPTable.Models;
  36. using XPTable.Themes;
  37. namespace XPTable.Renderers
  38. {
  39. /// <summary>
  40. /// A CellRenderer that draws Cell contents as a ProgressBar
  41. /// </summary>
  42. public class ProgressBarCellRenderer : CellRenderer
  43. {
  44. #region Class Data
  45. /// <summary>
  46. /// Specifies whether the ProgressBar's value as a string
  47. /// should be displayed
  48. /// </summary>
  49. private bool drawPercentageText;
  50. #endregion
  51. #region Constructor
  52. /// <summary>
  53. /// Initializes a new instance of the ProgressBarCellRenderer class with
  54. /// default settings
  55. /// </summary>
  56. public ProgressBarCellRenderer() : base()
  57. {
  58. this.drawPercentageText = true;
  59. }
  60. #endregion
  61. #region Properties
  62. /// <summary>
  63. /// Gets the rectangle that represents the client area of the Renderer
  64. /// </summary>
  65. public new Rectangle ClientRectangle
  66. {
  67. get
  68. {
  69. Rectangle client = base.ClientRectangle;
  70. client.Inflate(-1, -1);
  71. return client;
  72. }
  73. }
  74. /// <summary>
  75. /// Gets or sets whether the ProgressBar's value as a string
  76. /// should be displayed
  77. /// </summary>
  78. public bool DrawPercentageText
  79. {
  80. get
  81. {
  82. return this.drawPercentageText;
  83. }
  84. set
  85. {
  86. this.drawPercentageText = value;
  87. }
  88. }
  89. #endregion
  90. #region Events
  91. #region Paint
  92. /// <summary>
  93. /// Raises the PaintCell event
  94. /// </summary>
  95. /// <param name="e">A PaintCellEventArgs that contains the event data</param>
  96. public override void OnPaintCell(PaintCellEventArgs e)
  97. {
  98. if (e.Table.ColumnModel.Columns[e.Column] is ProgressBarColumn)
  99. {
  100. this.drawPercentageText = ((ProgressBarColumn) e.Table.ColumnModel.Columns[e.Column]).DrawPercentageText;
  101. }
  102. else
  103. {
  104. this.drawPercentageText = false;
  105. }
  106. base.OnPaintCell(e);
  107. }
  108. /// <summary>
  109. /// Raises the PaintBackground event
  110. /// </summary>
  111. /// <param name="e">A PaintCellEventArgs that contains the event data</param>
  112. protected override void OnPaintBackground(PaintCellEventArgs e)
  113. {
  114. base.OnPaintBackground(e);
  115. // don't bother if the Cell is null
  116. if (e.Cell == null)
  117. {
  118. return;
  119. }
  120. // fill the client area with the window color (this
  121. // will be the background color of the progress bar)
  122. e.Graphics.FillRectangle(SystemBrushes.Window, this.ClientRectangle);
  123. Rectangle progressRect = this.ClientRectangle;
  124. // draw the border
  125. if (e.Enabled)
  126. {
  127. // if xp themes are enabled, shrink the size of the
  128. // progress bar as otherwise the focus rect appears
  129. // to go awol if the cell has focus
  130. if (ThemeManager.VisualStylesEnabled)
  131. {
  132. progressRect.Inflate(-1, -1);
  133. }
  134. ThemeManager.DrawProgressBar(e.Graphics, progressRect);
  135. }
  136. else
  137. {
  138. using (Bitmap b = new Bitmap(progressRect.Width, progressRect.Height))
  139. {
  140. using (Graphics g = Graphics.FromImage(b))
  141. {
  142. ThemeManager.DrawProgressBar(g, new Rectangle(0, 0, progressRect.Width, progressRect.Height));
  143. }
  144. ControlPaint.DrawImageDisabled(e.Graphics, b, progressRect.X, progressRect.Y, this.BackBrush.Color);
  145. }
  146. }
  147. }
  148. /// <summary>
  149. /// Raises the Paint event
  150. /// </summary>
  151. /// <param name="e">A PaintCellEventArgs that contains the event data</param>
  152. protected override void OnPaint(PaintCellEventArgs e)
  153. {
  154. base.OnPaint(e);
  155. // don't bother if the Cell is null
  156. if (e.Cell == null)
  157. {
  158. return;
  159. }
  160. // get the Cells value
  161. int intVal = 0;
  162. if (e.Cell.Data != null && e.Cell.Data is int)
  163. {
  164. intVal = (int) e.Cell.Data;
  165. }
  166. if (intVal < 0)
  167. {
  168. intVal = 0;
  169. }
  170. else if (intVal > 100)
  171. {
  172. intVal = 100;
  173. }
  174. // adjust the chunk rect so we don't draw over the
  175. // progress bars borders
  176. Rectangle chunkRect = this.ClientRectangle;
  177. chunkRect.Inflate(-2, -2);
  178. // if xp themes are enabled, shrink the size of the
  179. // progress bar as otherwise the focus rect appears
  180. // to go awol if the cell has focus
  181. if (ThemeManager.VisualStylesEnabled)
  182. {
  183. chunkRect.Inflate(-1, -1);
  184. }
  185. chunkRect.Width = (int) ((((double) intVal) / 100d) * ((double) chunkRect.Width));
  186. if (e.Enabled)
  187. {
  188. ThemeManager.DrawProgressBarChunks(e.Graphics, chunkRect);
  189. }
  190. else
  191. {
  192. using (Bitmap b = new Bitmap(chunkRect.Width, chunkRect.Height))
  193. {
  194. using (Graphics g = Graphics.FromImage(b))
  195. {
  196. ThemeManager.DrawProgressBarChunks(g, new Rectangle(0, 0, chunkRect.Width, chunkRect.Height));
  197. }
  198. ControlPaint.DrawImageDisabled(e.Graphics, b, chunkRect.X, chunkRect.Y, this.BackBrush.Color);
  199. }
  200. }
  201. if (this.DrawPercentageText)
  202. {
  203. this.Alignment = ColumnAlignment.Center;
  204. this.LineAlignment = RowAlignment.Center;
  205. Font font = new Font(this.Font.FontFamily, this.Font.SizeInPoints, FontStyle.Bold);
  206. if (e.Enabled)
  207. {
  208. e.Graphics.DrawString("" + intVal + "%", font, SystemBrushes.ControlText, this.ClientRectangle, this.StringFormat);
  209. }
  210. else
  211. {
  212. e.Graphics.DrawString("" + intVal + "%", font, Brushes.White, this.ClientRectangle, this.StringFormat);
  213. }
  214. if (!ThemeManager.VisualStylesEnabled)
  215. {
  216. // remember the old clip area
  217. Region oldClip = e.Graphics.Clip;
  218. Rectangle clipRect = this.ClientRectangle;
  219. clipRect.Width = chunkRect.Width + 2;
  220. e.Graphics.SetClip(clipRect);
  221. if (e.Table.Enabled)
  222. {
  223. e.Graphics.DrawString("" + intVal + "%", font, SystemBrushes.HighlightText, this.ClientRectangle, this.StringFormat);
  224. }
  225. else
  226. {
  227. e.Graphics.DrawString("" + intVal + "%", font, Brushes.White, this.ClientRectangle, this.StringFormat);
  228. }
  229. // restore the old clip area
  230. e.Graphics.SetClip(oldClip, CombineMode.Replace);
  231. }
  232. }
  233. if (e.Focused && e.Enabled)
  234. {
  235. ControlPaint.DrawFocusRectangle(e.Graphics, this.ClientRectangle);
  236. }
  237. }
  238. #endregion
  239. #endregion
  240. }
  241. }