QA: How can I insert menu in a dialog?

Vassili Philippov (vasja@spbteam.com), October 01, 2001.

Question

In VC I can edit dialog's menu in a resource editor. I cannot find menu editing in eVC dialog resource editor. How can I use menu in dialogs?

Answer

In eVC menu is not a part of a window but is a separate control that takes place in the client area and has its own handle. There are several ways to add menu into a dialog.

Using CCeCommandBar class

MFC dialogs have built-in command bar (read more). You can insert menu using InsertMenuBar method of this command bar. The best place to insert menu into a dialog is OnInitDialog method.

CCeCommandBar *pCommandBar = (CCeCommandBar*)m_pWndEmptyCB; pCommandBar->InsertMenuBar(IDR_MENUBAR);

Using SHCreateMenuBar function

Since Pocket PC Windows CE provides SHCreateMenuBar function for adding a menu to a window. Here is a sample of using this funtion:

SHMENUBARINFO info; info.cbSize = sizeof(info); info.hwndParent = m_hWnd; info.dwFlags = 0; info.nToolBarId = IDR_MENUBAR; info.hInstRes = ::AfxGetInstanceHandle(); info.nBmpId = 0; info.cBmpImages = 0; SHCreateMenuBar(&info);

Sample

You can download a sample project (27 Kb) that demonstrates using menus if dialogs. The most important part of this project is OnInitDialog method of the dialog. Here it is:

BOOL CMenuInDialogDlg::OnInitDialog() { CDialog::OnInitDialog(); // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon CenterWindow(GetDesktopWindow()); // center to the hpc screen //The first way. Using CCeCommandBar class. CCeCommandBar *pCommandBar = (CCeCommandBar*)m_pWndEmptyCB; pCommandBar->InsertMenuBar(IDR_MENUBAR); /* //The second way. Using SHCreateMenuBar function. SHMENUBARINFO info; info.cbSize = sizeof(info); info.hwndParent = m_hWnd; info.dwFlags = 0; info.nToolBarId = IDR_MENUBAR; info.hInstRes = ::AfxGetInstanceHandle(); info.nBmpId = 0; info.cBmpImages = 0; SHCreateMenuBar(&info); */ return TRUE; // return TRUE unless you set the focus to a control }