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

承接专栏《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_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);
}

运行程序
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

兵哥工控

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值