xxxDlg.h文件中:
CListCtrl m_lstSorting;
afx_msg void OnMenuDelete(); //手动
// Generated message map functions
//{{AFX_MSG(CAlarmStoreSimulatorDlg)
...
afx_msg void OnRclickLisSorting(NMHDR* pNMHDR, LRESULT* pResult);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
xxxDlg.cpp中:
BEGIN_MESSAGE_MAP(CAlarmStoreSimulatorDlg, CDialog)
//{{AFX_MSG_MAP(CAlarmStoreSimulatorDlg)
...
ON_NOTIFY(NM_RCLICK, IDC_LIS_SORTING, OnRclickLisSorting)
ON_COMMAND_RANGE(ID_MENUITEM_DELETE, ID_MENUITEM_DELETE, OnMenuDelete)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void CAlarmStoreSimulatorDlg::OnRclickLisSorting(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
if(pNMListView->iItem != -1)
{
DWORD dwPos = GetMessagePos();
CPoint point( LOWORD(dwPos), HIWORD(dwPos) );
CMenu menu;
VERIFY( menu.LoadMenu( IDR_MENU_POPUP ) ); //IDR_MENU_POPUP是新建菜单ID
CMenu* popup = menu.GetSubMenu(0);
ASSERT( popup != NULL );
popup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this );
}
*pResult = 0;
}
void CAlarmStoreSimulatorDlg::OnMenuDelete()
{
int iItemSel = m_lstSorting.GetNextItem(-1, LVIS_SELECTED);
if(iItemSel != -1)
{
m_lstSorting.DeleteItem(iItemSel); //delete selected item in the list control
}
}
备注:
学会了用这个消息映射ON_COMMAND_RANGE(ID_MENUITEM_DELETE, ID_MENUITEM_DELETE, OnMenuDelete)去响应弹出菜单的事件响应,ID_MENUITEM_DELETE是菜单button的ID,好像还可以是一个ID区域, OnMenuDelete是消息响应函数.