MFC工控项目实例之八选择下拉菜单添加打钩图标

承接专栏《MFC工控项目实例之七点击下拉菜单弹出对话框》

具体添加菜单栏参考我的博客文章《MFC工控项目实例之七点击下拉菜单弹出对话框》

这里只给出相关代码

1、在SEAL_PRESSUREDlg.h文件中添加代码

cpp 复制代码
class CSEAL_PRESSUREDlg : public CDialog
{
// Construction
public:
    ...
    CBitmap m_bmSel,m_bmList;
	void OnTypeChange(UINT nID);
	...
	}

2、在SEAL_PRESSUREDlg.cpp文件中添加代码

cpp 复制代码
BEGIN_MESSAGE_MAP(CSEAL_PRESSUREDlg, CDialog)
	//{{AFX_MSG_MAP(CSEAL_PRESSUREDlg)
	
	//}}AFX_MSG_MAP
	ON_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);
		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);
}
相关推荐
@areok@7 分钟前
C++mat传入C#OpencvCSharp的mat
开发语言·c++·opencv·c#
小王C语言21 分钟前
【C++进阶】---- map和set的使用
开发语言·c++
Elnaij35 分钟前
从C++开始的编程生活(8)——内部类、匿名对象、对象拷贝时的编译器优化和内存管理
开发语言·c++
liuyao_xianhui1 小时前
内存管理(C/C++)
java·开发语言·c++
饭碗的彼岸one1 小时前
C++设计模式之单例模式
c语言·开发语言·c++·单例模式·设计模式·饿汉模式·懒汉模式
Tim_102 小时前
【算法专题训练】20、LRU 缓存
c++·算法·缓存
Vect__2 小时前
从零实现一个简化版string 类 —— 深入理解std::string的底层设计
c++
hope_wisdom2 小时前
C/C++数据结构之栈基础
c语言·数据结构·c++··stack
ajassi20002 小时前
开源 C++ QT Widget 开发(十四)多媒体--录音机
linux·c++·qt·开源
劲镝丶4 小时前
malloc概述
c语言·开发语言·c++