GradientHeaderRenderer.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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.Drawing.Drawing2D;
  34. using System.Windows.Forms;
  35. using XPTable.Events;
  36. using XPTable.Models;
  37. using XPTable.Themes;
  38. namespace XPTable.Renderers
  39. {
  40. /// <summary>
  41. /// A HeaderRenderer that draws gradient Column headers
  42. /// </summary>
  43. public class GradientHeaderRenderer : HeaderRenderer
  44. {
  45. #region Class Data
  46. /// <summary>
  47. /// The start Color of the gradient
  48. /// </summary>
  49. private Color startColor;
  50. /// <summary>
  51. /// The ned Color of the gradient
  52. /// </summary>
  53. private Color endColor;
  54. /// <summary>
  55. /// The Color of the Column header when it is pressed
  56. /// </summary>
  57. private Color pressedColor;
  58. #endregion
  59. #region Constructor
  60. /// <summary>
  61. /// Initializes a new instance of the GradientHeaderRenderer class
  62. /// with default settings
  63. /// </summary>
  64. public GradientHeaderRenderer() : base()
  65. {
  66. // steel blue gradient
  67. this.startColor = Color.FromArgb(200, 209, 215);
  68. this.endColor = Color.FromArgb(239, 239, 239);
  69. this.pressedColor = Color.Empty;
  70. }
  71. #endregion
  72. #region Properties
  73. /// <summary>
  74. /// Gets or sets the start Color of the gradient
  75. /// </summary>
  76. [Category("Appearance"),
  77. Description("The start color of a ColumnHeaders gradient")]
  78. public Color StartColor
  79. {
  80. get
  81. {
  82. return this.startColor;
  83. }
  84. set
  85. {
  86. if (this.startColor != value)
  87. {
  88. this.startColor = value;
  89. }
  90. }
  91. }
  92. /// <summary>
  93. /// Gets or sets the end Color of the gradient
  94. /// </summary>
  95. [Category("Appearance"),
  96. Description("The end color of a ColumnHeaders gradient")]
  97. public Color EndColor
  98. {
  99. get
  100. {
  101. return this.endColor;
  102. }
  103. set
  104. {
  105. if (this.endColor != value)
  106. {
  107. this.endColor = value;
  108. }
  109. }
  110. }
  111. /// <summary>
  112. /// Gets or sets the Color of the Column header when it is pressed
  113. /// </summary>
  114. [Category("Appearance"),
  115. Description("The color of a ColumnHeader when it is in a pressed state")]
  116. public Color PressedColor
  117. {
  118. get
  119. {
  120. return this.pressedColor;
  121. }
  122. set
  123. {
  124. if (this.pressedColor != value)
  125. {
  126. this.pressedColor = value;
  127. }
  128. }
  129. }
  130. #endregion
  131. #region Events
  132. #region Paint
  133. /// <summary>
  134. /// Raises the PaintBackground event
  135. /// </summary>
  136. /// <param name="e">A PaintHeaderEventArgs that contains the event data</param>
  137. protected override void OnPaintBackground(PaintHeaderEventArgs e)
  138. {
  139. base.OnPaintBackground(e);
  140. if (e.Column == null || e.Column.ColumnState != ColumnState.Pressed)
  141. {
  142. using (LinearGradientBrush brush = new LinearGradientBrush(e.HeaderRect, this.StartColor, this.EndColor, LinearGradientMode.Vertical))
  143. {
  144. e.Graphics.FillRectangle(brush, e.HeaderRect);
  145. }
  146. using (Pen pen = new Pen(this.EndColor))
  147. {
  148. e.Graphics.DrawLine(pen, e.HeaderRect.Left, e.HeaderRect.Top, e.HeaderRect.Right-2, e.HeaderRect.Top);
  149. e.Graphics.DrawLine(pen, e.HeaderRect.Left, e.HeaderRect.Top, e.HeaderRect.Left, e.HeaderRect.Bottom-1);
  150. }
  151. using (Pen pen = new Pen(this.StartColor))
  152. {
  153. e.Graphics.DrawLine(pen, e.HeaderRect.Right-1, e.HeaderRect.Top, e.HeaderRect.Right-1, e.HeaderRect.Bottom-1);
  154. e.Graphics.DrawLine(pen, e.HeaderRect.Left+1, e.HeaderRect.Bottom-1, e.HeaderRect.Right-1, e.HeaderRect.Bottom-1);
  155. }
  156. }
  157. else
  158. {
  159. Color pressed = this.PressedColor;
  160. if (pressed == Color.Empty)
  161. {
  162. pressed = ControlPaint.Light(this.startColor);
  163. }
  164. using (SolidBrush brush = new SolidBrush(pressed))
  165. {
  166. e.Graphics.FillRectangle(brush, e.HeaderRect);
  167. }
  168. using (Pen pen = new Pen(this.StartColor))
  169. {
  170. e.Graphics.DrawRectangle(pen, e.HeaderRect.X, e.HeaderRect.Y, e.HeaderRect.Width-1, e.HeaderRect.Height-1);
  171. }
  172. }
  173. }
  174. /// <summary>
  175. /// Raises the Paint event
  176. /// </summary>
  177. /// <param name="e">A PaintHeaderEventArgs that contains the event data</param>
  178. protected override void OnPaint(PaintHeaderEventArgs e)
  179. {
  180. base.OnPaint(e);
  181. if (e.Column == null)
  182. {
  183. return;
  184. }
  185. Rectangle textRect = this.ClientRectangle;
  186. Rectangle imageRect = Rectangle.Empty;
  187. if (e.Column.Image != null)
  188. {
  189. imageRect = this.CalcImageRect();
  190. textRect.Width -= imageRect.Width;
  191. textRect.X += imageRect.Width;
  192. if (e.Column.ImageOnRight)
  193. {
  194. imageRect.X = this.ClientRectangle.Right - imageRect.Width;
  195. textRect.X = this.ClientRectangle.X;
  196. }
  197. if (e.Column.ColumnState == ColumnState.Pressed)
  198. {
  199. imageRect.X += 1;
  200. imageRect.Y += 1;
  201. }
  202. this.DrawColumnHeaderImage(e.Graphics, e.Column.Image, imageRect, e.Column.Enabled);
  203. }
  204. if (e.Column.ColumnState == ColumnState.Pressed)
  205. {
  206. textRect.X += 1;
  207. textRect.Y += 1;
  208. }
  209. if (e.Column.SortOrder != SortOrder.None)
  210. {
  211. Rectangle arrowRect = this.CalcSortArrowRect();
  212. arrowRect.X = textRect.Right - arrowRect.Width;
  213. textRect.Width -= arrowRect.Width;
  214. this.DrawSortArrow(e.Graphics, arrowRect, e.Column.SortOrder, e.Column.Enabled);
  215. }
  216. if (e.Column.Text == null)
  217. {
  218. return;
  219. }
  220. if (e.Column.Text.Length > 0 && textRect.Width > 0)
  221. {
  222. if (e.Column.Enabled)
  223. {
  224. e.Graphics.DrawString(e.Column.Text, this.Font, this.ForeBrush, textRect, this.StringFormat);
  225. }
  226. else
  227. {
  228. using (SolidBrush brush = new SolidBrush(SystemPens.GrayText.Color))
  229. {
  230. e.Graphics.DrawString(e.Column.Text, this.Font, brush, textRect, this.StringFormat);
  231. }
  232. }
  233. }
  234. }
  235. #endregion
  236. #endregion
  237. }
  238. }