首次写MFC界面,献丑了。
一:目标:在一个tree list上添加右键响应菜单
1. 添加菜单资源(ID: IDR_MENU_DEVICE)
菜单项的ID可以编辑,例如IDR_MENU_TREE_ADD_DEVICE
2. 添加tree list响应函数(afx_msg void OnNMRclickTreeDeviceList(NMHDR *pNMHDR, LRESULT *pResult))
3. 响应函数load菜单资源
void CXXXDlg::OnNMRclickTreeDeviceList(NMHDR *pNMHDR, LRESULT *pResult)
{
CMenu pMenu;
HTREEITEM hParent = NULL;
CPoint pt(0, 0);
GetCursorPos(&pt);
CRect rect(0, 0, 0, 0);
ScreenToClient(&pt);
GetDlgItem(IDC_TREE_DEVICE_LIST)->GetWindowRect(&rect);
ScreenToClient(&rect);
pt.x = pt.x - rect.left;
pt.y = pt.y - rect.top;
if(!pMenu.LoadMenu(IDR_MENU_TREE_ALL))
{
return;
}
GetCursorPos(&pt);
pMenu.GetSubMenu(0)->TrackPopupMenu(TPM_RIGHTALIGN,pt.x ,pt.y,this); //注意:
TPM_RIGHTALIGN, 否则load不成功
*pResult = 0;
}
二:为菜单项添加弹出窗口
1. 添加窗口资源(ID: IDR_DLG_ADD_DEVICE),并添加窗口类(CDlgAddDevice)
2. 为菜单项添加命令(例如菜单项: IDR_MENU_TREE_ADD_DEVICE )
在tree list的窗口类中添加命令响应函数:void OnMenuAddFileDevice();
在tree list的窗口类中添加消息映射:ON_COMMAND(
IDR_MENU_TREE_ADD_DEVICE
, &CXXXDlg::OnMenuAddDevice)
函数实现:
void CXXXDlg::OnMenuAddDevice()
{
CDlgAddDevice dlg;
dlg.DoModal();
}