分类: C/C++
2006-11-20 12:47:14
作者:lixiaosan
时间:04/06/2006
http://blog.csdn.net/lixiaosan/archive/2006/04/07/653563.aspx
以下未经说明,listctrl默认view 风格为report
相关类及处理函数
MFC:CListCtrl类
SDK:以 “ListView_”开头的一些宏。如 ListView_InsertColumn
LVS_ICON: 为每个item显示大图标
LVS_SMALLICON: 为每个item显示小图标
LVS_LIST: 显示一列带有小图标的item
LVS_REPORT: 显示item详细资料
直观的理解:windows资源管理器,“查看”标签下的“大图标,小图标,列表,详细资料”
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
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 = 0;
//选中
m_list.SetItemState(nIndex,
LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);
//取消选中
m_list.SetItemState(nIndex, 0, LVIS_SELECTED|LVIS_FOCUSED);
m_list.SetExtendedStyle(LVS_EX_CHECKBOXES);
CString
str;
for(int i=0; i
if( m_list.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED ||
m_list.GetCheck(i))
{
str.Format(_T("第%d行的checkbox为选中状态"), i);
AfxMessageBox(str);
}
}
方法一:
CString
str;
for(int i=0; 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
}
}
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
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++;
}
方法一:
while ( m_list.DeleteColumn (0))
因为你删除了第一列后,后面的列会依次向上移动。
方法二:
int nColumns = 4;
for (int i=nColumns-1;
i>=0; i--)
m_list.DeleteColumn (i);
添加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控件的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控件的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;
}
添加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;
}
m_list.SetExtendedStyle(LVS_EX_SUBITEMIMAGES);
m_list.SetItem(..); //具体参数请参考msdn
网上找到的代码,share
BOOL CTest6Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
HIMAGELIST
himlSmall;
HIMAGELIST himlLarge;
SHFILEINFO
sfi;
char cSysDir[MAX_PATH];
CString strBuf;
memset(cSysDir, 0, MAX_PATH);
GetWindowsDirectory(cSysDir, MAX_PATH);
strBuf =
cSysDir;
sprintf(cSysDir, "%s", strBuf.Left(strBuf.Find("));
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(m_list.m_hWnd,
LVM_SETIMAGELIST,
(WPARAM)LVSIL_SMALL,
(LPARAM)himlSmall);
::SendMessage(m_list.m_hWnd,
LVM_SETIMAGELIST,
(WPARAM)LVSIL_NORMAL,
(LPARAM)himlLarge);
}
return TRUE; // return TRUE
unless you set the focus to a control
}
void
CTest6Dlg::AddFiles(LPCTSTR lpszFileName, BOOL bAddToDocument)
{
int nIcon = GetIconIndex(lpszFileName, FALSE,
FALSE);
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 = m_list.GetItemCount();
m_list.InsertItem(nItem,
strFileName, nIcon);
m_list.SetItemText(nItem, 1,
strSize);
m_list.SetItemText(nItem, 2,
strFileName.Right(3));
m_list.SetItemText(nItem, 3,
strPath);
}
int CTest6Dlg::GetIconIndex(LPCTSTR
lpszPath, BOOL bIsDir, BOOL bSelected)
{
SHFILEINFO
sfi;
memset(&sfi, 0, sizeof(sfi));
if
(bIsDir)
{
SHGetFileInfo(lpszPath,
FILE_ATTRIBUTE_DIRECTORY,
&sfi,
sizeof(sfi),
SHGFI_SMALLICON | SHGFI_SYSICONINDEX
|
SHGFI_USEFILEATTRIBUTES |(bSelected ?
SHGFI_OPENICON : 0));
return sfi.iIcon;
}
else
{
SHGetFileInfo (lpszPath,
FILE_ATTRIBUTE_NORMAL,
&sfi,
sizeof(sfi),
SHGFI_SMALLICON | SHGFI_SYSICONINDEX
|
SHGFI_USEFILEATTRIBUTES | (bSelected ?
SHGFI_OPENICON : 0));
return sfi.iIcon;
}
return -1;
}
m_list.SetRedraw(FALSE);
//更新内容
m_list.SetRedraw(TRUE);
m_list.Invalidate();
m_list.UpdateWindow();
或者参考
Q250614:How To Sort Items in a CListCtrl in Report View
Q141834: How to change the icon or the bitmap of a CListCtrl item in Visual
C++
Article ID | : | 141834 |
Last Review | : | June 2, 2005 |
Revision | : | 3.0 |
BEGIN_MESSAGE_MAP(CTestView, CView)
//{{AFX_MSG_MAP(CTestView)
ON_WM_CREATE()
//}}AFX_MSG_MAP
ON_NOTIFY (LVN_GETDISPINFO, IDI_LIST, OnGetDispInfo)
END_MESSAGE_MAP()
int CTestView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// m_pImage is a CTestView's member variable of type CImageList*
// create the CImageList with 16x15 images
m_pImage = new CImageList();
VERIFY (m_pImage->Create (16, 15, TRUE, 0, 1));
CBitmap bm;
// IDR_MAINFRAME is the toolbar bitmap in a default AppWizard
// project.
bm.LoadBitmap (IDR_MAINFRAME);
// This will automatically parse the bitmap into nine images.
m_pImage->Add (&bm, RGB (192, 192, 192));
// m_pList is CTestView's member variable of type CListCtrl*
// create the CListCtrl.
m_pList = new CListCtrl();
VERIFY (m_pList->Create (WS_VISIBLE | WS_CHILD | LVS_REPORT |
LVS_EDITLABELS, CRect (0, 0, 400, 400), this, IDI_LIST));
// Create column.
m_pList->InsertColumn (0, "Button Number", LVCFMT_LEFT, 100);
// Associate CImageList with CListCtrl.
m_pList->SetImageList (m_pImage, LVSIL_SMALL);
char szTemp[10];
for (int iCntr = 0; iCntr < 9; iCntr++)
{
wsprintf (szTemp, "%d", iCntr);
m_pList->InsertItem (LVIF_IMAGE | LVIF_TEXT,
iCntr, szTemp, 0, 0, I_IMAGECALLBACK, 0L);
}
return 0;
}
void CTestView::OnGetDispInfo (NMHDR* pnmhdr, LRESULT* pResult)
{
LV_DISPINFO* pdi = (LV_DISPINFO *) pnmhdr;
// Fill in the LV_ITEM structure with the image info.
// When an item is selected, the image is set to the first
// image (the new bitmap on the toolbar).
// When it is not selected, the image index is equal to the
// item number (that is, 0=new, 1=open, 2=save, and so on.)
if (LVIS_SELECTED == m_pList->GetItemState (pdi->item.iItem,
LVIS_SELECTED))
pdi->item.iImage = 0;
else
pdi->item.iImage = pdi->item.iItem;
}
CTestView::~CTestView()
{
// Clean up.
delete m_pImage;
delete m_pList;
}
Q151897: CListCtrl::InsertColumn() Causes Column Data to Shift
解决办法:把第一列当一个虚列,从第二列开始插入列及数据,最后删除第一列。
具体解释参阅
把需隐藏的列的宽度设为0,然后检测当该列为隐藏列时,用上面第27点的锁定column 的拖动来实现
解决办法:需要在item上放一个edit。
Q125694: How To Find Out Which Listview Column Was Right-Clicked
Q234310: How to implement a ListView control that is similar to Windows
Explorer by using DirLV.exe
Q200054:
PRB: OnTimer() Is Not Called Repeatedly for a List Control
(1) 拖放
在CListCtrl和CTreeCtrl间拖放
(2) 多功能listctrl
支持subitem可编辑,图标,radiobutton,checkbox,字符串改变颜色的类
支持排序,subitem可编辑,subitem图标,subitem改变颜色的类
(3) subitem中显示超链接
(4) subitem的tooltip提示
(5) subitem中显示进度条
(6) 动态改变subitem的颜色和背景色
(7) 类vb属性对话框
(8) 选中subitem(只高亮选中的item)
(9) 改变行高
(10) 改变行颜色
(11) 可编辑subitem的listctrl
(12) subitem可编辑,插入combobox,改变行颜色,subitem的tooltip提示
(13) header
中允许多行字符串
(14) 插入combobox
(15) 添加背景图片
http://www.codeguru.com/Cpp/controls/listview/backgroundcolorandimage/article.php/c4173/
http://www.codeguru.com/Cpp/controls/listview/backgroundcolorandimage/article.php/c983/
=
(16) 自适应宽度的listctrl
(17) 改变ListCtrl高亮时的颜色(默认为蓝色)
处理 NM_CUSTOMDRAW
http://www.codeproject.com/listctrl/lvcustomdraw.asp
(18) 改变header颜色