StatisticControl.cs 8.1 KB

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