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