Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Menu sidebar made easy

0.00/5 (No votes)
1 Apr 2005 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here