Download demo project - 42 Kb
Once again I'm working on real-time data displays and to be
perfectly honest - I'm tired of seeing my rather flat analog
meter given in the article "Analog
Meter Class". Furthermore, the app I'm currently
working on requires that the meter be displayed in a CFormView
derived view.
Thus, I've embarked on improving the meter - providing the
following enhancements:
- A better looking, 3D-style meter.
- The use of system colors thereby allowing the meter
to "blend-in" with the rest of the dialog
or FormView regardless of the display settings.
(The meter colors are determined based on your
Display Properties.)
- A simpler interface for modifying the parameters.
To add the meter to your Dialog or FormView:
- In the resource editor, draw a new "Picture"
and name it something like
"IDC_STATICMETER". Make sure that
"Visible" is checked and that the type is
"Frame".
- In the ClassWizard (for your Dialog of FormView) add a
member variable for the IDC_STATICMETER. Make the
"name" something like
"m_3DMeterCtrl". Under
"Category" select "Control" and make
the "Variable Type" "CStatic".
- Go to the header file associated with the Dialog or the
FormView and replace CStatic with C3DMeterCtrl for the
newly assigned variable.
C3DMeterCtrl m_3DMeterCtrl ;
- Make sure you include the header file 3DMeterCtrl.h in
the header file associated with your Dialog or FormView.
Positioning the Needle
To place the needle, simply call the meter's
"UpdateNeedle" member function:
dValue = 1.234 ;
m_3DMeterCtrl.UpdateNeedle(dValue);
How it works:
The meter is drawn in the desired rectangle in three steps.
- The background is drawn. This is typically achieved
by BitBlt'ing a stored background image with the meter
face, units and limit values. The background image
is generated when the meter is first drawn and whenever
the background items are changed.
- The needle is drawn on the background. The needle
is clipped based on a region established around the meter
face.
- The numerical value is displayed in the box below the
needle.
Having a Little more Fun with the Meter
As stated above, the meter control detects the colors that
you've set through your display properties. (No this isn't
dynamic, thus you'll have to force the meter to redraw itself to
have it change when you change display properties.)
However, there are some things you can do to the meter.
Set the needle color:
colorNeedle = RGB(0, 255, 0) ;
m_3DMeterCtrl.SetNeedleColor(colorNeedle) ;
Set the range:
dMeterMin = -2.0 ;
dMeterMax = 2.0 ;
m_3DMeterCtrl.SetRange(dMeterMin, dMeterMax) ;
Set the units:
strUnits.Format("mm") ;
m_3DMeterCtrl.SetUnits(strUnits);
Set the decimal places on the max and min values:
nScaleDecimals = 2 ;
m_3DMeterCtrl.SetScaleDecimals(nScaleDecimals) ;
Set the decimal places on the current value:
nDecimals = 4 ;
m_3DMeterCtrl.SetDecimals(nDecimals) ;
Thus far, I've deployed this in a splitter application
(CFormView on the left with the meter and a CView on the right)
and it seems to be working fine under Windows 98-SE.
However, I'm sure you guy's can find "areas for
improvement". (I hate to use the "b-u-g-s"
word.)
Have fun with this and please post your feedback!
- Mark Malburg