ImageCellRenderer.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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.ComponentModel;
  32. using System.Drawing;
  33. using System.Globalization;
  34. using System.Windows.Forms;
  35. using XPTable.Events;
  36. using XPTable.Models;
  37. namespace XPTable.Renderers
  38. {
  39. /// <summary>
  40. /// A CellRenderer that draws Cell contents as Images
  41. /// </summary>
  42. public class ImageCellRenderer : CellRenderer
  43. {
  44. #region Class Data
  45. /// <summary>
  46. /// Specifies whether any text contained in the Cell should be drawn
  47. /// </summary>
  48. private bool drawText;
  49. #endregion
  50. #region Constructor
  51. /// <summary>
  52. /// Initializes a new instance of the ImageCellRenderer class with
  53. /// default settings
  54. /// </summary>
  55. public ImageCellRenderer() : base()
  56. {
  57. this.drawText = true;
  58. }
  59. #endregion
  60. #region Methods
  61. /// <summary>
  62. /// Gets the Rectangle that specifies the Size and Location of
  63. /// the Image contained in the current Cell
  64. /// </summary>
  65. /// <param name="image">The Image to be drawn</param>
  66. /// <param name="sizeMode">An ImageSizeMode that specifies how the
  67. /// specified Image is scaled</param>
  68. /// <param name="rowAlignment">The alignment of the current Cell's row</param>
  69. /// <param name="columnAlignment">The alignment of the current Cell's Column</param>
  70. /// <returns>A Rectangle that specifies the Size and Location of
  71. /// the Image contained in the current Cell</returns>
  72. protected Rectangle CalcImageRect(Image image, ImageSizeMode sizeMode, RowAlignment rowAlignment, ColumnAlignment columnAlignment)
  73. {
  74. if (this.DrawText)
  75. {
  76. sizeMode = ImageSizeMode.ScaledToFit;
  77. }
  78. Rectangle imageRect = this.ClientRectangle;
  79. if (sizeMode == ImageSizeMode.Normal)
  80. {
  81. if (image.Width < imageRect.Width)
  82. {
  83. imageRect.Width = image.Width;
  84. }
  85. if (image.Height < imageRect.Height)
  86. {
  87. imageRect.Height = image.Height;
  88. }
  89. }
  90. else if (sizeMode == ImageSizeMode.ScaledToFit)
  91. {
  92. if (image.Width >= imageRect.Width || image.Height >= imageRect.Height)
  93. {
  94. double hScale = ((double) imageRect.Width) / ((double) image.Width);
  95. double vScale = ((double) imageRect.Height) / ((double) image.Height);
  96. double scale = Math.Min(hScale, vScale);
  97. imageRect.Width = (int) (((double) image.Width) * scale);
  98. imageRect.Height = (int) (((double) image.Height) * scale);
  99. }
  100. else
  101. {
  102. imageRect.Width = image.Width;
  103. imageRect.Height = image.Height;
  104. }
  105. }
  106. if (rowAlignment == RowAlignment.Center)
  107. {
  108. imageRect.Y += (this.ClientRectangle.Height - imageRect.Height) / 2;
  109. }
  110. else if (rowAlignment == RowAlignment.Bottom)
  111. {
  112. imageRect.Y = this.ClientRectangle.Bottom - imageRect.Height;
  113. }
  114. if (!this.DrawText)
  115. {
  116. if (columnAlignment == ColumnAlignment.Center)
  117. {
  118. imageRect.X += (this.ClientRectangle.Width - imageRect.Width) / 2;
  119. }
  120. else if (columnAlignment == ColumnAlignment.Right)
  121. {
  122. imageRect.X = this.ClientRectangle.Width - imageRect.Width;
  123. }
  124. }
  125. return imageRect;
  126. }
  127. #endregion
  128. #region Properties
  129. /// <summary>
  130. /// Gets or sets whether any text contained in the Cell should be drawn
  131. /// </summary>
  132. public bool DrawText
  133. {
  134. get
  135. {
  136. return this.drawText;
  137. }
  138. }
  139. #endregion
  140. #region Events
  141. #region Paint
  142. /// <summary>
  143. /// Raises the PaintCell event
  144. /// </summary>
  145. /// <param name="e">A PaintCellEventArgs that contains the event data</param>
  146. public override void OnPaintCell(PaintCellEventArgs e)
  147. {
  148. if (e.Table.ColumnModel.Columns[e.Column] is ImageColumn)
  149. {
  150. this.drawText = ((ImageColumn) e.Table.ColumnModel.Columns[e.Column]).DrawText;
  151. }
  152. else
  153. {
  154. this.drawText = true;
  155. }
  156. base.OnPaintCell(e);
  157. }
  158. /// <summary>
  159. /// Raises the Paint event
  160. /// </summary>
  161. /// <param name="e">A PaintCellEventArgs that contains the event data</param>
  162. protected override void OnPaint(PaintCellEventArgs e)
  163. {
  164. base.OnPaint(e);
  165. // don't bother if the Cell is null or doesn't have an image
  166. if (e.Cell == null || e.Cell.Image == null)
  167. {
  168. return;
  169. }
  170. // work out the size and location of the image
  171. Rectangle imageRect = this.CalcImageRect(e.Cell.Image, e.Cell.ImageSizeMode, this.LineAlignment, this.Alignment);
  172. // draw the image
  173. bool scaled = (this.DrawText || e.Cell.ImageSizeMode != ImageSizeMode.Normal);
  174. this.DrawImage(e.Graphics, e.Cell.Image, imageRect, scaled, e.Table.Enabled);
  175. // check if we need to draw any text
  176. if (this.DrawText)
  177. {
  178. if (e.Cell.Text != null && e.Cell.Text.Length != 0)
  179. {
  180. // rectangle the text will be drawn in
  181. Rectangle textRect = this.ClientRectangle;
  182. // take the imageRect into account so we don't
  183. // draw over it
  184. textRect.X += imageRect.Width;
  185. textRect.Width -= imageRect.Width;
  186. // check that we will be able to see the text
  187. if (textRect.Width > 0)
  188. {
  189. // draw the text
  190. if (e.Enabled)
  191. {
  192. e.Graphics.DrawString(e.Cell.Text, this.Font, this.ForeBrush, textRect, this.StringFormat);
  193. }
  194. else
  195. {
  196. e.Graphics.DrawString(e.Cell.Text, this.Font, this.GrayTextBrush, textRect, this.StringFormat);
  197. }
  198. }
  199. }
  200. }
  201. if (e.Focused && e.Enabled)
  202. {
  203. ControlPaint.DrawFocusRectangle(e.Graphics, this.ClientRectangle);
  204. }
  205. }
  206. /// <summary>
  207. /// Draws the Image contained in the Cell
  208. /// </summary>
  209. /// <param name="g">The Graphics used to paint the Image</param>
  210. /// <param name="image">The Image to be drawn</param>
  211. /// <param name="imageRect">A rectangle that specifies the Size and
  212. /// Location of the Image</param>
  213. /// <param name="scaled">Specifies whether the image is to be scaled</param>
  214. /// <param name="enabled">Specifies whether the Image should be drawn
  215. /// in an enabled state</param>
  216. protected void DrawImage(Graphics g, Image image, Rectangle imageRect, bool scaled, bool enabled)
  217. {
  218. if (scaled)
  219. {
  220. if (enabled)
  221. {
  222. g.DrawImage(image, imageRect);
  223. }
  224. else
  225. {
  226. using (Image im = new Bitmap(image, imageRect.Width, imageRect.Height))
  227. {
  228. ControlPaint.DrawImageDisabled(g, im, imageRect.X, imageRect.Y, this.BackBrush.Color);
  229. }
  230. }
  231. }
  232. else
  233. {
  234. if (enabled)
  235. {
  236. g.DrawImageUnscaled(image, imageRect);
  237. }
  238. else
  239. {
  240. ControlPaint.DrawImageDisabled(g, image, imageRect.X, imageRect.Y, this.BackBrush.Color);
  241. }
  242. }
  243. }
  244. #endregion
  245. #endregion
  246. }
  247. }