LinearPointer.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System.ComponentModel;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using FastReport.Utils;
  5. namespace FastReport.Gauge.Linear
  6. {
  7. /// <summary>
  8. /// Represents a linear pointer.
  9. /// </summary>
  10. #if !DEBUG
  11. [DesignTimeVisible(false)]
  12. #endif
  13. public class LinearPointer : GaugePointer
  14. {
  15. #region Fields
  16. private float left;
  17. private float top;
  18. private float height;
  19. private float width;
  20. #endregion // Fields
  21. #region Properties
  22. /// <summary>
  23. /// Gets o sets the height of gauge pointer.
  24. /// </summary>
  25. [Browsable(false)]
  26. public float Height
  27. {
  28. get { return height; }
  29. set { height = value; }
  30. }
  31. /// <summary>
  32. /// Gets or sets the width of a pointer.
  33. /// </summary>
  34. [Browsable(false)]
  35. public float Width
  36. {
  37. get { return width; }
  38. set { width = value; }
  39. }
  40. #endregion // Properties
  41. #region Constructors
  42. /// <summary>
  43. /// Initializes a new instance of the <see cref="LinearPointer"/>
  44. /// </summary>
  45. /// <param name="parent">The parent gauge object.</param>
  46. public LinearPointer(GaugeObject parent) : base(parent)
  47. {
  48. height = 4.0f;
  49. width = 8.0f;
  50. }
  51. #endregion // Constructors
  52. #region Private Methods
  53. private void DrawHorz(FRPaintEventArgs e)
  54. {
  55. IGraphics g = e.Graphics;
  56. Pen pen = e.Cache.GetPen(BorderColor, BorderWidth * e.ScaleX, DashStyle.Solid);
  57. left = (float)(Parent.AbsLeft + 0.5f * Units.Centimeters + (Parent.Width - 1.0f * Units.Centimeters) * (Parent.Value - Parent.Minimum) / (Parent.Maximum - Parent.Minimum)) * e.ScaleX;
  58. top = (Parent.AbsTop + Parent.Height / 2) * e.ScaleY;
  59. height = Parent.Height * 0.4f * e.ScaleY;
  60. width = Parent.Width * 0.036f * e.ScaleX;
  61. float dx = width / 2;
  62. float dy = height * 0.3f;
  63. Brush brush = Fill.CreateBrush(new RectangleF(left - dx, top, width, height), e.ScaleX, e.ScaleY);
  64. PointF[] p = new PointF[]
  65. {
  66. new PointF(left, top),
  67. new PointF(left + dx, top + dy),
  68. new PointF(left + dx, top + height),
  69. new PointF(left - dx, top + height),
  70. new PointF(left - dx, top + dy)
  71. };
  72. if ((Parent as LinearGauge).Inverted)
  73. {
  74. p[1].Y = top - dy;
  75. p[2].Y = top - height;
  76. p[3].Y = top - height;
  77. p[4].Y = top - dy;
  78. }
  79. GraphicsPath path = new GraphicsPath();
  80. path.AddLines(p);
  81. path.AddLine(p[4], p[0]);
  82. g.FillAndDrawPath(pen, brush, path);
  83. }
  84. private void DrawVert(FRPaintEventArgs e)
  85. {
  86. IGraphics g = e.Graphics;
  87. Pen pen = e.Cache.GetPen(BorderColor, BorderWidth * e.ScaleX, DashStyle.Solid);
  88. left = (Parent.AbsLeft + Parent.Width / 2) * e.ScaleX;
  89. top = (float)(Parent.AbsTop + Parent.Height - 0.5f * Units.Centimeters - (Parent.Height - 1.0f * Units.Centimeters) * (Parent.Value - Parent.Minimum) / (Parent.Maximum - Parent.Minimum)) * e.ScaleY;
  90. height = Parent.Height * 0.036f * e.ScaleY;
  91. width = Parent.Width * 0.4f * e.ScaleX;
  92. float dx = width * 0.3f;
  93. float dy = height / 2;
  94. Brush brush = Fill.CreateBrush(new RectangleF(left, top - dy, width, height), e.ScaleX, e.ScaleY);
  95. PointF[] p = new PointF[]
  96. {
  97. new PointF(left, top),
  98. new PointF(left + dx, top - dy),
  99. new PointF(left + width, top - dy),
  100. new PointF(left + width, top + dy),
  101. new PointF(left + dx, top + dy)
  102. };
  103. if ((Parent as LinearGauge).Inverted)
  104. {
  105. p[1].X = left - dx;
  106. p[2].X = left - width;
  107. p[3].X = left - width;
  108. p[4].X = left - dx;
  109. }
  110. GraphicsPath path = new GraphicsPath();
  111. path.AddLines(p);
  112. path.AddLine(p[4], p[0]);
  113. g.FillAndDrawPath(pen, brush, path);
  114. }
  115. #endregion // Private Methods
  116. #region Public Methods
  117. /// <inheritdoc/>
  118. public override void Assign(GaugePointer src)
  119. {
  120. base.Assign(src);
  121. LinearPointer s = src as LinearPointer;
  122. Height = s.Height;
  123. Width = s.Width;
  124. }
  125. /// <inheritdoc/>
  126. public override void Draw(FRPaintEventArgs e)
  127. {
  128. base.Draw(e);
  129. if (Parent.Vertical)
  130. {
  131. DrawVert(e);
  132. }
  133. else
  134. {
  135. DrawHorz(e);
  136. }
  137. }
  138. /// <inheritdoc/>
  139. public override void Serialize(FRWriter writer, string prefix, GaugePointer diff)
  140. {
  141. base.Serialize(writer, prefix, diff);
  142. LinearPointer dc = diff as LinearPointer;
  143. if (Height != dc.Height)
  144. {
  145. writer.WriteFloat(prefix + ".Height", Height);
  146. }
  147. if (Width != dc.Width)
  148. {
  149. writer.WriteFloat(prefix + ".Width", Width);
  150. }
  151. }
  152. #endregion // Public Methods
  153. }
  154. }