VC控件ListCtrl的使用方法总汇
以下未经说明,listctrl
-------------------------------------------------------------------------------
1. CListCtrl
LVS_ICON:
LVS_SMALLICON:
LVS_LIST:
LVS_REPORT:
直观的理解:windows
--------------------------------------------------------------------------------
2.
LONG lStyle;
lStyle = GetWindowLong(m_list.m_hWnd, GWL_STYLE);//
lStyle &= ~LVS_TYPEMASK; //
lStyle |= LVS_REPORT; //
SetWindowLong(m_list.m_hWnd, GWL_STYLE, lStyle);//
DWORD dwStyle = m_list.GetExtendedStyle();
dwStyle |= LVS_EX_FULLROWSELECT;//
dwStyle |= LVS_EX_GRIDLINES;//
dwStyle |= LVS_EX_CHECKBOXES;//item
m_list.SetExtendedStyle(dwStyle); //
注:listview
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");//
//新插入的数据在下面
int nIndex = m_list.GetItemCount();
LV_ITEM lvItem;
lvItem.mask = LVIF_TEXT ;
lvItem.iItem = nIndex; //行数
lvItem.iSubItem = 0;
lvItem.pszText = (char*)(LPCTSTR)strCount; //第一列
//在最后一行插入记录值.
m_list.InsertItem(&lvItem);
//插入其它列
m_list.SetItemText(nIndex,1,strLat);
--------------------------------------------------------------------------------
4.
选中style
--------------------------------------------------------------------------------
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.
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("
AfxMessageBox(str);
}
}
--------------------------------------------------------------------------------
7.
方法一:
CString str;
for(int i=0; i<m_list.GetItemCount(); i++)
{
if( m_list.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED )
{
str.Format(_T("
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.
TCHAR szBuf[1024];