Download Source Code and Demo
Features:
- Unlimited entries
- Displays 32X32 Icons with text
- Text wraps to multiple line
- Button pops up when the mouse is on the image
- Owner Drawn and easily customize the source!
- Can be used in SDI or MDI
Usage Instructions:
Open the project in which you want to add this feature. From the Project menu, select Add To Project and then Components &
Controls. From Developer Studio Components, select Dialog Bar component and click insert. When prompted, change the name of
the dialog bar to ContentMenu and the member variable to m_wndContentMenu.
Also change the default docking side to left and click OK and close all the Component Gallery windows.
Now, open the CG_IDD_CONTENTMENU from the dialog resource and replace the default TODO… static control with a new List
Box (it is a list box and NOT list control). Change the default ID from IDC_LIST1 to IDC_LIST_CONTENTS in the property window.
Also change the following style:
- Owner Draw – Variable
- No Sort
- Optionally – change the extended style to Modal Frame
With the dialog bar open in the resource editor, bring up the Class Wizard by pressing Ctrl-W. You will be given the option of
creating a new class or select an existing class. Click Select an existing class and press OK. When prompted, select
CMainFrame from the list and click OK and close all the open windows.
- Add the provided ContentMenu.cpp to the project and include the ContentMenu.h in the mainfrm.h.
- Create a variable for the class CContentMenu in the CMainFrame object in mainfrm.h
CContentMenu m_ContentMenu;
- In the OnCreate function of CMainFrame, add the following code
m_ContentMenu.SubclassDlgItem (IDC_LIST_CONTENTS, &m_wndContentBar);
m_ContentMenu.SetItems(ContentInfo, sizeof(ContentInfo) / sizeof(CONTENT_ITEMS));
ContentInfo is a regular C structure defined in the mainfrm.cpp which holds the information about the items to be displayed. It is
created as
// Menu Information for the Content Bar
static CONTENT_ITEMS ContentInfo[] =
{
{IDI_COMPOSE, "Main View"},
{IDI_FINISHED, "Another View"},
{IDI_FILED, "Another View"},
{IDI_CALENDAR, "Another View"},
};
where IDI_… are the 32×32 icons.
Now, you should be able to compile and see how it works. To get the selection from the CMainFrame object, you have to add the
code to handle the ON_LBN_SELCHANGE message. Normally, you would do this by using the Class Wizard, but in this case, you
have to do it manually:
- Add the following prototype in the mainfrm.h
afx_msg void OnSelchangeListContents();
- Add the following in mainfrm.cpp
ON_LBN_SELCHANGE(IDC_LIST_CONTENTS, OnSelchangeListContents)
before the line
END_MESSAGE_MAP()
- Add the following function also in mainfrm.cpp
void CMainFrame::OnSelchangeListContents()
{
switch (m_ContentMenu.GetCurSel())
{
case 0:
AfxMessageBox(“You have selected First Item”);
break;case 1:
AfxMessageBox(“You have selected Second Item”);
break;case 2:
AfxMessageBox(“You have selected Third Item”);
break;case 3:
AfxMessageBox(“You have selected Fourth Item”);
break;
}
}
NOTE: THIS SOURCE CODE IS PROVIDED AS IS AND I WILL NOT BE RESPONSIBLE FOR ANY DELAYS
OR DAMAGES CAUSED BY USING IT. USE AT OWN RISK.
Last updated: 10 May 1998