以下未经说明,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];
LVITEM lvi;
lvi.iItem = nItemIndex;
lvi.iSubItem = 0;
lvi.mask = LVIF_TEXT;
lvi.pszText = szBuf;
lvi.cchTextMax = 1024;
m_list.GetItem(&lvi);
关于得到设置item
Q173242: Use Masks to Set/Get Item States in ClistCtrl
http://support.microsoft.com/kb/173242/en-us
--------------------------------------------------------------------------------
9.
LVCOLUMN lvcol;
char str[256];
int nColNum;
CString strColumnName[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.
m_list.EnsureVisible(i, FALSE);
--------------------------------------------------------------------------------
11.
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
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("
AfxMessageBox(strtemp);
}
*/
//
/*
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
if(pNMListView->iItem != -1)
{
CString strtemp;
strtemp.Format("
pNMListView->iItem, pNMListView->iSubItem);
AfxMessageBox(strtemp);
}
*/
*pResult = 0;
}
--------------------------------------------------------------------------------
14.
添加listctrl
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);
//
if(nFlag == LVHT_ONITEMSTATEICON)
{
AfxMessageBox("
}
*pResult = 0;
}
--------------------------------------------------------------------------------
15.
添加listctrl
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;
}