转载自:http://blog.csdn.net/yueliangge910101/article/details/17347405
设置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控件
(3)设置某个item的checkbox的状态
void SetLVCheck (WPARAM ItemIndex, BOOL bCheck)
{
ListView_SetItemState (m_lvTestList.m_hWnd, ItemIndex,
UINT((int(bCheck) + 1) << 12), LVIS_STATEIMAGEMASK);
得到单击的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;
判断是否点击在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;
右键点击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;
}
item切换焦点时(包括用键盘和鼠标切换item时),状态的一些变化顺序
添加listctrl控件的LVN_ITEMCHANGED消息相应函数
void CTest6Dlg::OnItemchangedList1(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
CString sTemp;
if((pNMListView->uOldState & LVIS_FOCUSED) == LVIS_FOCUSED &&
(pNMListView->uNewState & LVIS_FOCUSED) == 0)
{
sTemp.Format("%d losted focus",pNMListView->iItem);
}
else if((pNMListView->uOldState & LVIS_FOCUSED) == 0 &&
(pNMListView->uNewState & LVIS_FOCUSED) == LVIS_FOCUSED)
{
sTemp.Format("%d got focus",pNMListView->iItem);
}
if((pNMListView->uOldState & LVIS_SELECTED) == LVIS_SELECTED &&
(pNMListView->uNewState & LVIS_SELECTED) == 0)
{
sTemp.Format("%d losted selected",pNMListView->iItem);
}
else if((pNMListView->uOldState & LVIS_SELECTED) == 0 &&
(pNMListView->uNewState & LVIS_SELECTED) == LVIS_SELECTED)
{
sTemp.Format("%d got selected",pNMListView->iItem);
}
*pResult = 0;
在CListCtrl显示文件,并根据文件类型来显示图标
网上找到的代码,自己试用并改正过, share
步骤1: 设置 list 的图像列表为 系统图像列表
BOOL SetSystemImageList( CListCtrl & list )
{
HIMAGELIST himlSmall;
HIMAGELIST himlLarge;
SHFILEINFO sfi;
char cSysDir[MAX_PATH];
CString strBuf;
memset(cSysDir, 0, MAX_PATH);
GetWindowsDirectory(cSysDir, MAX_PATH);
strBuf = cSysDir ;
// SHGetFileInfo:
// If uFlags contains SHGFI_SYSICONINDEX, the return value is a handle to
// an image list that contains the large icon images.
// If SHGFI_SMALLICON is included with SHGFI_SYSICONINDEX, the return value
// is the handle to an image list that contains the small icon images.
//
himlSmall = (HIMAGELIST)SHGetFileInfo ( (LPCSTR)cSysDir,
0,
&sfi,
sizeof(SHFILEINFO),
SHGFI_SYSICONINDEX | SHGFI_SMALLICON );
himlLarge = (HIMAGELIST)SHGetFileInfo((LPCSTR)cSysDir,
0,
&sfi,
sizeof(SHFILEINFO),
SHGFI_SYSICONINDEX | SHGFI_LARGEICON);
if (himlSmall && himlLarge)
{
::SendMessage(list.m_hWnd, LVM_SETIMAGELIST,
(WPARAM)LVSIL_SMALL, (LPARAM)himlSmall);
::SendMessage(list.m_hWnd, LVM_SETIMAGELIST,
(WPARAM)LVSIL_NORMAL, (LPARAM)himlLarge);
}
return TRUE;
}
步骤2: 给列表添加指定的文件, 同时获得该文件的的图标索引, 给 list 添加图标
int GetIconIndex(LPCTSTR lpszPath, BOOL bIsDir, BOOL bSelected) ; // 向前声明
void AddFiles(CListCtrl & list, LPCTSTR lpszFileName, BOOL bAddToDocument)
{
int nIcon = GetIconIndex(lpszFileName, FALSE, TRUE);
CString strSize;
CFileFind filefind;
// get file size
if ( filefind.FindFile(lpszFileName))
{
filefind.FindNextFile();
strSize.Format(“%d”, filefind.GetLength());
}
else
strSize = “0”;
// split path and filename
CString strFileName = lpszFileName;
CString strPath;
int nPos = strFileName.ReverseFind(‘\’);
if (nPos != -1)
{
strPath = strFileName.Left(nPos);
strFileName = strFileName.Mid(nPos 1);
}
// insert to list
int nItem = list.GetItemCount();
//list.InsertItem(nItem, strFileName, nIcon);
//list.SetItemText(nItem, 1, strSize);
//
// 此处可以根据实际需要修改代码
//
}
清除 ListCtrl 以重新初始化:
// 删除所有行
m_ctrllist.DeleteAllItems() ;
// 删除所有列
int iColCount = m_ctrllist.GetHeaderCtrl()->GetItemCount() ;
// 方法 1
// 原理: 删除第一列后, 其他列向前移动
while( m_ctrllist.DeleteColumn(0) ) ;
// 方法 2
for (int i=0; i < iColCount ; i )
{
m_ctrllist.DeleteColumn(0);
}
// 方法 3
for (int i=iColCount-1; i >= 0; i– )
{
m_ctrllist.DeleteColumn( i );
(2)当一个checkbox被check或uncheck的时候,如何获得通知
添加消息映射
ON_NOTIFY(LVN_ITEMCHANGED, IDC_MYLIST, OnItemchangedLinksList)
消息处理函数
void DemoDlg::OnItemchangedLinksList(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
*pResult = 0;
if (pNMListView->uOldState == 0 && pNMListView->uNewState == 0)
return; // No change
// Old check box state
BOOL bPrevState = (BOOL)(((pNMListView->uOldState &
LVIS_STATEIMAGEMASK)>>12)-1);
if (bPrevState < 0) // On startup there's no previous state
bPrevState = 0; // so assign as false (unchecked)
// New check box state
BOOL bChecked =
(BOOL)(((pNMListView->uNewState & LVIS_STATEIMAGEMASK)>>12)-1);
if (bChecked < 0) // On non-checkbox notifications assume false
bChecked = 0;
if (bPrevState == bChecked) // No change in check box
return;
// Now bChecked holds the new check box state
// ....
(3)设置某个item的checkbox的状态
void SetLVCheck (WPARAM ItemIndex, BOOL bCheck)
{
ListView_SetItemState (m_lvTestList.m_hWnd, ItemIndex,
UINT((int(bCheck) + 1) << 12), LVIS_STATEIMAGEMASK);
(4)获得某个item的checkbox的状态
使用宏 ListView_GetCheckState(hwndLV, i)
1、获得用户双击行的索引号,这个索引号是以0开头的;
int nIndex = m_ListCtrl.GetNextItem( -1, LVNI_SELECTED );
2、根据行索引号,取得你需要的列的内容;
CString strValue;
strValue = m_ListCtrl.GetItemText( nIndex, 0 );
如何得到ListControl选中的项 ListControl选中一行
大家都知道,ListControl只能点击第一列来选中想选中的项,而很多朋友想实现点击直接选择一行的功能,其实很简单,直接把它的ExtentedStyle设置一下就可以了,如下:
m_listcontrol为控件CListCtrl*变量
点击选中一行:m_listcontrol.SetExtendedStyle(m_listcontrol.GetExtendedStyle()|LVS_EX_FULLROWSELECT);
而如何得到具体一项里面的值,又成了很多朋友的问题,其实也很简单,下面是我知道的一种方法:
得到具体的某一项:
CString str;
int nId;
//首先得到点击的位置
POSITION pos=m_listcontrol.GetFirstSelectedItemPosition();
if(pos==NULL)
{
MessageBox(“请至少选择一项”,”娃子理财”,MB_ICONEXCLAMATION);
return;
}
//得到行号,通过POSITION转化
nId=(int)m_listcontrol.GetNextSelectedItem(pos);
//得到列中的内容(0表示第一列,同理1,2,3…表示第二,三,四…列)
str=m_listcontrol.GetItemText(nId,0);
str=m_listcontrol.GetItemText(nId,1);
…