HeaderRenderer.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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.Windows.Forms;
  34. using XPTable.Events;
  35. namespace XPTable.Renderers
  36. {
  37. /// <summary>
  38. /// Base class for Renderers that draw Column headers
  39. /// </summary>
  40. public abstract class HeaderRenderer : Renderer, IHeaderRenderer
  41. {
  42. #region Constructor
  43. /// <summary>
  44. /// Initializes a new instance of the HeaderRenderer class with default settings
  45. /// </summary>
  46. protected HeaderRenderer() : base()
  47. {
  48. this.StringFormat.Alignment = StringAlignment.Near;
  49. }
  50. #endregion
  51. #region Methods
  52. /// <summary>
  53. /// Returns a Rectangle that represents the size and location of the Image
  54. /// displayed on the ColumnHeader
  55. /// </summary>
  56. /// <returns>A Rectangle that represents the size and location of the Image
  57. /// displayed on the ColumnHeader</returns>
  58. protected Rectangle CalcImageRect()
  59. {
  60. Rectangle imageRect = this.ClientRectangle;
  61. if (imageRect.Width > 16)
  62. {
  63. imageRect.Width = 16;
  64. }
  65. if (imageRect.Height > 16)
  66. {
  67. imageRect.Height = 16;
  68. imageRect.Y += (this.ClientRectangle.Height - imageRect.Height) / 2;
  69. }
  70. return imageRect;
  71. }
  72. /// <summary>
  73. /// Returns a Rectangle that represents the size and location of the sort arrow
  74. /// </summary>
  75. /// <returns>A Rectangle that represents the size and location of the sort arrow</returns>
  76. protected Rectangle CalcSortArrowRect()
  77. {
  78. Rectangle arrowRect = this.ClientRectangle;
  79. arrowRect.Width = 12;
  80. arrowRect.X = this.ClientRectangle.Right - arrowRect.Width;
  81. return arrowRect;
  82. }
  83. #endregion
  84. #region Properties
  85. /// <summary>
  86. /// Overrides Renderer.ClientRectangle
  87. /// </summary>
  88. [Browsable(false)]
  89. public override Rectangle ClientRectangle
  90. {
  91. get
  92. {
  93. Rectangle client = new Rectangle(this.Bounds.Location, this.Bounds.Size);
  94. //
  95. client.Inflate(-2, -2);
  96. return client;
  97. }
  98. }
  99. #endregion
  100. #region Events
  101. #region Mouse
  102. #region MouseEnter
  103. /// <summary>
  104. /// Raises the MouseEnter event
  105. /// </summary>
  106. /// <param name="e">A HeaderMouseEventArgs that contains the event data</param>
  107. public virtual void OnMouseEnter(HeaderMouseEventArgs e)
  108. {
  109. this.Bounds = e.HeaderRect;
  110. bool tooltipActive = e.Table.ToolTip.Active;
  111. if (tooltipActive)
  112. {
  113. e.Table.ToolTip.Active = false;
  114. }
  115. e.Table.ResetMouseEventArgs();
  116. e.Table.ToolTip.SetToolTip(e.Table, e.Column.ToolTipText);
  117. if (tooltipActive)
  118. {
  119. e.Table.ToolTip.Active = true;
  120. }
  121. }
  122. #endregion
  123. #region MouseLeave
  124. /// <summary>
  125. /// Raises the MouseLeave event
  126. /// </summary>
  127. /// <param name="e">A HeaderMouseEventArgs that contains the event data</param>
  128. public virtual void OnMouseLeave(HeaderMouseEventArgs e)
  129. {
  130. this.Bounds = e.HeaderRect;
  131. }
  132. #endregion
  133. #region MouseUp
  134. /// <summary>
  135. /// Raises the MouseUp event
  136. /// </summary>
  137. /// <param name="e">A HeaderMouseEventArgs that contains the event data</param>
  138. public virtual void OnMouseUp(HeaderMouseEventArgs e)
  139. {
  140. this.Bounds = e.HeaderRect;
  141. }
  142. #endregion
  143. #region MouseDown
  144. /// <summary>
  145. /// Raises the MouseDown event
  146. /// </summary>
  147. /// <param name="e">A HeaderMouseEventArgs that contains the event data</param>
  148. public virtual void OnMouseDown(HeaderMouseEventArgs e)
  149. {
  150. if (!e.Table.Focused)
  151. {
  152. e.Table.Focus();
  153. }
  154. this.Bounds = e.HeaderRect;
  155. }
  156. #endregion
  157. #region MouseMove
  158. /// <summary>
  159. /// Raises the MouseMove event
  160. /// </summary>
  161. /// <param name="e">A HeaderMouseEventArgs that contains the event data</param>
  162. public virtual void OnMouseMove(HeaderMouseEventArgs e)
  163. {
  164. this.Bounds = e.HeaderRect;
  165. }
  166. #endregion
  167. #region Click
  168. /// <summary>
  169. /// Raises the Click event
  170. /// </summary>
  171. /// <param name="e">A HeaderMouseEventArgs that contains the event data</param>
  172. public virtual void OnClick(HeaderMouseEventArgs e)
  173. {
  174. this.Bounds = e.HeaderRect;
  175. }
  176. /// <summary>
  177. /// Raises the DoubleClick event
  178. /// </summary>
  179. /// <param name="e">A HeaderMouseEventArgs that contains the event data</param>
  180. public virtual void OnDoubleClick(HeaderMouseEventArgs e)
  181. {
  182. this.Bounds = e.HeaderRect;
  183. }
  184. #endregion
  185. #endregion
  186. #region Paint
  187. /// <summary>
  188. /// Raises the PaintHeader event
  189. /// </summary>
  190. /// <param name="e">A PaintHeaderEventArgs that contains the event data</param>
  191. public virtual void OnPaintHeader(PaintHeaderEventArgs e)
  192. {
  193. // paint the Column header's background
  194. this.OnPaintBackground(e);
  195. // paint the Column headers foreground
  196. this.OnPaint(e);
  197. }
  198. /// <summary>
  199. /// Raises the PaintBackground event
  200. /// </summary>
  201. /// <param name="e">A PaintHeaderEventArgs that contains the event data</param>
  202. protected virtual void OnPaintBackground(PaintHeaderEventArgs e)
  203. {
  204. }
  205. /// <summary>
  206. /// Raises the Paint event
  207. /// </summary>
  208. /// <param name="e">A PaintHeaderEventArgs that contains the event data</param>
  209. protected virtual void OnPaint(PaintHeaderEventArgs e)
  210. {
  211. }
  212. /// <summary>
  213. /// Draws the Image contained in the ColumnHeader
  214. /// </summary>
  215. /// <param name="g">The Graphics used to paint the Image</param>
  216. /// <param name="image">The Image to be drawn</param>
  217. /// <param name="imageRect">A rectangle that specifies the Size and
  218. /// Location of the Image</param>
  219. /// <param name="enabled">Specifies whether the Image should be drawn
  220. /// in an enabled state</param>
  221. protected void DrawColumnHeaderImage(Graphics g, Image image, Rectangle imageRect, bool enabled)
  222. {
  223. if (enabled)
  224. {
  225. g.DrawImage(image, imageRect);
  226. }
  227. else
  228. {
  229. using (Image im = new Bitmap(image, imageRect.Width, imageRect.Height))
  230. {
  231. ControlPaint.DrawImageDisabled(g, im, imageRect.X, imageRect.Y, this.BackBrush.Color);
  232. }
  233. }
  234. }
  235. /// <summary>
  236. /// Draws the ColumnHeader's sort arrow
  237. /// </summary>
  238. /// <param name="g">The Graphics to draw on</param>
  239. /// <param name="drawRect">A Rectangle that specifies the location
  240. /// of the sort arrow</param>
  241. /// <param name="direction">The direction of the sort arrow</param>
  242. /// <param name="enabled">Specifies whether the sort arrow should be
  243. /// drawn in an enabled state</param>
  244. protected virtual void DrawSortArrow(Graphics g, Rectangle drawRect, SortOrder direction, bool enabled)
  245. {
  246. if (direction != SortOrder.None)
  247. {
  248. using (Font font = new Font("Marlett", 9f))
  249. {
  250. using (StringFormat format = new StringFormat())
  251. {
  252. format.Alignment = StringAlignment.Far;
  253. format.LineAlignment = StringAlignment.Center;
  254. if (direction == SortOrder.Ascending)
  255. {
  256. if (enabled)
  257. {
  258. g.DrawString("t", font, SystemBrushes.ControlDarkDark, drawRect, format);
  259. }
  260. else
  261. {
  262. using (SolidBrush brush = new SolidBrush(SystemPens.GrayText.Color))
  263. {
  264. g.DrawString("t", font, brush, drawRect, format);
  265. }
  266. }
  267. }
  268. else
  269. {
  270. if (enabled)
  271. {
  272. g.DrawString("u", font, SystemBrushes.ControlDarkDark, drawRect, format);
  273. }
  274. else
  275. {
  276. using (SolidBrush brush = new SolidBrush(SystemPens.GrayText.Color))
  277. {
  278. g.DrawString("u", font, brush, drawRect, format);
  279. }
  280. }
  281. }
  282. }
  283. }
  284. }
  285. }
  286. #endregion
  287. #endregion
  288. }
  289. }