Create an Owner Draw Menu - Step by Step

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

Environment: Visual C++ 6

For the owner draw control, I found the most difficult part is the menu. I have searched through the Web, but I only found some difficult examples. In this article, I will demonstrates the step for drawing our own menu. We'll make a menu class and a class for holding the menu data.

Let's take the SDI MFC application as an example. In the File Menu, click New to add a new project. Then, choose MFC Application Wizard (exe). In the project name, type in OwnerdrawMenu (just an example), and then click OK. In step one of the MFC App Wizard, choose Single Document. After pressing Finish, you are brought to the "New Project Information" page. You'll ignore this page, so press OK.

Let's make our menu data class first. It is just a simple class which has one CString member variable to hold the menu item's caption.

Right-click "OwnerdrawMenu classes" in the ClassView; then choose "New Class." For the class type, choose "Generic Class." In the "Name" editbox, type in "CMyMenuData" (just an example). Finally, click OK.

Add a public CString member variable which is called "m_strCaption"(just an example).

Next, we have to make a menu class. Right-click "OwnerdrawMenu classes" in the ClassView; then choose "New Class." For the class type, choose "Generic Class." In the "Name" editbox, type in "CMyMenu" (just an example). In the "Base Classes" section, type in "CMenu" in the "Derived From" editbox and choose "public" in the "As" listbox.

Our menu class is added. It's time to give some content to it. We will add three functions, some include statements, and two member variables in the CMyMenu header file (such as MyMenu.h).

#include "MyMenuData.h"
#include <vector>
using namespace std;

  //a recursive function to change all the sub menu and menu items
  // to have the owner draw style
  void ChangeToOwnerDraw(CMyMenu* pMyMenu);
  //use for drawing
  virtual void DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct );
  //use for giving the dimension of the menu item
  virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);
  vector<CMyMenu*> rgpMyMenu;         //use to handle sub menu
  vector<CMyMenuData*> rgpMyMenuData; //use to handle my menu data

We have to add the implementation in MyMenu.cpp, as shown below.

void CMyMenu::ChangeToOwnerDraw(CMyMenu *pMyMenu)
  CString str;                  //use to hold the caption temporarily
  CMyMenu* pMenu;               //use to hold the sub menu
  CMyMenuData* pMenuData;       //use to hold the menu data
  //get the number of the menu items of the parent menu
  int iMenuCount = pMyMenu->GetMenuItemCount();
  UINT nID; //use to hold the identifier of the menu items
  for (int i=0; i<iMenuCount; i++)
    //get the caption of the menu item
    pMyMenu->GetMenuString(i, str, MF_BYPOSITION);
    pMenu = 0;       //reset pointer for safety
    pMenuData = 0;   //reset pointer for safety
    pMenuData = new CMyMenuData;
    pMenuData->m_strCaption = str;

    if (pMyMenu->GetSubMenu(i)) //if the parent menu has sub menu
                             MF_BYPOSITION | MF_OWNERDRAW, 
      pMenu = new CMyMenu;
      HMENU hMenu = pMyMenu->GetSubMenu(i)->GetSafeHmenu();
      nID = pMyMenu->GetMenuItemID(i);
                             MF_BYPOSITION | MF_OWNERDRAW, 

void CMyMenu::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
  CMyMenuData* pMyMenuData = 
  //get the caption of the menu item
  CString str = pMyMenuData->m_strCaption;
  //assign the height of the menu item
  lpMeasureItemStruct->itemHeight = 23;0
  //assign the width of the menu item
  lpMeasureItemStruct->itemWidth = str.GetLength()*7;

void CMyMenu::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
  CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
  //draw an orange background

  //if the menu item is selected
  if ((lpDrawItemStruct->itemState & ODS_SELECTED) &&
    (lpDrawItemStruct->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
    //draw a blue background

  CMyMenuData* pMyMenuData = 

  CString str = pMyMenuData->m_strCaption;
  //draw the caption of the menu item
                  lpDrawItemStruct->, str);

  int iCount = rgpMyMenu.size();
  for (int i=0; i<iCount; i++)
    delete rgpMyMenu[i];
    rgpMyMenu[i] = 0;
  iCount = rgpMyMenuData.size();
  for (i=0; i<iCount; i++)
    delete rgpMyMenuData[i];
    rgpMyMenuData[i] = 0;

After we have added a lot of things to CMyMenu class, it's the time to use it in the CMainFrame class. Add the following to the MainFrm.h.

#include "MyMenu.h"

  CMyMenu* pMyMenu;

At the end of the CMainFrame::OnCreate function, but before the line "return 0;", add the following code.

pMyMenu = new CMyMenu;
HMENU hMenu = ::GetMenu(GetSafeHwnd());

We have to add a message handler for OnDestory, too. Right-click CMainFrame in the ClassView; choose "Add Windows Message Handler." Add WM_DESTORY handler. The code of the OnDestroy function is as the following.

void CMainFrame::OnDestroy() 

  if (pMyMenu)
    delete pMyMenu;
    pMyMenu = 0;

We have finished our long adventure. Press F7 for our final fire.


Download demo project - 33 Kb


  • Bugfix in code example

    Posted by Jaap on 09/05/2017 11:26pm

    There is a bug on line 21. The ID passed into ModifyMenu is not correct. The correct line is: pMyMenu-ModifyMenu(i, MF_BYPOSITION | MF_OWNERDRAW | MF_POPUP, reinterpret_cast(pMyMenu-GetSubMenu(i)-GetSafeHmenu()), (LPCTSTR)pMenuData);

  • Possible memoryleak?

    Posted by Jaap on 06/21/2017 11:03pm

    In the ChangeToOwnerDraw you do - pMenuData = new CMyMenuData; - pMenu = new CMyMenu; There is no delete statement (if I add it, it wont work anymore). Who deletes these objects?

    • Destructor

      Posted by Jaap on 09/05/2017 11:23pm

      The destructor does :)

  • How do I change the font size?

    Posted by Gregory Cocco on 07/31/2014 10:10am

    Thanks for the article. How do I change the font size? I make my own font and then use "m_wndToolBar.SetFont(&m_font)" in the OnCreate method in the MainFrm.cpp in the demo project. Thanks again!!!

  • Article helped me a lot

    Posted by pvaan kumar on 02/27/2013 09:42am

    thanks for the article. It is very useful and neat.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date