ObjectLabel.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. namespace IndexFormula.Finance
  2. {
  3. using System;
  4. using System.Collections;
  5. using System.Drawing;
  6. using System.Drawing.Text;
  7. public class ObjectLabel
  8. {
  9. public Color BackColor;
  10. public Color BorderColor;
  11. public StringFormat format = new StringFormat(StringFormat.GenericDefault);
  12. public int Left;
  13. public int RoundWidth = 4;
  14. public int ShadowWidth = 2;
  15. public IndexFormula.Finance.StickAlignment StickAlignment;
  16. public int StickHeight = 6;
  17. public string Text;
  18. public Brush TextBrush = Brushes.Black;
  19. public Font TextFont = new Font("Verdana", 8f);
  20. public int Top;
  21. public ObjectLabel()
  22. {
  23. this.format.Alignment = StringAlignment.Center;
  24. this.format.LineAlignment = StringAlignment.Center;
  25. }
  26. public void Draw(Graphics g)
  27. {
  28. SizeF ef = g.MeasureString(this.Text, this.TextFont, 0x3e8, this.format);
  29. RectangleF layoutRectangle = new RectangleF((float) this.Left, (float) this.Top, ef.Width + 4f, ef.Height + 4f);
  30. ArrayList al = new ArrayList();
  31. al.Add(new PointF(layoutRectangle.Left, layoutRectangle.Top + this.RoundWidth));
  32. al.Add(new PointF(layoutRectangle.Left + this.RoundWidth, layoutRectangle.Top));
  33. al.Add(new PointF(layoutRectangle.Right - this.RoundWidth, layoutRectangle.Top));
  34. al.Add(new PointF(layoutRectangle.Right, layoutRectangle.Top + this.RoundWidth));
  35. al.Add(new PointF(layoutRectangle.Right, layoutRectangle.Bottom - this.RoundWidth));
  36. al.Add(new PointF(layoutRectangle.Right - this.RoundWidth, layoutRectangle.Bottom));
  37. al.Add(new PointF(layoutRectangle.Left + this.RoundWidth, layoutRectangle.Bottom));
  38. al.Add(new PointF(layoutRectangle.Left, layoutRectangle.Bottom - this.RoundWidth));
  39. al.Add(al[0]);
  40. PointF tf = new PointF((float) this.Left, (float) this.Top);
  41. if (this.StickHeight > 0)
  42. {
  43. float offsetX = 0f;
  44. float offsetY = 0f;
  45. int index = 1;
  46. switch (this.StickAlignment)
  47. {
  48. case IndexFormula.Finance.StickAlignment.LeftTop:
  49. offsetX = this.StickHeight;
  50. offsetY = this.StickHeight;
  51. break;
  52. case IndexFormula.Finance.StickAlignment.LeftBottom:
  53. offsetX = this.StickHeight;
  54. offsetY = -layoutRectangle.Height - this.StickHeight;
  55. index = 7;
  56. break;
  57. case IndexFormula.Finance.StickAlignment.RightTop:
  58. offsetX = -layoutRectangle.Width - this.StickHeight;
  59. offsetY = this.StickHeight;
  60. index = 3;
  61. break;
  62. case IndexFormula.Finance.StickAlignment.RightBottom:
  63. offsetX = -layoutRectangle.Width - this.StickHeight;
  64. offsetY = -layoutRectangle.Height - this.StickHeight;
  65. index = 5;
  66. break;
  67. }
  68. al = this.OffsetPoint(al, offsetX, offsetY);
  69. al.Insert(index, tf);
  70. layoutRectangle.Offset(offsetX, offsetY);
  71. }
  72. PointF[] points = (PointF[]) al.ToArray(typeof(PointF));
  73. if (this.ShadowWidth > 0)
  74. {
  75. PointF[] pfs = (PointF[]) points.Clone();
  76. this.OffsetPoint(pfs, (float) this.ShadowWidth, (float) this.ShadowWidth);
  77. g.FillPolygon(new SolidBrush(Color.FromArgb(0x40, Color.Black)), pfs);
  78. }
  79. if (this.BackColor != Color.Empty)
  80. {
  81. g.FillPolygon(new SolidBrush(this.BackColor), points);
  82. }
  83. if (this.BorderColor != Color.Empty)
  84. {
  85. g.DrawLines(new Pen(this.BorderColor), points);
  86. }
  87. TextRenderingHint textRenderingHint = g.TextRenderingHint;
  88. g.TextRenderingHint = TextRenderingHint.AntiAlias;
  89. g.DrawString(this.Text, this.TextFont, this.TextBrush, layoutRectangle, this.format);
  90. g.TextRenderingHint = textRenderingHint;
  91. }
  92. public ArrayList OffsetPoint(ArrayList al, float OffsetX, float OffsetY)
  93. {
  94. PointF[] pfs = (PointF[]) al.ToArray(typeof(PointF));
  95. this.OffsetPoint(pfs, OffsetX, OffsetY);
  96. ArrayList list = new ArrayList();
  97. list.AddRange(pfs);
  98. return list;
  99. }
  100. public void OffsetPoint(PointF[] pfs, float OffsetX, float OffsetY)
  101. {
  102. for (int i = 0; i < pfs.Length; i++)
  103. {
  104. pfs[i].X += OffsetX;
  105. pfs[i].Y += OffsetY;
  106. }
  107. }
  108. }
  109. }