CNavigatorCtrl’�A Navigation Control that Allows You to Mark and Set Items Completed

Environment: Visual C++

Here’s another control that I wrote (love writing controls!) that most likely no one else will use, but I figured I would contribute it anyway! I wrote it in a day so it does have some limitations. The control is a custom navigation control that allows a user to scroll horizontally through any number of items, selecting them, marking them, and/or setting them as completed. It is not much use on its own, but when combined with a class that needs navigation, it can be quite handy.

Personally, I needed the control for a very specific project, so built-in functionality is somewhat limited and quite basic. Some of these limitations include: no vertical support, set block and font size, and it has not been tested when resizing occurs.

The control, by default, uses the system colors for the items that are painted. Here’s what a default one may look like:

The control can also be completely customized to use any set of colors or fonts types that you want. Here’s what a customized one may look like:

Using the navagation control is extremely easy. To add the control to your project, simply do the following.

  1. Add the files NavigatorCtrl.cpp and NavigatorCtrl.cpp to your project.
  2. Add the files ‘marked.bmp’ and ‘HAND_T.CUR’ to the resources of your project. The marked.bmp image must be called IDD_MARKED and the cursor must be called “HAND_T”. You can replace the marked.bmp with an image of your own, but be sure that it is NOT bigger than the one supplied!
  3. Create the control (see below)
  4. Set items’ attributes (see below)
  5. Handle the messages (see below)

Step 3. Create the Control


int nItems = 10;
m_pNavigatorCtrl = new CNavigatorCtrl();
m_pNavigatorCtrl->Create(CRect(200,100,460,140), this,
ID_CONTROL_ID, nItems
,5 // Block Spacer
,false // Border
,RGB(204,214,223) // Background color
,RGB(255,255,255) // Boxes Background color
,RGB(200,255,255) // Completed Background color
,RGB(52,90,129) // Outline Color
,RGB(52,90,129 // Text Color
,RGB(0,0,255) // Completed Text Color
,RGB(157,176,194) // Separator Line Color
,”Ms Sans Serif” // Font face
);

The first four items in the create are required. The only one that probably needs explaining is the last one—’nItems’. This parameter is the number of items you want the control to start with. All the other parameters set the GUI type of attributes. As you can see, it is quite robust in this area.

Step 4. Set Items’ Attributes

You can set an item either Marked, Completed, or both. Here’s an example:


Completed
int nItemNumber = 3
bool bCompleted = true;
m_pNavigatorCtrl->SetCompleted(nItemNumber, bCompleted);

Marked
bool bMarked = true;
m_pNavigatorCtrl->SetMarked(nItemNumber, bMarked);

Step 5. Handle the Control’s Messages

The control only sends one message right now: ICM_NAV_ITEMSELECTED.

To add this to your project, add the following Message Map handler in the class that will handle the message:

ON_MESSAGE(ICM_NAV_ITEMSELECTED, OnNavItemSelected)

Then, add the defintition of this handler to the .h file of the same class:

afx_msg LONG OnNavItemSelected(UINT uItemSelected, LONG lUnused);

Then, add the function that will handle the message. This function also shows you how to get values out of an item in the control. As you can see, there are not many functions to call on the control.


LONG CTempView::OnNavItemSelected(UINT uItemSelected, LONG lUnused)
{
// Get the Item Selected
CString csItem;
csItem.Format(“Item %d is “, uItemSelected);

// Is it marked?
if(m_pNavigatorCtrl->GetMarked(uItemSelected))
csItem += “Marked”;
else
csItem += “NOT Marked”;

// Is it completed?
if(m_pNavigatorCtrl->GetCompleted(uItemSelected))
csItem += ” and is Completed”;
else
csItem += ” and is NOT Completed”;

// Show Message
AfxMessageBox(csItem);
return 1;
}

As stated before, this control has limited functionality in order to meet a crazy timeline! (What else is new, huh?) I’m sure there will be updates in the near future for added functionality, and I will post them ASAP.

Also, because it was written in a day, I’m sure there are bugs (bugs?!?) in it that I have overlooked. As I find them, I will also post updates.

Well, I hope at least one other person also finds some type of use for this control. It was fun to write!

Downloads

Download demo project – 56 Kb

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read