Adding a Menu to a WTL CWindow (non modal). I've updated this article for WTL 8.0 and Visual Studio 2008.
- Start VS 2008
- Create a new WTL application based on a non-modal Dialog
- Open resource view and insert a new menu in your project
- Add a menu variable to your CMainDlg private or protected section:
protected:
CMenu menu;
- Include the menu in the windows OnInitDialog method
LRESULT CMainDlg::OnInitDialogOnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
menu.Attach( LoadMenu( _Module.GetResourceInstance(),MAKEINTRESOURCE(IDR_MENU1)));
SetMenu( menu );
// Other initializations go here
}
- 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 CMainDlg::OnMenuItem1( WORD code, WORD id, HWND hwnd, BOOL& bHandled)
{
return 0;
}
- The MESSAGE_HANDLER method skeleton:
LRESULT CMainDlg::OnMenu( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
PostQuitMessage(0);
return 0;
}
- The menu will take up a small band at the top of your dialog window. (See screen shots for an idea of what happens.) You will need to increase the height of your dialog to account for the menu dimensions.
Here's a screen shot of an app with and without the menu using the above outline:

No Menu
 With Menu Code |