1、本程序基于前期我的博客文章《MFC_ CFile类实现下拉菜单读写实例(源码下载)》
2、程序功能动态添加下拉子菜单,用CFile类读取保存下拉菜单,用GetProfileInt WriteProfileInt读取保存下拉菜单打钩序列号,实现选中即切换成打钩图标。
3、如何添加打钩图标参阅前期我的博客文章《按钮弹出式菜单子菜单选中实时显式打钩图标实例(源码下载)MFC菜单系列三》。
4、在myDlg.h 文件
class CMyDlg : public CDialog中添加代码
CBitmap m_bmSel,m_bmList;
void OnTypeChange(UINT nID);
5、在myDlg.cpp 文件开头添加代码
#define ID_DEF_PRODUCT 50000
void CMyDlg::DoDataExchange(CDataExchange* pDX)中添加代码
//}}AFX_MSG_MAP
ON_COMMAND_RANGE(ID_DEF_PRODUCT, ID_DEF_PRODUCT + 100, OnTypeChange)
END_MESSAGE_MAP()
6、在myDlg.cpp 文件添加OnTypeChange函数代码
void CMyDlg::OnTypeChange(UINT nID)
{
theApp.m_nProductSel = nID - ID_DEF_PRODUCT+1;
CWinApp* pApp = AfxGetApp();
pApp->WriteProfileInt("参数","nProductSel1",theApp.m_nProductSel);
}
7、在myDlg.cpp 文件下拉菜单按钮中添加代码
void CMyDlg::OnButton2()
{
int m_nActivePlace = 0;
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);
m_menuType.GetSubMenu(0)->SetMenuItemBitmaps(ID_DEF_PRODUCT + i, MF_BYCOMMAND, &m_bmList,&m_bmSel);
}
CWinApp* pApp = AfxGetApp();
int m_nProductSel=pApp->GetProfileInt("参数","nProductSel1",0);
m_menuType.GetSubMenu(0)->CheckMenuItem(2 + m_nProductSel - 1,MF_CHECKED|MF_BYPOSITION);
m_menuType.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN |TPM_RIGHTBUTTON,
rect.left,rect.bottom,this);
}
运行程序
源码下载