Codwiz51's Wiki

Add a simple menu to your WTL project.

Modified: 2008/07/19 14:26 by codewiz51 - Categorized as: CPP, WTL
Adding a Menu to a WTL CWindow (non modal)
  • Go to resource view and create the menu in your project using the menu editor
  • Load the menu like this:
       CMenu menu;
       menu.Attach( LoadMenu( _Module.GetResourceInstance(),MAKEINTRESOURCE(IDR_MENU1)));
  • Include the menu in the windows OnCreate method
    LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
    {
     SetMenu( menu );
    }
  • Add handlers to the Message map:
      BEGIN_MSG_MAP(CATLWindow)
           MESSAGE_HANDLER( WM_CLOSE, OnClose)
           MESSAGE_HANDLER(WM_CREATE, OnCreate)
           // to link a method per item:
           COMMAND_ID_HANDLER( ID_MENU_ITEM_1, OnMenuItem1)
           COMMAND_ID_HANDLER( ID_MENU_ITEM_2, OnMenuItem1)
           // to make a handler for all messages at once:
           // MESSAGE_HANDLER( WM_COMMAND, OnMenu)
       END_MSG_MAP()
  • The COMMAND_ID_HANDLER method skeleton:
      LRESULT OnMenuItem1( WORD code, WORD id, HWND hwnd, BOOL& bHandled)
       {
        return 0;
       }
  • The MESSAGE_HANDLER method skeleton:
      LRESULT OnMenu( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
       {
        PostQuitMessage(0);
        return 0;
       }
  • That's it

ScrewTurn Wiki version 2.0.31. Some of the icons created by FamFamFam.