.添加菜单资源
2.添加一个菜单,添加菜单项
如图所示:
3.加入WM_CREATE消息
代码如下:
1 int CDemoDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
2 {
3 if (CDialogEx::OnCreate(lpCreateStruct) == -1)
4 return -1;
5
6 // TODO: Add your specialized creation code here
7 CMenu* pMenu = NULL;
8 pMenu = GetMenu();
9 if(pMenu != NULL)
10 {
11 CMenu* pSubMenu = NULL;
12 pSubMenu = pMenu->GetSubMenu(0);
13 if(pSubMenu == NULL)
14 {
15 return 0;
16 }
17 int iItemCount = pSubMenu->GetMenuItemCount();
18 for(int i = 0; i < iItemCount; ++i)
19 {
20 //清除该菜单中的项
21 pSubMenu->DeleteMenu(0, MF_BYPOSITION);
22 }
23 //加入新菜单项
24 pSubMenu->AppendMenuW(MF_POPUP|MF_STRING, (UINT)pSubMenu->m_hMenu, L"一级菜单");
25 CMenu* pMenuPop = pSubMenu->GetSubMenu(0);
26 if(pMenuPop != NULL)
27 {
28 pMenuPop->AppendMenuW(MF_STRING, (UINT)pMenuPop->m_hMenu, L"二级菜单");
29 }
30 }
31 return 0;
32 }