8. Get Item Infomation
TCHAR szBuf[1024];
LVITEM lvi;
lvi.iItem = nItemIndex;
lvi.iSubItem = 0;
lvi.mask = LVIF_TEXT;
lvi.pszText = szBuf;
lvi.cchTextMax = 1024;
m_list.GetItem(&lvi);
More infomation: Q173242: Use Masks to Set/Get Item States in CListCtrl
9. Get the header titile of CListCtrl
LVCOLUMN lvcol;
char str[256];
int nColNum;
CString strColumnName[MAX_COL];
nColNum = 0;
lvcol.mask = LVCF_TEXT;
lvcol.pszText = str;
lvcol.cchTextMax = 256;
while(m_list.GetColumn(nColNum, &lvcol))
{
strColumnName[nColNum] = lvcol.pszText;
nColNum++;
}
10. Scoll the scollbar to ensure item visible
m_list.EnsureVisible(i, FALSE);
11. Get the column count of CListCtrl
int nHeadNum = m_list.GetHeaderCtrl()->GetItemCount();
12. Delete all the columns
Method 1:
while ( m_list.DeleteColumn (0));
Once you delete the first column, the next columns will move above.
Method 2:
for (int i=nColumns-1; i>=0; i–)
m_list.DeleteColumn (i);
13. Get the row and column index of the clicked item
Method 1:
void CTestDlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
DWORD dwPos = GetMessagePos();
CPoint point( LOWORD(dwPos), HIWORD(dwPos) );
m_list.ScreenToClient(&point);
LVHITTESTINFO lvinfo;
lvinfo.pt = point;
lvinfo.flags = LVHT_ABOVE;
int nItem = m_list.SubItemHitTest(&lvinfo);
if(nItem != -1)
{
CString strtemp;
strtemp.Format(”You clicked row %d column %d”, lvinfo.iItem, lvinfo.iSubItem);
AfxMessageBox(strtemp);
}
}
Method 2:
void CTestDlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
if(pNMListView->iItem != -1)
{
CString strtemp;
strtemp.Format(”You clicked row %d column %d”,
pNMListView->iItem, pNMListView->iSubItem);
AfxMessageBox(strtemp);
}
}
14. Check if you clicked on the checkbox
void CTestDlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
DWORD dwPos = GetMessagePos();
CPoint point( LOWORD(dwPos), HIWORD(dwPos) );
m_list.ScreenToClient(&point);
LVHITTESTINFO lvinfo;
lvinfo.pt = point;
lvinfo.flags = LVHT_ABOVE;
UINT nFlag;
int nItem = m_list.HitTest(point, &nFlag);
if(nFlag == LVHT_ONITEMSTATEICON)
{
AfxMessageBox(”clicked on the checkbox”);
}
*pResult = 0;
}
15. pop up menu when right mouse button clicked
Tags: CListCtrl, Tipsvoid CTestDlg::OnRclickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
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_MENU1 ) );
CMenu* popup = menu.GetSubMenu(0);
ASSERT( popup != NULL );
popup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this );
}
*pResult = 0;
}
Permalink: Code Library - 30 Great Tips of CListCtrl Program(2)
Subcribe the update with Google Reader.
RSS feed for comments on this post · TrackBack URI
Leave a reply