-
Notifications
You must be signed in to change notification settings - Fork 3
/
3DMeterCtrl.cpp
executable file
·557 lines (432 loc) · 15.8 KB
/
3DMeterCtrl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
// 3DMeterCtrl.cpp : implementation file
//
#include "stdafx.h"
#include "math.h"
//#include "MeterTestForm.h"
#include "3DMeterCtrl.h"
#include "MemDC.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// C3DMeterCtrl
C3DMeterCtrl::C3DMeterCtrl()
{
m_dCurrentValue = 0.0 ;
m_dMaxValue = 0.4 ;
m_dMinValue = 0.0 ;
m_nScaleDecimals = 1 ;
m_nValueDecimals = 3 ;
m_strUnits.Format("Peak") ;
m_colorNeedle = RGB(200, 0, 0) ;
}
C3DMeterCtrl::~C3DMeterCtrl()
{
if ((m_pBitmapOldBackground) &&
(m_bitmapBackground.GetSafeHandle()) &&
(m_dcBackground.GetSafeHdc()))
m_dcBackground.SelectObject(m_pBitmapOldBackground);
}
BEGIN_MESSAGE_MAP(C3DMeterCtrl, CStatic)
//{{AFX_MSG_MAP(C3DMeterCtrl)
ON_WM_PAINT()
ON_WM_SIZE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// C3DMeterCtrl message handlers
void C3DMeterCtrl::OnPaint()
{
CPaintDC dc(this); // device context for painting
// Find out how big we are
GetClientRect (&m_rectCtrl) ;
// make a memory dc
CMemDC memDC(&dc, &m_rectCtrl);
// set up a memory dc for the background stuff
// if one isn't being used
if ((m_dcBackground.GetSafeHdc() == NULL) || (m_bitmapBackground.m_hObject == NULL))
{
m_dcBackground.CreateCompatibleDC(&dc) ;
m_bitmapBackground.CreateCompatibleBitmap(&dc, m_rectCtrl.Width(),
m_rectCtrl.Height()) ;
m_pBitmapOldBackground = m_dcBackground.SelectObject(&m_bitmapBackground) ;
// Fill this bitmap with the background.
// Note: This requires some serious drawing and calculating,
// therefore it is drawn "once" to a bitmap and
// the bitmap is stored and blt'd when needed.
DrawMeterBackground(&m_dcBackground, m_rectCtrl) ;
}
// drop in the background
memDC.BitBlt(0, 0, m_rectCtrl.Width(), m_rectCtrl.Height(),
&m_dcBackground, 0, 0, SRCCOPY) ;
// add the needle to the background
DrawNeedle(&memDC) ;
// add the value to the background
DrawValue(&memDC) ;
}
void C3DMeterCtrl::DrawValue(CDC *pDC)
{
CFont *pFontOld ;
CString strTemp ;
// Pick up the font.
// Note: the font was determined in the drawing
// of the background
pFontOld = pDC->SelectObject(&m_fontValue) ;
// set the colors based on the system colors
pDC->SetTextColor(m_colorText) ;
pDC->SetBkColor(m_colorButton) ;
// draw the text in the recessed rectangle
pDC->SetTextAlign(TA_CENTER|TA_BASELINE) ;
strTemp.Format("%.*f", m_nValueDecimals, m_dCurrentValue) ;
pDC->TextOut(m_nValueCenter, m_nValueBaseline, strTemp) ;
// restore the color and the font
pDC->SetBkColor(m_colorWindow) ;
pDC->SelectObject(pFontOld) ;
}
void C3DMeterCtrl::UpdateNeedle(double dValue)
{
m_dCurrentValue = dValue ;
Invalidate() ;
}
void C3DMeterCtrl::DrawNeedle(CDC *pDC)
{
int nResult ;
double dAngleRad ;
double dTemp ;
CBrush brushFill, *pBrushOld ;
CPen penDraw, *pPenOld ;
CPoint pointNeedle[3] ;
// This function draws a triangular needle.
// The base of the needle is a horizontal line
// that runs through the center point of the arcs
// that make up the face of the meter.
// The tip of the needle is at an angle that is
// calculated based on the current value and the scale.
// The needle is constructed as a 3-point polygon
// (i.e. triangle). The triangle is drawn to the
// screen based on a clipping region that is derived
// from the meter face. See "DrawMeterBackground".
// calculate the first and last points of the needle.
pointNeedle[0].x = m_nBottomCX + m_nBottomRadius/20 ;
pointNeedle[0].y = m_nBottomCY ;
pointNeedle[2].x = m_nBottomCX - m_nBottomRadius/20 ;
pointNeedle[2].y = m_nBottomCY ;
// calculate the angle for the tip of the needle
dAngleRad = (m_dCurrentValue-m_dMinValue)*(m_dRightAngleRad-m_dLeftAngleRad)/
(m_dMaxValue-m_dMinValue) + m_dLeftAngleRad ;
// if the angle is beyond the meter, draw the needle
// at the limit (as if it is "pegged")
dAngleRad = max(dAngleRad, m_dRightAngleRad) ;
dAngleRad = min(dAngleRad, m_dLeftAngleRad) ;
// calculate the X position of the tip
dTemp = m_nBottomCX + m_nTopRadius*cos(dAngleRad) ;
pointNeedle[1].x = ROUND(dTemp) ;
// calculate the Y position of the tip
dTemp = m_nBottomCY - m_nTopRadius*sin(dAngleRad) ;
pointNeedle[1].y = ROUND(dTemp) ;
// select the clipping region based on the meter
pDC->SelectClipRgn(&m_rgnBoundary) ;
// create a pen and brush based on the needle color
brushFill.CreateSolidBrush(m_colorNeedle) ;
penDraw.CreatePen(PS_SOLID, 1, m_colorNeedle) ;
// select the pen and brush
pPenOld = pDC->SelectObject(&penDraw) ;
pBrushOld = pDC->SelectObject(&brushFill) ;
// draw the needle
pDC->Polygon(pointNeedle, 3) ;
// restore the clipping region
nResult = pDC->SelectClipRgn(NULL) ;
// restore the pen and brush
pDC->SelectObject(pPenOld) ;
pDC->SelectObject(pBrushOld) ;
}
void C3DMeterCtrl::DrawMeterBackground(CDC *pDC, CRect &rect)
{
int i, nAngleDeg, nRef ;
int nHeight ;
int nHalfPoints ;
int nStartAngleDeg, nEndAngleDeg ;
int nTickDeg ;
int nAngleIncrementDeg ;
double dTemp, dAngleRad, dX, dY ;
double dRadPerDeg ;
CString strTemp ;
CPoint pointRecess[BOUNDARY_POINTS] ;
CBrush brushFill, *pBrushOld ;
CFont *pFontOld ;
CPen penDraw, *pPenOld ;
TEXTMETRIC tm ;
// determine the centerpoint of the radii for
// the meter face.
m_nBottomCX = (rect.left+rect.right)/2 ;
m_nBottomCY = rect.bottom - rect.Height()/16 ;
// determine the radii for the top of the meter
// and the bottom of the meter
m_nTopRadius = rect.Height()*6/8 ;
m_nBottomRadius = m_nTopRadius/2 ;
// Radians per Degree
// This helps me lay things out in degrees
// which are more intuitive than radians.
dRadPerDeg = 4.0*atan(1.0)/180.0 ;
// set the left and right limits for the meter face
nStartAngleDeg = 60 ;
nEndAngleDeg = 120 ;
nTickDeg = 15 ;
// this is the density of points along the arcs
nAngleIncrementDeg = 5 ;
// convert these to radians
// for computer (rather than human) use!
m_dLeftAngleRad = nEndAngleDeg*dRadPerDeg ;
m_dRightAngleRad = nStartAngleDeg*dRadPerDeg ;
// construct the meter face region
// This is a polygon starting at the top right of
// the meter face and moving across the top arc.
nRef = 0 ;
for (nAngleDeg=nStartAngleDeg; nAngleDeg<=nEndAngleDeg; nAngleDeg+=nAngleIncrementDeg)
{
// determine the current angle in radians
dAngleRad = nAngleDeg*dRadPerDeg ;
// determine the X position
dTemp = m_nBottomCX + m_nTopRadius*cos(dAngleRad) ;
m_pointBoundary[nRef].x = ROUND(dTemp) ;
// determine the Y position
dTemp = m_nBottomCY - m_nTopRadius*sin(dAngleRad) ;
m_pointBoundary[nRef].y = ROUND(dTemp) ;
nRef++ ;
}
// at this point we have constructed the entire
// top arc of the meter face
nHalfPoints = nRef ; // hold onto this for later use
// now add points to the polygon starting at the
// left side of the lower arc.
for (nAngleDeg=nEndAngleDeg; nAngleDeg>=nStartAngleDeg; nAngleDeg-=nAngleIncrementDeg)
{
dAngleRad = nAngleDeg*dRadPerDeg ;
dTemp = m_nBottomCX + m_nBottomRadius*cos(dAngleRad) ;
m_pointBoundary[nRef].x = ROUND(dTemp) ;
dTemp = m_nBottomCY - m_nBottomRadius*sin(dAngleRad) ;
m_pointBoundary[nRef].y = ROUND(dTemp) ;
nRef++ ;
}
// Now construct a polygon that is just outside
// the meter face to use in drawing a "recess"
// around the meter face.
for (i=0; i<nRef; i++)
{
pointRecess[i].x = m_pointBoundary[i].x ;
pointRecess[i].y = m_pointBoundary[i].y-1 ;
}
pointRecess[0].x = pointRecess[0].x + 1 ;
pointRecess[nRef-1].x = pointRecess[nRef-1].x - 1 ;
for (i=nHalfPoints; i<nRef; i++)
{
pointRecess[i].x = m_pointBoundary[i].x ;
pointRecess[i].y = m_pointBoundary[i].y+1 ;
}
pointRecess[nHalfPoints].x = pointRecess[nHalfPoints].x - 1 ;
pointRecess[nRef-1].x = pointRecess[nRef-1].x + 1 ;
// create a region based on the meter face
// (to be used lated in clipping the needle)
m_rgnBoundary.DeleteObject() ;
m_rgnBoundary.CreatePolygonRgn(m_pointBoundary, nRef, ALTERNATE) ;
// locate a rectangle below the meter face
m_rectValue.left = rect.left + rect.Width()/20 ;
m_rectValue.right = rect.right - rect.Width()/20 ;
m_rectValue.bottom = rect.bottom - rect.Width()/20 ;
m_rectValue.top = rect.bottom - (rect.bottom - pointRecess[nRef-1].y)*60/100 ;
// get the relevant system colors
m_colorWindow = RGB(255,255,220);// GetSysColor(COLOR_WINDOW) ;
m_colorButton = GetSysColor(COLOR_BTNFACE) ;
m_colorShadow = GetSysColor(COLOR_BTNSHADOW) ;
m_colorHighlight = GetSysColor(COLOR_BTNHIGHLIGHT) ;
m_colorText = GetSysColor(COLOR_BTNTEXT) ;
// fill the background with the button color
brushFill.DeleteObject() ;
brushFill.CreateSolidBrush(m_colorButton) ;
pBrushOld = pDC->SelectObject(&brushFill) ;
pDC->Rectangle(rect) ;
pDC->SelectObject(pBrushOld) ;
// Draw the meter recess.
// This happens by first drawing the
// top and left sides in the shadow color.
penDraw.DeleteObject() ;
penDraw.CreatePen(PS_SOLID, 1, m_colorShadow) ;
pPenOld = pDC->SelectObject(&penDraw) ;
pDC->MoveTo(pointRecess[0]) ;
pDC->PolylineTo(pointRecess, nHalfPoints+1) ;
pDC->SelectObject(pPenOld) ;
// and then drawing the
// left and bottom sides in the shadow color.
penDraw.DeleteObject() ;
penDraw.CreatePen(PS_SOLID, 1, m_colorHighlight) ;
pPenOld = pDC->SelectObject(&penDraw) ;
// draw the bottom arc
pDC->PolylineTo(&pointRecess[nHalfPoints], nHalfPoints) ;
pDC->LineTo(pointRecess[0]) ; // connect it to the top
pDC->SelectObject(pPenOld) ;
// Draw the meter face
// use the text color for the border
penDraw.DeleteObject() ;
penDraw.CreatePen(PS_SOLID, 1, m_colorText) ;
pPenOld = pDC->SelectObject(&penDraw) ;
// use the current window color for filling
brushFill.DeleteObject() ;
brushFill.CreateSolidBrush(m_colorWindow) ;
pBrushOld = pDC->SelectObject(&brushFill) ;
// draw the meter face
pDC->Polygon(m_pointBoundary, nRef) ;
// restore the brush (but keep the pen!)
pDC->SelectObject(pBrushOld) ;
// Draw the tick marks.
// Make the tick marks extend down 10% of the face
dTemp = m_nTopRadius - 0.1*(m_nTopRadius-m_nBottomRadius) ;
// draw the right side tick marks
for (nAngleDeg=90; nAngleDeg>nStartAngleDeg; nAngleDeg-=nTickDeg)
{
dAngleRad = nAngleDeg*dRadPerDeg ;
// move to the top of the tick mark
dX = m_nBottomCX + m_nTopRadius*cos(dAngleRad) ;
dY = m_nBottomCY - m_nTopRadius*sin(dAngleRad) ;
pDC->MoveTo(ROUND(dX), ROUND(dY)) ;
// move to the bottom of the tick mark
dX = m_nBottomCX + dTemp*cos(dAngleRad) ;
dY = m_nBottomCY - dTemp*sin(dAngleRad) ;
pDC->LineTo(ROUND(dX), ROUND(dY)) ;
}
// draw the left side tick marks
for (nAngleDeg=90+nTickDeg; nAngleDeg<nEndAngleDeg; nAngleDeg+=nTickDeg)
{
dAngleRad = nAngleDeg*dRadPerDeg ;
// move to the top of the tick mark
dX = m_nBottomCX + m_nTopRadius*cos(dAngleRad) ;
dY = m_nBottomCY - m_nTopRadius*sin(dAngleRad) ;
pDC->MoveTo(ROUND(dX), ROUND(dY)) ;
// move to the bottom of the tick mark
dX = m_nBottomCX + dTemp*cos(dAngleRad) ;
dY = m_nBottomCY - dTemp*sin(dAngleRad) ;
pDC->LineTo(ROUND(dX), ROUND(dY)) ;
}
// now we're done with the pen!
pDC->SelectObject(pPenOld) ;
// Draw the recessed rectangle
// for the numerical value.
// draw the left and top sides with the shadow
penDraw.DeleteObject() ;
penDraw.CreatePen(PS_SOLID, 1, m_colorShadow) ;
pPenOld = pDC->SelectObject(&penDraw) ;
pDC->MoveTo(m_rectValue.left, m_rectValue.bottom) ;
pDC->LineTo(m_rectValue.left, m_rectValue.top) ;
pDC->LineTo(m_rectValue.right, m_rectValue.top) ;
pDC->SelectObject(pPenOld) ;
// draw the right and bottom sides with the highlight
penDraw.DeleteObject() ;
penDraw.CreatePen(PS_SOLID, 1, m_colorHighlight) ;
pPenOld = pDC->SelectObject(&penDraw) ;
pDC->LineTo(m_rectValue.right, m_rectValue.bottom) ;
pDC->LineTo(m_rectValue.left, m_rectValue.bottom) ;
pDC->SelectObject(pPenOld) ;
// determine the font size
nHeight = m_rectValue.Height()*45/100 ;
m_fontValue.DeleteObject() ;
m_fontValue.CreateFont (nHeight, 0, 0, 0, 400,
FALSE, FALSE, 0, ANSI_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH|FF_SWISS, "Arial") ;
// Now select this font and calculate the
// actual height to see what Windows gave us.
pFontOld = pDC->SelectObject(&m_fontValue) ;
pDC->GetTextMetrics(&tm) ;
m_nValueFontHeight = tm.tmHeight ;
// determine the location for the value within
// the rectangele based on the font height
m_nValueBaseline = m_rectValue.bottom -
m_nValueFontHeight/4 ;
m_nValueCenter = m_rectValue.left +
m_rectValue.Width()/2 ;
// now we need to use the font to draw some text
// set the colors (based on system colors)
pDC->SetTextColor(m_colorText) ;
pDC->SetBkColor(m_colorButton) ;
// draw the units below the meter face
pDC->SetTextAlign(TA_CENTER|TA_BASELINE) ;
pDC->TextOut(m_nValueCenter,
m_rectValue.top - m_nValueFontHeight/4,
m_strUnits) ;
// draw the value on the minimum side of the meter
pDC->SetTextAlign(TA_LEFT|TA_BASELINE) ;
strTemp.Format("%.*lf", m_nScaleDecimals, m_dMinValue) ;
pDC->TextOut((rect.left+m_pointBoundary[nHalfPoints-1].x)/2,
m_pointBoundary[nHalfPoints/2].y-m_nValueFontHeight*25/100 ,
strTemp) ;
// draw the center
pDC->SetTextAlign(TA_RIGHT|TA_BASELINE) ;
if(m_dMaxValue > 0.1)
strTemp.Format("%.*lf", m_nScaleDecimals, m_dMaxValue / 2) ;
else
strTemp.Format("%.*lf", 2, m_dMaxValue / 2) ;
if(m_dMaxValue > 1)
pDC->TextOut((rect.right + 40)/2 - 10 ,
m_pointBoundary[nHalfPoints/2].y-m_nValueFontHeight*25/100 - 20,
strTemp) ;
else
pDC->TextOut((rect.right + 40)/2 ,
m_pointBoundary[nHalfPoints/2].y-m_nValueFontHeight*25/100 - 20,
strTemp) ;
// draw the value on the maximum side of the meter
pDC->SetTextAlign(TA_RIGHT|TA_BASELINE) ;
strTemp.Format("%.*lf", m_nScaleDecimals, m_dMaxValue) ;
pDC->TextOut((rect.right+m_pointBoundary[0].x)/2,
m_pointBoundary[nHalfPoints/2].y-m_nValueFontHeight*25/100 ,
strTemp) ;
// restore the font and background color
pDC->SelectObject(pFontOld) ;
pDC->SetBkColor(m_colorWindow) ;
}
void C3DMeterCtrl::OnSize(UINT nType, int cx, int cy)
{
CStatic::OnSize(nType, cx, cy);
ReconstructControl() ;
}
void C3DMeterCtrl::ReconstructControl()
{
// if we've got a stored background - remove it!
if ((m_pBitmapOldBackground) &&
(m_bitmapBackground.GetSafeHandle()) &&
(m_dcBackground.GetSafeHdc()))
{
m_dcBackground.SelectObject(m_pBitmapOldBackground);
m_dcBackground.DeleteDC() ;
m_bitmapBackground.DeleteObject();
}
Invalidate () ;
}
void C3DMeterCtrl::SetRange(double dMin, double dMax)
{
m_dMaxValue = dMax ;
m_dMinValue = dMin ;
ReconstructControl() ;
}
void C3DMeterCtrl::SetScaleDecimals(int nDecimals)
{
m_nScaleDecimals = nDecimals ;
ReconstructControl() ;
}
void C3DMeterCtrl::SetValueDecimals(int nDecimals)
{
m_nValueDecimals = nDecimals ;
ReconstructControl() ;
}
void C3DMeterCtrl::SetUnits(CString &strUnits)
{
m_strUnits = strUnits ;
ReconstructControl() ;
}
void C3DMeterCtrl::SetNeedleColor (COLORREF colorNeedle)
{
m_colorNeedle = colorNeedle ;
ReconstructControl() ;
}