本程序基于前期我的博客文章《MFC_CFile类读写列表控件数据实例》
程序功能用CFile类读取保存下拉菜单。
1、在原项目上添加一个菜单如图。
2、在原项目资源对话框上添加一个按钮如图。
3、在myDlg.h 文件
class CMyDlg : public CDialog中添加代码
public:
CMenu m_menuType;
4、在myDlg.cpp 文件
BOOL CMyDlg::OnInitDialog()中添加代码
m_menuType.LoadMenu(IDR_MENU1);
5、在myDlg.cpp 文件下拉菜单按钮中添加代码
void CMyDlg::OnButton2()
{
int m_nActivePlace = 0;
int ID_DEF_PRODUCT =5000;
CRect rect;
GetDlgItem(IDC_BUTTON2)->GetWindowRect(&rect);
while(m_menuType.GetSubMenu(0)->GetMenuItemCount() >2)
{
m_menuType.GetSubMenu(0)->RemoveMenu(2,MF_BYPOSITION);
}
for(int i = 0 ; i < theApp.m_allPara.GetSize() -1; i ++)
{
CString str;
if(strlen(theApp.m_allPara[i + 1].m_strBrand) > 0)
str.Format("%s(%s)",theApp.m_allPara[i + 1].m_strTypeName,theApp.m_allPara[i + 1].m_strBrand);
else
str.Format("%s",theApp.m_allPara[i + 1].m_strTypeName,theApp.m_allPara[i + 1].m_strBrand);
m_menuType.GetSubMenu(0)->AppendMenu(MF_STRING,ID_DEF_PRODUCT + i,str);
}
if(theApp.m_nProductSel > 0)
m_menuType.GetSubMenu(0)->CheckMenuItem(2 + theApp.m_nProductSel - 1,MF_CHECKED|MF_BYPOSITION);
m_menuType.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN |TPM_RIGHTBUTTON,
rect.left,rect.bottom,this);
}
运行程序
源码下载