XPHeaderRenderer.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.Windows.Forms;
  33. using XPTable.Events;
  34. using XPTable.Models;
  35. using XPTable.Themes;
  36. namespace XPTable.Renderers
  37. {
  38. /// <summary>
  39. /// A HeaderRenderer that draws Windows XP themed Column headers
  40. /// </summary>
  41. public class XPHeaderRenderer : HeaderRenderer
  42. {
  43. #region Constructor
  44. /// <summary>
  45. /// Initializes a new instance of the XPHeaderRenderer class
  46. /// with default settings
  47. /// </summary>
  48. public XPHeaderRenderer() : base()
  49. {
  50. }
  51. #endregion
  52. #region Events
  53. #region Paint
  54. /// <summary>
  55. /// Raises the PaintBackground event
  56. /// </summary>
  57. /// <param name="e">A PaintHeaderEventArgs that contains the event data</param>
  58. protected override void OnPaintBackground(PaintHeaderEventArgs e)
  59. {
  60. base.OnPaintBackground(e);
  61. if (e.Column == null)
  62. {
  63. ThemeManager.DrawColumnHeader(e.Graphics, e.HeaderRect, ColumnHeaderStates.Normal);
  64. }
  65. else
  66. {
  67. ThemeManager.DrawColumnHeader(e.Graphics, e.HeaderRect, (ColumnHeaderStates) e.Column.ColumnState);
  68. }
  69. }
  70. /// <summary>
  71. /// Raises the Paint event
  72. /// </summary>
  73. /// <param name="e">A PaintHeaderEventArgs that contains the event data</param>
  74. protected override void OnPaint(PaintHeaderEventArgs e)
  75. {
  76. base.OnPaint(e);
  77. if (e.Column == null)
  78. {
  79. return;
  80. }
  81. Rectangle textRect = this.ClientRectangle;
  82. Rectangle imageRect = Rectangle.Empty;
  83. if (e.Column.Image != null)
  84. {
  85. imageRect = this.CalcImageRect();
  86. textRect.Width -= imageRect.Width;
  87. textRect.X += imageRect.Width;
  88. if (e.Column.ImageOnRight)
  89. {
  90. imageRect.X = this.ClientRectangle.Right - imageRect.Width;
  91. textRect.X = this.ClientRectangle.X;
  92. }
  93. if (!ThemeManager.VisualStylesEnabled && e.Column.ColumnState == ColumnState.Pressed)
  94. {
  95. imageRect.X += 1;
  96. imageRect.Y += 1;
  97. }
  98. this.DrawColumnHeaderImage(e.Graphics, e.Column.Image, imageRect, e.Column.Enabled);
  99. }
  100. if (!ThemeManager.VisualStylesEnabled && e.Column.ColumnState == ColumnState.Pressed)
  101. {
  102. textRect.X += 1;
  103. textRect.Y += 1;
  104. }
  105. if (e.Column.SortOrder != SortOrder.None)
  106. {
  107. Rectangle arrowRect = this.CalcSortArrowRect();
  108. arrowRect.X = textRect.Right - arrowRect.Width;
  109. textRect.Width -= arrowRect.Width;
  110. this.DrawSortArrow(e.Graphics, arrowRect, e.Column.SortOrder, e.Column.Enabled);
  111. }
  112. if (e.Column.Text == null)
  113. {
  114. return;
  115. }
  116. if (e.Column.Text.Length > 0 && textRect.Width > 0)
  117. {
  118. if (e.Column.Enabled)
  119. {
  120. e.Graphics.DrawString(e.Column.Text, this.Font, this.ForeBrush, textRect, this.StringFormat);
  121. }
  122. else
  123. {
  124. using (SolidBrush brush = new SolidBrush(SystemPens.GrayText.Color))
  125. {
  126. e.Graphics.DrawString(e.Column.Text, this.Font, brush, textRect, this.StringFormat);
  127. }
  128. }
  129. }
  130. }
  131. #endregion
  132. #endregion
  133. }
  134. }