Click here to Skip to main content
15,867,568 members
Articles / Desktop Programming / MFC
Article

An Improved Analog Meter Control

Rate me:
Please Sign up or sign in to vote.
4.76/5 (13 votes)
19 Jun 2000 208.1K   3.8K   89   29
An Analog Meter Control for displaying real-time data
  • Download demo project - 42 Kb
  • Sample Image - 3dMeter.gif

    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 "<a href="http: www.codeproject.com="" miscctrl="" analog_meter.asp"="">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:

    1. 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".
    2. 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".
    3. 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 ;
    
    1. 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.

    1. 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.
    2. The needle is drawn on the background.  The needle is clipped based on a region established around the meter face.
    3. 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!

        - <a href="mailto:mcmalburg@digitalmetrology.com">Mark Malburg

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    Software Developer (Senior) Digital Metrology Solutions, Inc.
    United States United States
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    GeneralNeed Help Pin
    ganesh.dp9-Dec-09 1:04
    ganesh.dp9-Dec-09 1:04 
    GeneralGood Job Pin
    JBAK_CP24-Mar-09 8:57
    JBAK_CP24-Mar-09 8:57 
    GeneralSetName Pin
    varandas10-Jul-08 6:34
    varandas10-Jul-08 6:34 
    GeneralGPF Pin
    Michel Wassink29-Jun-05 23:33
    Michel Wassink29-Jun-05 23:33 
    GeneralSetUnits Pin
    Michel Wassink22-Jun-05 22:56
    Michel Wassink22-Jun-05 22:56 
    GeneralGreat Meter.... need to change on the fly Pin
    clav16-May-05 9:13
    clav16-May-05 9:13 
    GeneralRe: Great Meter.... need to change on the fly Pin
    Mark C. Malburg16-May-05 9:24
    Mark C. Malburg16-May-05 9:24 
    GeneralRe: Great Meter.... I fixed my problem Pin
    clav18-May-05 8:52
    clav18-May-05 8:52 
    Generaldivision by zero Pin
    Michel Wassink18-Nov-04 0:31
    Michel Wassink18-Nov-04 0:31 
    GeneralRe: division by zero Pin
    Anonymous18-Nov-04 1:05
    Anonymous18-Nov-04 1:05 
    GeneralGreat work Pin
    philliad2-Sep-04 13:46
    philliad2-Sep-04 13:46 
    GeneralRe: Great work Pin
    Mark C. Malburg2-Sep-04 16:30
    Mark C. Malburg2-Sep-04 16:30 
    QuestionHow to add this to a dialog box? Pin
    jianxing16-Jun-04 18:15
    jianxing16-Jun-04 18:15 
    GeneralLooks good - A request though Pin
    alan9318-Jun-03 17:32
    alan9318-Jun-03 17:32 
    GeneralRe: Looks good - A request though Pin
    Mark C. Malburg19-Jun-03 2:56
    Mark C. Malburg19-Jun-03 2:56 
    GeneralRe: Looks good - A request though Pin
    15-Apr-05 21:43
    suss15-Apr-05 21:43 
    GeneralRe: Looks good - A request though Pin
    Dieter6821-Jan-09 9:10
    Dieter6821-Jan-09 9:10 
    QuestionNo tap button? Pin
    Tim Smith3-Jan-03 6:26
    Tim Smith3-Jan-03 6:26 
    AnswerRe: No tap button? Pin
    Mark C. Malburg3-Jan-03 6:52
    Mark C. Malburg3-Jan-03 6:52 
    GeneralVery Nice!! Pin
    Miki3-Jan-03 6:09
    Miki3-Jan-03 6:09 
    GeneralRe: Very Nice!! Pin
    Mark C. Malburg3-Jan-03 6:51
    Mark C. Malburg3-Jan-03 6:51 
    GeneralGreat - just what the doctor ordered :) Pin
    Lea Robbo30-Jul-02 6:13
    Lea Robbo30-Jul-02 6:13 
    GeneralSmall Bug Pin
    Ernesto Perales Soto10-May-02 4:32
    Ernesto Perales Soto10-May-02 4:32 
    Hi fellows,

    I you want to insert this control directly as a child of your frame window, insert the following initialization:

    <br />
    C3DMeterCtrl::C3DMeterCtrl()<br />
    {<br />
      m_dCurrentValue  =  0.0 ;<br />
      m_dMaxValue      =  5.0 ;<br />
      m_dMinValue      = -5.0 ;<br />
    <br />
      m_nScaleDecimals = 1 ;<br />
      m_nValueDecimals = 3 ;<br />
    <br />
      m_strUnits.Format("Volts") ;<br />
    <br />
      m_pBitmapOldBackground=NULL; //<-- Here<br />
    <br />
      m_colorNeedle = RGB(255, 0, 0) ;<br />
    }<br />

    GeneralRe: Small Bug Pin
    10-May-02 4:35
    suss10-May-02 4:35 
    GeneralNice Pin
    15-Jan-02 14:34
    suss15-Jan-02 14:34 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.