Click here to Skip to main content
11,478,873 members (59,621 online)
Click here to Skip to main content

Menu sidebar made easy

, 1 Apr 2005 CPOL 85.1K 1.6K 23
Rate this:
Please Sign up or sign in to vote.
A menu class with sidebar and color support.

Sample Image - SidebarMenu.jpg

Introduction

This is one of the simplest implementations of a custom drawn popup menu with a gradient sidebar text and other color options. The class named CSidebarMenu can be used just like an ordinary CMenu object. One important thing is that when appending menu items, the MF_OWNERDRAW flag should be mentioned.

Functions

   void SetSideBarText(LPCSTR text);
   void SetSideBarTextColor(COLORREF color); 
   void SetSideBarColor(COLORREF start, COLORREF end);
   void SetMenuBkgColor(COLORREF color);
   void SetHiLitColor(COLORREF color);
   void SetMenuTextColor(COLORREF color);
   void SetTextHiLitColor(COLORREF color);

The purpose of the above functions is very clear from the function names themselves. The function "SetSideBarColor" takes two colors - one for the starting color of the gradient and the other for the ending color of the gradient. You don't have to use all of the above functions. There is one default implementation so you can use this class just like a CMenu class.

Example

Step 1:

Declare a CSideBarMenu object and a menu handler function in your dialog's header file, such as:

class CDemoDlg : public CDialog
{

public:
   ......

protected:
BOOL 
OnInitDialog();
void OnRButtonDown( UINT nFlags, CPoint point );
void MenuHandler(UINT id);
   ......

DECLARE_MESSAGE_MAP()

private:

  CSideBarMenu mnuSideBar;
  ......
}

Step 2:

In the implementation function, create the menu, i.e.:

BOOL CDemoDlg::OnInitDialog()
{
 //Creating the menu
 mnuSideBar.CreatePopupMenu();
 mnuSideBar.AppendMenu(MF_STRING|MF_OWNERDRAW,10,"Item 1");
 mnuSideBar.AppendMenu(MF_SEPARATOR|MF_OWNERDRAW,0,"");
 mnuSideBar.AppendMenu(MF_STRING|MF_OWNERDRAW,11,"Item 2");
 mnuSideBar.AppendMenu(MF_SEPARATOR|MF_OWNERDRAW,0,"");
 mnuSideBar.AppendMenu(MF_STRING|MF_OWNERDRAW,12,"Item 3");
 return 1;
}

Step 3:

Give the message maps for handling right click mouse event and the menu events:

BEGIN_MESSAGE_MAP(CDemoDlg,CDialog)
ON_WM_RBUTTONDOWN()
ON_COMMAND_RANGE(10,12,MenuHandler)
......
END_MESSAGE_MAP()

Step 4:

Implement the mouse handler and the menu selection handler as below:

void CDemoDlg::OnRButtonDown(UINT nFlags, CPoint point)
{
 ClientToScreen(&point);
 mnuSideBar.TrackPopupMenu(TPM_LEFTALIGN, point.x, point.y, this,NULL);
 .....
}

void CDemoDlg::MenuHandler(UINT id)
{
 switch(id)
 {
  case 10:
   MessageBox("Item 1","SideBarMenu Demo");
   break;

  case 11:
   MessageBox("Item 2","SideBarMenu Demo");
   break;

  case 12:
   MessageBox("Item 3","SideBarMenu Demo");
   break;
 }
}

That's it. Enjoy using and modifying the class. All comments are welcome. Have fun with it!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Nisamudheen
Software Developer
India India
Shooting for the Moon.

Comments and Discussions

 
QuestionHow to make it work with Win32? Pin
Kumar Sundaram9-Aug-07 19:42
memberKumar Sundaram9-Aug-07 19:42 
AnswerRe: How to make it work with Win32? Pin
Nisamudheen26-Nov-07 16:17
memberNisamudheen26-Nov-07 16:17 
QuestionAny idea as to how to support theme support Pin
Vikrant for VC++18-Mar-07 13:07
memberVikrant for VC++18-Mar-07 13:07 
GeneralCan't compile! Pin
thompsons8-Dec-05 18:44
memberthompsons8-Dec-05 18:44 
GeneralRe: Can't compile! Pin
Mohd Nisamudheen S.9-Dec-05 18:15
memberMohd Nisamudheen S.9-Dec-05 18:15 
GeneralNice Job Rated a 5 Pin
rkwalker4-Apr-05 16:13
memberrkwalker4-Apr-05 16:13 
GeneralLooks Great!!! Pin
ThatsAlok1-Apr-05 1:56
memberThatsAlok1-Apr-05 1:56 
GeneralGood work, one doubt Pin
Anash P Oommen29-Jan-05 5:27
sussAnash P Oommen29-Jan-05 5:27 
GeneralRe: Good work, one doubt Pin
Mohd Nisamudheen S.3-Mar-05 0:55
memberMohd Nisamudheen S.3-Mar-05 0:55 
GeneralOne doubt Pin
neelimakrishna13-Dec-04 0:50
memberneelimakrishna13-Dec-04 0:50 
GeneralRe: One doubt Pin
Mohd Nisamudheen S.14-Dec-04 1:23
memberMohd Nisamudheen S.14-Dec-04 1:23 
GeneralWorks well, but.... Pin
Doug Knudson25-Oct-04 5:46
memberDoug Knudson25-Oct-04 5:46 
GeneralRe: Works well, but.... Pin
Mohd Nisamudheen S.26-Oct-04 0:19
memberMohd Nisamudheen S.26-Oct-04 0:19 
GeneralNothing, but an empty dialog. Pin
WREY25-Oct-04 4:00
memberWREY25-Oct-04 4:00 
GeneralRe: Nothing, but an empty dialog. Pin
Doug Knudson25-Oct-04 5:32
memberDoug Knudson25-Oct-04 5:32 
GeneralRe: Nothing, but an empty dialog. Pin
WREY25-Oct-04 12:46
memberWREY25-Oct-04 12:46 
GeneralRe: Nothing, but an empty dialog. Pin
Mohd Nisamudheen S.26-Oct-04 0:26
memberMohd Nisamudheen S.26-Oct-04 0:26 
GeneralRe: Nothing, but an empty dialog. Pin
WREY26-Oct-04 13:05
memberWREY26-Oct-04 13:05 
GeneralRe: Nothing, but an empty dialog. Pin
Mohd Nisamudheen S.26-Oct-04 20:24
memberMohd Nisamudheen S.26-Oct-04 20:24 
GeneralRe: Nothing, but an empty dialog. Pin
WREY27-Oct-04 11:35
memberWREY27-Oct-04 11:35 
GeneralRe: Nothing, but an empty dialog. Pin
Mohd Nisamudheen S.27-Oct-04 19:33
memberMohd Nisamudheen S.27-Oct-04 19:33 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 1 Apr 2005
Article Copyright 2004 by Nisamudheen
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid