å é¤æä¸è¡
m_listcontrol.DeleteItem(m_listcontrol.GetNextItem(-1,LVNI_SELECTED));
----------------------------------------------------------------------------------
æ¸
空list:
m_List1.DeleteAllItems();
---------------------------------------------------------------------------------
å¾å°selè¡ç第1åçå¼
CString str;
str=m_List1.GetItemText(sel,0);
--------------------------------------------------------------------------------
1. CListCtrl é£æ ¼ LVS_ICON: 为æ¯ä¸ªitemæ¾ç¤ºå¤§å¾æ
LVS_SMALLICON: 为æ¯ä¸ªitemæ¾ç¤ºå°å¾æ
LVS_LIST: æ¾ç¤ºä¸å带æå°å¾æ çitem
LVS_REPORT: æ¾ç¤ºitem详ç»èµæ
ç´è§çç解ï¼windowsèµæºç®¡çå¨ï¼âæ¥çâæ ç¾ä¸çâ大å¾æ ï¼å°å¾æ ï¼å表ï¼è¯¦ç»èµæâ
--------------------------------------------------------------------------------
2. 设置listctrl é£æ ¼åæ©å±é£æ ¼ LONG lStyle;
lStyle = GetWindowLong(m_list.m_hWnd, GWL_STYLE);//è·åå½åçªå£style
lStyle &= ~LVS_TYPEMASK; //æ¸
é¤æ¾ç¤ºæ¹å¼ä½
lStyle |= LVS_REPORT; //设置style
SetWindowLong(m_list.m_hWnd, GWL_STYLE, lStyle);//设置style
DWORD dwStyle = m_list.GetExtendedStyle();
dwStyle |= LVS_EX_FULLROWSELECT;//éä¸æè¡ä½¿æ´è¡é«äº®ï¼åªéç¨ä¸reporté£æ ¼çlistctrlï¼
dwStyle |= LVS_EX_GRIDLINES;//ç½æ ¼çº¿ï¼åªéç¨ä¸reporté£æ ¼çlistctrlï¼
dwStyle |= LVS_EX_CHECKBOXES;//itemåçæcheckboxæ§ä»¶
m_list.SetExtendedStyle(dwStyle); //设置æ©å±é£æ ¼
注ï¼listviewçstyle请æ¥é
msdn
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wceshellui5/html/wce50lrflistviewstyles.asp--------------------------------------------------------------------------------
3. æå
¥æ°æ® m_list.InsertColumn( 0, "ID", LVCFMT_LEFT, 40 );//æå
¥å
m_list.InsertColumn( 1, "NAME", LVCFMT_LEFT, 50 );
int nRow = m_list.InsertItem(0, â11â);//æå
¥è¡
m_list.SetItemText(nRow, 1, âjackyâ);//设置æ°æ®
--------------------------------------------------------------------------------
4. ä¸ç´éä¸item éä¸styleä¸çShow selection alwaysï¼æè
å¨ä¸é¢ç¬¬2ç¹ä¸è®¾ç½®LVS_SHOWSELALWAYS
--------------------------------------------------------------------------------
5. éä¸ååæ¶éä¸ä¸è¡ int nIndex = 0;
//éä¸
m_list.SetItemState(nIndex, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);
//åæ¶éä¸
m_list.SetItemState(nIndex, 0, LVIS_SELECTED|LVIS_FOCUSED);
--------------------------------------------------------------------------------
6. å¾å°listctrlä¸ææè¡çcheckboxçç¶æ m_list.SetExtendedStyle(LVS_EX_CHECKBOXES);
CString str;
for(int i=0; i<m_list.GetItemCount(); i++)
{
if( m_list.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED || m_list.GetCheck(i))
{
str.Format(_T("第%dè¡çcheckbox为éä¸ç¶æ"), i);
AfxMessageBox(str);
}
}
--------------------------------------------------------------------------------
7. å¾å°listctrlä¸ææéä¸è¡çåºå·
æ¹æ³ä¸ï¼
CString str;
for(int i=0; i<m_list.GetItemCount(); i++)
{
if( m_list.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED )
{
str.Format(_T("éä¸äºç¬¬%dè¡"), i);
AfxMessageBox(str);
}
}
æ¹æ³äºï¼
POSITION pos = m_list.GetFirstSelectedItemPosition();
if (pos == NULL)
TRACE0("No items were selected!\n");
else
{
while (pos)
{
int nItem = m_list.GetNextSelectedItem(pos);
TRACE1("Item %d was selected!\n", nItem);
// you could do your own processing on nItem here
}
}
--------------------------------------------------------------------------------
8. å¾å°itemçä¿¡æ¯ 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);
å
³äºå¾å°è®¾ç½®itemçç¶æï¼è¿å¯ä»¥åèmsdnæç«
Q173242: Use Masks to Set/Get Item States in CListCtrl
http://support.microsoft.com/kb/173242/en-us--------------------------------------------------------------------------------
9. å¾å°listctrlçææåçheaderå符串å
容 LVCOLUMN lvcol;
char str[256];
int nColNum;
CString strColumnName[4];//åå¦æ4å
nColNum = 0;
lvcol.mask = LVCF_TEXT;
lvcol.pszText = str;
lvcol.cchTextMax = 256;
while(m_list.GetColumn(nColNum, &lvcol))
{
strColumnName[nColNum] = lvcol.pszText;
nColNum++;
}
--------------------------------------------------------------------------------
10. 使listctrlä¸ä¸é¡¹å¯è§ï¼å³æ»å¨æ»å¨æ¡ m_list.EnsureVisible(i, FALSE);
--------------------------------------------------------------------------------
11. å¾å°listctrlåæ° int nHeadNum = m_list.GetHeaderCtrl()->GetItemCount();
--------------------------------------------------------------------------------
12. å é¤ææå æ¹æ³ä¸ï¼
while ( m_list.DeleteColumn (0))
å ä¸ºä½ å é¤äºç¬¬ä¸ååï¼åé¢çåä¼ä¾æ¬¡åä¸ç§»å¨ã
æ¹æ³äºï¼
int nColumns = 4;
for (int i=nColumns-1; i>=0; i--)
m_list.DeleteColumn (i);
--------------------------------------------------------------------------------
13. å¾å°åå»çlistctrlçè¡åå· æ·»å listctrlæ§ä»¶çNM_CLICKæ¶æ¯ç¸åºå½æ°
void CTest6Dlg::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("åå»çæ¯ç¬¬%dè¡ç¬¬%då", lvinfo.iItem, lvinfo.iSubItem);
AfxMessageBox(strtemp);
}
*/
// æ¹æ³äº:
/*
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
if(pNMListView->iItem != -1)
{
CString strtemp;
strtemp.Format("åå»çæ¯ç¬¬%dè¡ç¬¬%då",
pNMListView->iItem, pNMListView->iSubItem);
AfxMessageBox(strtemp);
}
*/
*pResult = 0;
}
--------------------------------------------------------------------------------
14. å¤ææ¯å¦ç¹å»å¨listctrlçcheckboxä¸ æ·»å listctrlæ§ä»¶çNM_CLICKæ¶æ¯ç¸åºå½æ°
void CTest6Dlg::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);
//å¤ææ¯å¦ç¹å¨checkboxä¸
if(nFlag == LVHT_ONITEMSTATEICON)
{
AfxMessageBox("ç¹å¨listctrlçcheckboxä¸");
}
*pResult = 0;
}
--------------------------------------------------------------------------------
15. å³é®ç¹å»listctrlçitemå¼¹åºèå æ·»å listctrlæ§ä»¶çNM_RCLICKæ¶æ¯ç¸åºå½æ°
void CTest6Dlg::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;
}