Checker Control

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

Environment: VC++6.0, Windows 98, Windows NT

I needed the same UI functionality found in Microsoft Scandisk application. However,
after looking around on the Internet for some time I was not able to find one.
Therefore, I decided to create my own (called CCheckerCtrl).
With this control (pictured above), you can fill a series of blocks with a
color of your own choice, as well as retrieving the color of any block.

To use the control, do the following:

  1. Import the header file (including in the attached source code)
  2. Add the .cpp file (also attached to this article)
  3. Open the dialog editor and place a rectangle on the desired dialog. This control
    must have an id of IDC_STATIC_CHECKER.

  4. Define a member variable for the dialog of type CCheckerCtrl class. This variable
    can have whatever name you wish.

  5. In the dialog’s OnInitDialog member function, insert the following code
    //Declaring a local variable
    CRect rect;
    //Get the rectangle coordinates of the 
    //rectangle we already placed on dialog
    //Convert to the client coordinates 
    //Create and place the checker control on screen
    m_ctrlChecker.Create(WS_VISIBLE | WS_CHILD
                         | WS_VSCROLL | WS_TABSTOP,
                         rect, this, IDC_STATIC_CHECKER);
  6. Now, we user SetTotalBlocks method of CCheckerCtrl class to set the
    total number of blocks we would like the checker control contains.

    //Creating and placing 500 blocks on the checker control

    The function is declared as follows:

    void CCheckerCtrl::SetTotalBlocks(const UINT nNumberofBlocks,
                                      const UINT nStartIndex = 0);

    where nNumberofBlocks is the number of blocks we would like
    to place in checker control and nStartIndex is the starting index of blocks.
    i.e. sometimes you need to number those blocks from 5 to 505. To do so, you give
    the 5 as the nStartIndex. This way the first number of the first block will be
    5, the second one 6, and so on… until 505. The default value is 0.

If you run the program now, you are faced with 500 white blocks. Now, you can simply
set the color of a block by the following member function of class:

void CCheckerCtrl::SetBlock(const UINT nBlockNumber,
                            const COLORREF crColor)

For example, to paint the 2nd, 3rd, 4th and 5th block of the control red, we will do
the following:

for(register i = 2; i < 6; i++)
 m_ctrlChecker.SetBlock(i, RGB(255, 0, 0));

Now in order to make the changes to be refreshed, we simply call Refresh
function of the class:


But sometimes you need an instant refresh of a block, so you can use Update method
of the class. The declaration is as follows:

void CCheckerCtrl::Update(const UINT nBlockNumber);
for (register i = 2; i < 6; i++)
 m_ctrlChecker.SetBlock(i, RGB(255, 0, 0));

The next function of this control comes to play when we need to get the
color of a specific block. To do so, GetBlock member function is used:

COLORREF GetBlock(const UINT nBlockNumber) const;

For example to get the color of the 6th block, we do this:

COLORREF crColor = m_ctrlChecker.GetBlock(6);

Finally, we come to the last member function of
class, Reset() which resets all the block’s color into white, again.

also a pre-defined message in this class, named WM_CHECKERCTRL_RBUTTONDOWN
which is used when you need to control left-clicking, say, bring a popup menu on
screen. Please note that the message will be send to the parent window which was
given to class at creation time. You can define a handler for this message with
the following prototype:

OnRclickedCherk(WPARAM wParam, LPARAM lParam)

where wParam is the control ID of checker control and lParam is the number of block
the user is clicked on. Control ID is used to distinguish between different
checker control on the same dialog box.

Since the source code is pretty straight forward, and fulfilled with
Preconditions, Postconditions & different lines of comments, I think that
this article is finished now. You can reach the author via his email address.
Any comments, questions or suggestions are welcome. Aloha!


Download source – 4 Kb
Download demo project – 23 Kb

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read