StatisticControl.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. namespace Easychart.Finance.Win
  2. {
  3. using System;
  4. using System.ComponentModel;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7. [ToolboxItem(false)]
  8. public class StatisticControl : Panel
  9. {
  10. public bool AutoHeight = true;
  11. public bool AutoWidth = true;
  12. private Rectangle CloseRect = new Rectangle(4, 4, 10, 10);
  13. public int ColumnSpace = 2;
  14. public int[] ColumnWidth = new int[] { 80, 80 };
  15. private Container components = null;
  16. private Graphics CurrentG;
  17. private string Data;
  18. public bool EnableMove = true;
  19. public Color FrameColor = Color.LightGray;
  20. public Brush[] RowBrushs = new Brush[] { Brushes.Khaki, Brushes.Beige, Brushes.WhiteSmoke };
  21. public int RowHeight = 20;
  22. public int RowSpace = 2;
  23. private string[][] sss;
  24. private PointF StartDrag = PointF.Empty;
  25. public int TitleHeight = 0x12;
  26. public event EventHandler OnHide;
  27. public StatisticControl()
  28. {
  29. this.InitializeComponent();
  30. }
  31. /// <summary>
  32. /// 释放由 <see cref="T:System.ComponentModel.Component" /> 使用的所有资源。
  33. /// </summary>
  34. public new void Dispose()
  35. {
  36. if (components != null) components.Dispose();
  37. components = null;
  38. if (CurrentG != null) CurrentG.Dispose();
  39. CurrentG = null;
  40. RowBrushs = null;
  41. ColumnWidth = null;
  42. sss = null;
  43. base.MouseUp -= this.StatisticControl_MouseUp;
  44. base.Paint -= this.StatisticControl_Paint;
  45. base.MouseMove -= this.StatisticControl_MouseMove;
  46. base.MouseDown -= this.StatisticControl_MouseDown;
  47. base.Dispose();
  48. }
  49. protected override void Dispose(bool disposing)
  50. {
  51. if (disposing && (this.components != null))
  52. {
  53. this.components.Dispose();
  54. }
  55. base.Dispose(disposing);
  56. }
  57. public bool HasData()
  58. {
  59. return ((this.Data != null) && (this.Data != ""));
  60. }
  61. private void HideMe()
  62. {
  63. base.Visible = false;
  64. if (this.OnHide != null)
  65. {
  66. this.OnHide(this, new EventArgs());
  67. }
  68. }
  69. private void InitializeComponent()
  70. {
  71. this.BackColor = SystemColors.Info;
  72. this.Cursor = Cursors.Arrow;
  73. this.ForeColor = Color.Black;
  74. base.Size = new Size(0xd0, 0x158);
  75. base.Visible = false;
  76. base.MouseUp += new MouseEventHandler(this.StatisticControl_MouseUp);
  77. base.Paint += new PaintEventHandler(this.StatisticControl_Paint);
  78. base.MouseMove += new MouseEventHandler(this.StatisticControl_MouseMove);
  79. base.MouseDown += new MouseEventHandler(this.StatisticControl_MouseDown);
  80. }
  81. protected override void OnPaintBackground(PaintEventArgs pevent)
  82. {
  83. if (base.DesignMode || (this.Data == null))
  84. {
  85. base.OnPaintBackground(pevent);
  86. }
  87. }
  88. public void PaintTo(Graphics g)
  89. {
  90. if (base.Visible)
  91. {
  92. this.PaintTo(g, new Rectangle(base.Location.X, base.Location.Y, base.Size.Width, base.Size.Height - this.TitleHeight), false);
  93. }
  94. }
  95. public void PaintTo(Graphics g, Rectangle Rect, bool ShowTitle)
  96. {
  97. if ((this.Data == null) || (this.Data == ""))
  98. {
  99. g.FillRectangle(Brushes.WhiteSmoke, Rect);
  100. }
  101. else
  102. {
  103. if (ShowTitle)
  104. {
  105. g.FillRectangle(Brushes.WhiteSmoke, 0, 0, Rect.Width - 1, this.TitleHeight);
  106. g.DrawRectangle(Pens.Black, 0, 0, Rect.Width - 1, this.TitleHeight);
  107. g.DrawLine(Pens.Black, this.CloseRect.X, this.CloseRect.Y, this.CloseRect.Right, this.CloseRect.Bottom);
  108. g.DrawLine(Pens.Black, this.CloseRect.Right, this.CloseRect.Y, this.CloseRect.Left, this.CloseRect.Bottom);
  109. Rect.Y += this.TitleHeight;
  110. Rect.Height -= this.TitleHeight;
  111. }
  112. Pen pen = new Pen(this.FrameColor);
  113. Rectangle rect = Rect;
  114. rect.Inflate(-1, -1);
  115. g.DrawRectangle(new Pen(Color.Black, 2f), rect);
  116. rect.Height--;
  117. g.SetClip(rect);
  118. int bottom = 0;
  119. for (int i = 0; i < this.sss.Length; i++)
  120. {
  121. for (int j = 0; j < 2; j++)
  122. {
  123. Rectangle rectangle2 = new Rectangle(((this.ColumnSpace + Rect.X) + ((j == 1) ? (this.ColumnWidth[0] + this.ColumnSpace) : 0)) + j, (2 + Rect.Y) + ((this.RowHeight + this.RowSpace) * i), (j == 0) ? (this.ColumnWidth[j] + this.ColumnSpace) : ((rect.Width - this.ColumnWidth[0]) - (this.ColumnSpace * 3)), (this.RowHeight + this.RowSpace) - 1);
  124. bottom = rectangle2.Bottom;
  125. g.DrawRectangle(pen, rectangle2);
  126. g.FillRectangle(this.RowBrushs[i % this.RowBrushs.Length], rectangle2);
  127. StringFormat format = new StringFormat();
  128. format.LineAlignment = StringAlignment.Center;
  129. if (j == 0)
  130. {
  131. format.Alignment = StringAlignment.Far;
  132. }
  133. g.DrawString(this.sss[i][j], this.Font, new SolidBrush(this.ForeColor), rectangle2, format);
  134. }
  135. }
  136. g.FillRectangle(Brushes.WhiteSmoke, 2, bottom + 1, base.Width - 4, (base.Height - bottom) - 1);
  137. }
  138. }
  139. public void RefreshData()
  140. {
  141. this.RefreshData(this.Data);
  142. }
  143. public void RefreshData(string s)
  144. {
  145. this.Data = s;
  146. if (s != null)
  147. {
  148. if (s.EndsWith(";"))
  149. {
  150. s = s.Substring(0, s.Length - 1);
  151. }
  152. string[] strArray = s.Split(new char[] { ';' });
  153. this.sss = new string[strArray.Length][];
  154. for (int i = 0; i < strArray.Length; i++)
  155. {
  156. this.sss[i] = strArray[i].Split(new char[] { '=' });
  157. }
  158. if (this.CurrentG == null)
  159. {
  160. this.CurrentG = base.CreateGraphics();
  161. }
  162. if (this.AutoWidth)
  163. {
  164. this.ColumnWidth[0] = -2147483648;
  165. this.ColumnWidth[1] = -2147483648;
  166. for (int j = 0; j < 2; j++)
  167. {
  168. for (int k = 0; k < strArray.Length; k++)
  169. {
  170. SizeF ef = this.CurrentG.MeasureString(this.sss[k][j], this.Font);
  171. this.ColumnWidth[j] = Math.Max(this.ColumnWidth[j], (int)ef.Width);
  172. }
  173. }
  174. int num4 = (this.ColumnWidth[0] + this.ColumnWidth[1]) + (this.ColumnSpace * 5);
  175. if ((num4 > base.Width) && this.EnableMove)
  176. {
  177. base.Width = num4;
  178. }
  179. }
  180. if (this.AutoHeight)
  181. {
  182. this.RowHeight = (int)this.CurrentG.MeasureString(this.sss[0][0], this.Font, 0x3e8).Height;
  183. if (this.EnableMove)
  184. {
  185. base.Height = (((strArray.Length * (this.RowHeight + this.RowSpace)) + this.TitleHeight) + this.RowSpace) + 2;
  186. }
  187. }
  188. base.Invalidate();
  189. }
  190. }
  191. private void StatisticControl_MouseDown(object sender, MouseEventArgs e)
  192. {
  193. if (this.CloseRect.Contains(e.X, e.Y))
  194. {
  195. this.HideMe();
  196. }
  197. else if (this.EnableMove)
  198. {
  199. this.StartDrag = new PointF((float)e.X, (float)e.Y);
  200. }
  201. }
  202. private void StatisticControl_MouseMove(object sender, MouseEventArgs e)
  203. {
  204. if (this.StartDrag != PointF.Empty)
  205. {
  206. base.Location = Point.Round(new PointF((base.Location.X + e.X) - this.StartDrag.X, (base.Location.Y + e.Y) - this.StartDrag.Y));
  207. }
  208. }
  209. private void StatisticControl_MouseUp(object sender, MouseEventArgs e)
  210. {
  211. this.StartDrag = PointF.Empty;
  212. }
  213. private void StatisticControl_Paint(object sender, PaintEventArgs e)
  214. {
  215. this.PaintTo(e.Graphics, base.ClientRectangle, true);
  216. }
  217. }
  218. }