SimplePointer.cs 5.5 KB

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