动态创建的控件的消息响应只能手工来添加,以list control为例,如果你想双击list control的item项时得到一个响应可以用如下做法:
首先在头文件中添加 afx_msg void OnNMDblclkList(NMHDR *pNMHDR, LRESULT *pResult);
然后在cpp文件中添加 ON_NOTIFY(NM_DBLCLK, 1, &CpytestDlg::OnNMDblclkList)这个要房子BEGIN_MESSAGE_MAP和END_MESSAGE_MAP中间
之后在void OnNMDblckList()函数实现中做判断,例程如下:
void CpytestDlg::OnNMDblclkList(NMHDR *pNMHDR, LRESULT *pResult) { NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR; int nItem = pNMListView -> iItem; int nSubItem = pNMListView ->iSubItem; if(nItem >= 0) { CString str_NM, str_BH; //str1.Format(L"%d", nItem); //::AfxMessageBox(str1); DWORD_PTR pStr = m_listctrl1->GetItemData(nItem); m_listctrl1->SetItemData(nItem, 1); POSITION pos=m_listctrl1->GetFirstSelectedItemPosition(); int nId=(int)m_listctrl1->GetNextSelectedItem(pos); str_NM = m_listctrl1->GetItemText(nId, 0); str_BH = m_listctrl1->GetItemText(nId, 1); m_xsbh = str_BH; m_xsxm = str_NM; UpdateData(FALSE); for(int i=0; i<m_lstStudent.GetCount(); i++) { POSITION pos1 = m_lstStudent.FindIndex(i); if(m_xsxm == m_lstStudent.GetAt(pos1).xsxm) { m_iCurPos = i; SetNavigatorButton(m_iCurPos); } } //::AfxMessageBox(str1); //CString str2 = (CString*)pStr; } *pResult = 0; }其中
POSITION pos=m_listctrl1->GetFirstSelectedItemPosition(); int nId=(int)m_listctrl1->GetNextSelectedItem(pos); str_NM = m_listctrl1->GetItemText(nId, 0); str_BH = m_listctrl1->GetItemText(nId, 1);可以把你双击选择的item存到CString类型的变量中,我这里是将选出的值放入了CEdit变量中显示出来了