ExtendedStyle
m_list.SetExtendedStyle(m_list.GetExtendedStyle()|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES|);
InsertColumn插入列,实际就是加个列头
InsertItem加入一行,实际是加一行的第一列元素,可以设置很多属性,最重要的有个lparam
SetItemText设置一行中其它元素的值
单击列头排序
int CALLBACK CmpFunc(LPARAM lParam0, LPARAM lParam1, LPARAM lParam)
{
CListCtrl *pList = (CListCtrl*)lParam;
LVCOLUMN stCol;
LVFINDINFO stFindInfo;
int iFindItem;
CString str0;
CString str1;
stFindInfo.flags = LVFI_PARAM;
stFindInfo.lParam = lParam0;
/* 根据参数lParam找到要排的行 */
iFindItem = pList->FindItem(&stFindInfo);
/* 获取要排的元素 */
str0 = pList->GetItemText(iFindItem, g_curSelCol);
stFindInfo.flags = LVFI_PARAM;
stFindInfo.lParam = lParam1;
iFindItem = pList->FindItem(&stFindInfo);
str1 = pList->GetItemText(iFindItem, g_curSelCol);
/* 根据列选择排序算法 */
if (g_curSelCol == 0)
{
return iSortDir*strcmp(str0, str1);
}
else
{
return iSortDir*(atoi(str0) - atoi(str1));
}
}
void ListView_SetHeaderSortImage(HWND listView, int columnIndex, BOOL isAscending)
{
HWND header = ListView_GetHeader(listView);
int columnCount = Header_GetItemCount(header);
for (int i = 0; i<columnCount; i++)
{
HDITEM hi = {0};
//I only need to retrieve the format if i'm on
//windows xp. If not, then i need to retrieve
//the bitmap also.
hi.mask = HDI_FORMAT;
Header_GetItem(header, i, &hi);
//Set sort image to this column
if(i == columnIndex)
{
//Windows xp has a easier way to show the sort order
//in the header: i just have to set a flag and windows
//will do the drawing. No windows xp, no easy way.
hi.fmt &= ~(HDF_SORTDOWN|HDF_SORTUP);
hi.fmt |= isAscending ? HDF_SORTUP : HDF_SORTDOWN;
}
//Remove sort image (if exists)
//from other columns.
else
{
hi.fmt &= ~(HDF_SORTDOWN|HDF_SORTUP);
}
Header_SetItem(header, i, &hi);
}
}
void CAboutDlg::OnColumnclickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
/* 获取要排序的列编号 */
g_curSelCol = pNMListView->iSubItem;
/* 排序 */
m_list.SortItems(CmpFunc, (DWORD_PTR)&m_list);
ListView_SetHeaderSortImage(m_list.GetSafeHwnd(), g_curSelCol, iSortDir>0?TRUE:FALSE);
iSortDir *= -1;
}
关于排序的箭头显示
http://www.winapizone.net/tutorials/winapi/listview/columnsortimage.php