承接专栏《MFC工控项目实例之七点击下拉菜单弹出对话框》
具体添加菜单栏参考我的博客文章《MFC工控项目实例之七点击下拉菜单弹出对话框》
这里只给出相关代码
1、在SEAL_PRESSUREDlg.h文件中添加代码
class CSEAL_PRESSUREDlg : public CDialog
{
// Construction
public:...CBitmap m_bmSel,m_bmList;void OnTypeChange(UINT nID);...}
2、在SEAL_PRESSUREDlg.cpp文件中添加代码
BEGIN_MESSAGE_MAP(CSEAL_PRESSUREDlg, CDialog)//{{AFX_MSG_MAP(CSEAL_PRESSUREDlg)//}}AFX_MSG_MAPON_COMMAND_RANGE(ID_DEF_PRODUCT, ID_DEF_PRODUCT + 100, OnTypeChange)
END_MESSAGE_MAP()BOOL CSEAL_PRESSUREDlg::OnInitDialog()
{CDialog::OnInitDialog();...m_bmSel.LoadBitmap(IDB_BITMAP1);m_bmList.LoadBitmap(IDB_BITMAP2);m_menuType.LoadMenu(IDR_MENU1); return TRUE; // return TRUE unless you set the focus to a control
}void CSEAL_PRESSUREDlg::OnTypeChange(UINT nID)
{theApp.m_nProductSel = nID - ID_DEF_PRODUCT+1;CWinApp* pApp = AfxGetApp();pApp->WriteProfileInt("参数","nProductSel1",theApp.m_nProductSel);}void CSEAL_PRESSUREDlg::OnTypChoice()
{// TODO: Add your control notification handler code here
// CTypDlg dlg;
// dlg.DoModal();int m_nActivePlace = 0;CRect rect;GetDlgItem(IDC_TYP_CHOICE)->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);elsestr.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);
}
运行程序