DateTimeCellRenderer.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. using XPTable.Models;
  36. using XPTable.Themes;
  37. namespace XPTable.Renderers
  38. {
  39. /// <summary>
  40. /// A CellRenderer that draws Cell contents as a DateTime
  41. /// </summary>
  42. public class DateTimeCellRenderer : DropDownCellRenderer
  43. {
  44. #region Class Data
  45. /// <summary>
  46. /// The format of the date and time displayed in the Cell
  47. /// </summary>
  48. private DateTimePickerFormat dateFormat;
  49. #endregion
  50. #region Constructor
  51. /// <summary>
  52. /// Initializes a new instance of the DateTimeCellRenderer class with
  53. /// default settings
  54. /// </summary>
  55. public DateTimeCellRenderer() : base()
  56. {
  57. this.dateFormat = DateTimePickerFormat.Long;
  58. this.Format = DateTimeColumn.LongDateFormat;
  59. }
  60. #endregion
  61. #region Properties
  62. /// <summary>
  63. /// Gets or sets the format of the date and time displayed in the Cell
  64. /// </summary>
  65. public DateTimePickerFormat DateTimeFormat
  66. {
  67. get
  68. {
  69. return this.dateFormat;
  70. }
  71. set
  72. {
  73. if (!Enum.IsDefined(typeof(DateTimePickerFormat), value))
  74. {
  75. throw new InvalidEnumArgumentException("value", (int) value, typeof(DateTimePickerFormat));
  76. }
  77. this.dateFormat = value;
  78. }
  79. }
  80. #endregion
  81. #region Events
  82. #region Paint
  83. /// <summary>
  84. /// Raises the PaintCell event
  85. /// </summary>
  86. /// <param name="e">A PaintCellEventArgs that contains the event data</param>
  87. public override void OnPaintCell(PaintCellEventArgs e)
  88. {
  89. if (e.Table.ColumnModel.Columns[e.Column] is DateTimeColumn)
  90. {
  91. DateTimeColumn column = (DateTimeColumn) e.Table.ColumnModel.Columns[e.Column];
  92. this.DateTimeFormat = column.DateTimeFormat;
  93. this.Format = column.CustomDateTimeFormat;
  94. }
  95. else
  96. {
  97. this.DateTimeFormat = DateTimePickerFormat.Long;
  98. this.Format = "";
  99. }
  100. base.OnPaintCell(e);
  101. }
  102. /// <summary>
  103. /// Raises the Paint event
  104. /// </summary>
  105. /// <param name="e">A PaintCellEventArgs that contains the event data</param>
  106. protected override void OnPaint(PaintCellEventArgs e)
  107. {
  108. base.OnPaint(e);
  109. // don't bother going any further if the Cell is null
  110. // or doesn't contain any data
  111. if (e.Cell == null || e.Cell.Data == null || !(e.Cell.Data is DateTime))
  112. {
  113. return;
  114. }
  115. Rectangle buttonRect = this.CalcDropDownButtonBounds();
  116. Rectangle textRect = this.ClientRectangle;
  117. if (this.ShowDropDownButton)
  118. {
  119. textRect.Width -= buttonRect.Width - 1;
  120. }
  121. // draw the text
  122. if (e.Enabled)
  123. {
  124. this.DrawText((DateTime) e.Cell.Data, e.Graphics, this.ForeBrush, textRect);
  125. }
  126. else
  127. {
  128. this.DrawText((DateTime) e.Cell.Data, e.Graphics, this.GrayTextBrush, textRect);
  129. }
  130. if (e.Focused && e.Enabled)
  131. {
  132. Rectangle focusRect = this.ClientRectangle;
  133. if (this.ShowDropDownButton)
  134. {
  135. focusRect.Width -= buttonRect.Width;
  136. }
  137. ControlPaint.DrawFocusRectangle(e.Graphics, focusRect);
  138. }
  139. }
  140. /// <summary>
  141. /// Draws the DateTime text
  142. /// </summary>
  143. /// <param name="dateTime">The DateTime value to be drawn</param>
  144. /// <param name="g">The Graphics to draw on</param>
  145. /// <param name="brush">The Brush to draw the text with</param>
  146. /// <param name="textRect">A Rectangle that specifies the bounds of the text</param>
  147. protected void DrawText(DateTime dateTime, Graphics g, Brush brush, Rectangle textRect)
  148. {
  149. // get the custom format
  150. string format = this.Format;
  151. // if a custom format hasn't been defined, use
  152. // one of the default formats
  153. if (format.Length == 0)
  154. {
  155. switch (this.DateTimeFormat)
  156. {
  157. case DateTimePickerFormat.Long:
  158. format = DateTimeColumn.LongDateFormat;
  159. break;
  160. case DateTimePickerFormat.Short:
  161. format = DateTimeColumn.ShortDateFormat;
  162. break;
  163. case DateTimePickerFormat.Time:
  164. format = DateTimeColumn.TimeFormat;
  165. break;
  166. }
  167. }
  168. g.DrawString(dateTime.ToString(format), this.Font, brush, textRect, this.StringFormat);
  169. }
  170. #endregion
  171. #endregion
  172. }
  173. }