Adding digital counters

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Description

CDigitST is a class derived from MFC CStatic class.

This class easly lets put digital counters inside your applications!



CDigitST features:


  • Easy to use!
  • Create a new digital style just drawing a bitmap
  • Automatic resize feature
  • Written in Visual C++ v5.0
  • Full source code included!
  • It’s free!



You are encouraged to use this class everywhere you want; there is no fee
required for CDigitST. Freely add modifications and/or fix bugs, but please,
send any of these to
SoftechSoftware!

How to integrate CDigitST in your application

In your project include the following files:

   DigitST.h
   DigitST.cpp
   

You also need a bitmap containing the actual digits. This bitmap must
have the following format:

  • Each digit must have the same width and height
  • In the bitmap must be present 12 digits: 0..9 [blank] and [-]
    [blank] will be used for zeroes to the left
    [-] will be used as minus sign

For example include in your project Small_1.bmp and call it IDB_SMALL1.

With dialog editor create a static control called, for example,
IDC_COUNTER and create a member variable for it:

   CDigitST m_dgtCounter;
   

Now attach the static control to CDigitST. In your OnInitDialog procedure:

   // Call the base method
   CDialog::OnInitDialog();

   // Create the IDC_COUNTER digital counter
   m_dgtCounter.SubclassDlgItem(IDC_COUNTER, this);
   

The control can display any number of digits you want (maximum is 10). Don’t
worry about the control dimensions since it will be resized at runtime depending
on the precision (number of digits) you want. The control can be
expanded/shrinked from left-to-right (or viceversa) and from top-to-bottom
(or viceversa). See “CDigitST members” section for a detailed explanation of
all possible resize values.

Set resize direction to left-to-right and top-to-bottom:

   // Set resize direction
   m_dgtCounter.SetResize(CDigitST::ST_RIGHT | CDigitST::ST_BOTTOM);
   

Now assign the bitmap to the control and set its precision:

   // Assign bitmap and precision
   m_dgtCounter.SetStyle(IDB_SMALL1, 5);    // The control will be automatically resized
   

CDigitST has a default starting value of 0 (zero) but any
value (in the range of signed integers) can be assigned to it:

   // Assign a starting value
   m_dgtCounter.SetValue(1000);
   

By default the zeroes at the left of the value will be not displayed. This
feature can be enabled/disabled at any time:

   // Enable zero padding
   m_dgtCounter.SetZeroPadding(TRUE);
   

Your static control is now a CDigitST!

Look inside the demo program to learn more about CDigitST. This is the
best way! Also give a look to the included bitmaps to learn how to
implement your own digital styles!

CDigitST members

BOOL SetStyle(UINT nBitmapId, int nPrecision);
Set digital style and precision (number of digits).
The control will be automatically resized.
Input values:


  • nBitmapId

    The resource bitmap to use as digits
  • nPrecision

    Number of digits to display

Return value:

  • Nonzero if successful; otherwise 0.

Example:
m_dgtCounter.SetStyle(IDB_SMALL1, 5);

void SetValue(int nValue, BOOL bRepaint = TRUE);
Set current digital counter value.
Input values:


  • nValue

    The new value
  • bRepaint

    If TRUE the control is immediately redrawn

Example:
m_dgtCounter.SetValue(1000);

int GetValue();
Get current digital counter value.
Example:
int nCurrent = m_dgtCounter.GetValue();

void SetPrecision(int nPrecision, BOOL bResize = TRUE);
Set number of displayed digits (called precision).
Input values:


  • nPrecision

    Number of digits to display

    If precision is too low the counter will be truncated at the left

    Precision is limited between 1 and 10 (this limit can be easly modified)
  • bRepaint

    If TRUE the control is immediately redrawn

Example:
m_dgtCounter.SetPrecision(5);

int GetPrecision();
Get number of displayed digits.
Example:
int nPrecision = m_dgtCounter.GetPrecision();

void SetResize(DWORD dwResize, BOOL bResize = TRUE);
Set control resize direction.
Input values:


  • dwResize

    Resize direction. This value can be a bitwise-OR operation of the following values:

    • ST_LEFT

      Resize to left
    • ST_RIGHT

      Resize to right
    • ST_TOP

      Resize to top
    • ST_BOTTOM

      Resize to bottom

  • bResize

    If TRUE the control is immediately resized

Example:
m_dgtCounter.SetResize(CDigitST::ST_LEFT | CDigitST::ST_TOP);

DWORD GetResize();
Get control resize direction.
Example:
DWORD dwDirection = m_dgtCounter.GetResize();

void SetZeroPadding(BOOL bPad = FALSE, BOOL bRepaint = TRUE);
Enable/Disable showing zeroes at the left of the counter (called ZeroPadding).
Input values:


  • bPad

    If TRUE the zeroes at the left are displayed
  • bRepaint

    If TRUE the control is immediately redrawn

Example:
m_dgtCounter.SetZeroPadding(TRUE);

BOOL GetZeroPadding();
Get ZeroPadding state.
Example:
BOOL bPadding = m_dgtCounter.GetZeroPadding();

void Inc(BOOL bRepaint = TRUE);
Increment current digital counter value.
Input values:


  • bRepaint

    If TRUE the control is immediately redrawn

Example:
m_dgtCounter.Inc();

void Dec(BOOL bRepaint = TRUE);
Decrement current digital counter value.
Input values:


  • bRepaint

    If TRUE the control is immediately redrawn

Example:
m_dgtCounter.Dec();


static const short GetVersionI()

static const char* GetVersionC()

Return CDigitST version.
Example:

int nVer = CDigitST::GetVersionI(); // Divide by 10 to get actual version

char szVer[20];

strcpy(szVer, CDigitST::GetVersionC());

Download the demo project (103Kb).

History


  • CDigitST v1.0

    First release

Things to do


  • Any suggestion?

Contact addresses

SoftechSoftware E-Mail:

[email protected]

SoftechSoftware homepage:

http://members.tripod.com/~SoftechSoftware/index.html

CDigitST homepage:

http://members.tripod.com/~SoftechSoftware/cdigit.html

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read