BEGIN_MESSAGE_MAP(CAlarmLargeView, CFormView)
ON_NOTIFY(NM_DBLCLK, IDC_LISTCTRL, OnClickItem)
END_MESSAGE_MAP()
void CAlarmLargeView::OnClickItem(NMHDR* pNMHDR, LRESULT* pResult)
{
}
ON_NOTIFY(NM_DBLCLK, IDC_LISTCTRL, OnClickItem)
END_MESSAGE_MAP()
void CAlarmLargeView::OnClickItem(NMHDR* pNMHDR, LRESULT* pResult)
{
}
贴一段我用过的代码:
void CMyListView::OnLButtonDown(UINT nFlags, CPoint point)
{
CListCtrl *pListCtrl = &GetListCtrl();
int index, iSubItem, iColumn;
if( ( index = HitTestEx( point, iSubItem, iColumn)) > -1 )
{
//index就是按下的那一行
//取数据使用GetListCtrl().GetItem(&lvi);,具体参数lvi看你的需要了
}
else
CListView::OnLButtonDown(nFlags, point);
}int CMyListView::HitTestEx(const CPoint &point, int &iSubItem, int &iColumn) const
{
CListCtrl *pListCtrl = &GetListCtrl(); int colnum = 0;
int row = pListCtrl->HitTest( point, NULL );
iSubItem = 0; // Make sure that the ListView is in LVS_REPORT
if( (GetWindowLong(m_hWnd, GWL_STYLE) & LVS_TYPEMASK) != LVS_REPORT )
return row; // Get the top and bottom row visible
row = pListCtrl->GetTopIndex();
int bottom = row + pListCtrl->GetCountPerPage();
if( bottom > pListCtrl->GetItemCount() )
bottom = pListCtrl->GetItemCount();
// Get the number of columns
CHeaderCtrl* pHeader = (CHeaderCtrl*)GetDlgItem(0);
int nColumnCount = pHeader->GetItemCount(); // Loop through the visible rows
for( ;row <=bottom;row++)
{
// Get bounding rect of item and check whether point falls in it.
CRect rect;
pListCtrl->GetItemRect( row, &rect, LVIR_BOUNDS );
if( rect.PtInRect(point) )
{
// Now find the column
for( colnum = 0; colnum < nColumnCount; colnum++ )
{
int colwidth = pListCtrl->GetColumnWidth(pHeader->OrderToIndex(colnum));
if( point.x >= rect.left
&& point.x <= (rect.left + colwidth ) )
{
iColumn = colnum;
// Since the header column can be dragged, so return the
// actual iSubItem value instead of the absolute column.
iSubItem = pHeader->OrderToIndex(colnum); return row;
}
rect.left += colwidth;
}
}
}
return -1;
}
ON_NOTIFY(NM_CLICK, IDC_LISTCTRL, OnClickItem)
OnClickItem为处理函数,在head文件中定义
afx_msg void OnClickItem(NMHDR* pNMHDR, LRESULT* pResult);void CAlarmLargeView::OnClickItem(NMHDR* pNMHDR, LRESULT* pResult)
{
ListCtrl2 =(CListCtrl*)GetDlgItem(IDC_LISTCTRL);
if((IndexNum = ListCtrl2->GetNextItem(-1,LVNI_SELECTED)) != -1)
{
for(i = 0;i<10; i++)//10个subitem
{
Detail[i] = ListCtrl2->GetItemText(IndexNum,i);
}
}
{
POSITION pos;
pos=GetFirstSelectedItemPosition();
if(pos)
{
LVHITTESTINFO testinfo;
testinfo.pt.x=point.x;
testinfo.pt.y=point.y; // 点击时的鼠标位置
testinfo.flags=LVHT_ONITEMLABEL; // 点击的必须是标题
// if(SubItemHitTest(&testinfo)<0)
// goto defalt_session; // 没有点在有效区域,不进入编辑
m_nItem=testinfo.iItem; // 被点击表项的行号
m_nSubItem=testinfo.iSubItem; //被点击表项的列号
}
//defalt_session:
CListCtrl::OnLButtonDown(nFlags, point);
}
int item=plist->GetNextItem(-1,LVNI_SELECTED);
CString temp=plist->GetItemText(item,LVNI_SELECTED);
其中,plist是指向listctrl的指针。
谢谢大家
int i=plist->GetSelectionMark();
CString strtext=plist->GetItemText(i,0);//第1项数据