ListControl控件的使用方法
ListControl控件的使用方法
列表控件可以看作是功能增强的ListBox,它提供了四种风格,而且可以同时显示一列的多中属性值。
MFC中使用CListCtrl类来封装列表控件的各种操作。
通过调用BOOL Create( DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID );创建一个窗口,dwStyle中可以使用以下一些列表控件的专用风格:
LVS_ICON ,LVS_SMALLICON ,LVS_LIST, LVS_REPORT 这四种风格决定控件的外观,同时只可以选择其中一种,分别对应:大图标显示,小图标显示,列表显示,详细报表显示LVS_EDITLABELS 结点的显示字符可以被编辑,对于报表风格来讲可编辑的只为第一列。
LVS_SHOWSELALWAYS 在失去焦点时也显示当前选中的结点
LVS_SINGLESEL 同时只能选中列表中一项
首先你需要设置列表控件所使用的ImageList,如果你使用大图标显示风格,你就需要以如下形式调用:
CImageList* SetImageList( CImageList* pImageList, LVSIL_NORMAL);
如果使用其它三种风格显示而不想显示图标你可以不进行任何设置,否则需要以如下形式调用:
CImageList* SetImageList( CImageList* pImageList, LVSIL_SMALL);
int InsertItem( int nItem, LPCTSTR lpszItem ); 插入行
nItem:指明插入位置
lpszItem:为显示字符。
除LVS_REPORT风格外其他三种风格都只需要直接调用InsertItem就可以了,但如果使用报表风格就必须先设置列表控件中的列信息。
int InsertColumn( int nCol, LPCTSTR lpszColumnHeading, int nFormat , int nWidth, int nSubItem); 插入列iCol:为列的位置,从零开始lpszColumnHeading:为显示的列名nFormat:为显示对齐方式nWidth:为显示宽度nSubItem:为分配给该列的列索引。
BOOL SetItemText( int nItem, int nSubItem, LPTSTR lpszText );设置每列的显示字符
nItem:为行位置
nSubItem:为列位置
lpszText:为显示字符
下面的代码演示了如何设置多列并插入数据:
m_list.SetImageList(&m_listSmall,LVSIL_SMALL);//设置ImageList
m_list.InsertColumn(0,"Col 1",LVCFMT_LEFT,300,0);//设置列
m_list.InsertColumn(1,"Col 2",LVCFMT_LEFT,300,1);
m_list.InsertColumn(2,"Col 3",LVCFMT_LEFT,300,2);
m_list.InsertItem(0,"Item 1_1");//插入行
m_list.SetItemText(0,1,"Item 1_2");//设置该行的不同列的显示字符
m_list.SetItemText(0,2,"Item 1_3")
COLORREF GetTextColor( )/BOOL SetTextColor( COLORREF cr ):用于得到/设置显示的字符颜色。
COLORREF GetTextBkColor( )/BOOL SetTextBkColor( COLORREF cr ):用于得到/设置显示的背景颜色。
void SetItemCount( int iCount ):用于得到添加进列表中项的数量。
BOOL DeleteItem(int nItem):用于删除某一项
BOOL DeleteAllItems( ):将删除所有项。
BOOL SetBkImage(HBITMAP hbm, BOOL fTile , int xOffsetPercent, int yOffsetPercent):用于设置背景位图。
CString GetItemText( int nItem, int nSubItem ):用于得到某项的显示字符。
列表控件的消息映射同样使用ON_NOTIFY宏,形式如同:ON_NOTIFY( wNotifyCode, id, memberFxn ),wNotifyCode为通知代码,id为产生该消息的窗口ID,memberFxn为处理函数,函数的原型如同void OnXXXList(NMHDR* pNMHDR, LRESULT* pResult),其中pNMHDR 为一数据结构,在具体使用时需要转换成其他类型的结构。
对于列表控件可能取值和对应的
数据结构为:
LVN_BEGINLABELEDIT 在开始某项编辑字符时发送,所用结构:
NMLVDISPINFO
LVN_ENDLABELEDIT 在结束某项编辑字符时发送,所用结构:
NMLVDISPINFO
LVN_GETDISPINFO 在需要得到某项信息时发送,
(如得到某项的显示字符)所用结构:NMLVDISPINFO
关于ON_NOTIFY有很多内容,将在以后的内容中进行详细讲解。
使用ListControl控件显示数据表
使用ListControl控件显示数据表一、在Windows应用程序中添加ListControl控件的几种方法1、在对话框中添加控件:创建对话框应用程序,在资源视图中添加对话框。
如下图所示,在控件工具条中选择ListControl控件添加到对话框中,并调整控件尺寸。
在ListControl控件的属性页中设置控件样式(Styles)和扩展样式等。
为了显示详细数据信息,可以设置控件的显示风格为报表视图(Report)。
为了在对话框中使用控件,需要在ClassWizard中为对话框类添加控件成员变量,如下图所示:执行以上操作后,系统自动添加了一个ClistControl对象,并与ListControl控件资源关联,相关代码如下:// ListDialogDlg.h : header file......public:CListCtrl m_cListCtrl;......// ListDialogDlg.cpp : implementation file......void CListDialogDlg::DoDataExchange(CDataExchange* pDX) {CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CListDialogDlg)DDX_Control(pDX, IDC_LIST_CTRL, m_cListCtrl);//}}AFX_DATA_MAP}......因为本例只为演示在对话框中添加ListControl控件,所以只在对话框初始化消息函数中添加了以下显示控件的示例代码(代码含义请参照MSDN文献):BOOL CListDialogDlg::OnInitDialog(){CDialog::OnInitDialog();......// TODO: Add extra initialization hereDWORD exstyle = m_cListCtrl.GetExtendedStyle();m_cListCtrl.SetExtendedStyle(exstyle | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES |LVS_EX_CHECKBOXES | WS_EX_STATICEDGE );CRect rect;m_cListCtrl.GetClientRect(&rect);int nColInterval = rect.Width()/5;m_cListCtrl.InsertColumn(0, _T("Item Name"), LVCFMT_LEFT, nColInterval*3);m_cListCtrl.InsertColumn(1, _T("Value"), LVCFMT_LEFT, nColInterval);m_cListCtrl.InsertColumn(2, _T("Time"), LVCFMT_LEFT, rect.Width()-4*nColInterval);m_cListCtrl.InsertItem(0,"name");m_cListCtrl.SetItemText(0,1,"value");m_cListCtrl.SetItemText(0,2,"time");......return TRUE; // return TRUE unless you set the focus to a control}2、在单文档视图中自行添加ListControl控件:创建单文档应用程序,如下图所示为视图类添加CListCtrl对象成员变量:如下图所示,在资源视图中打开String Table资源,在右键菜单中选择new string,为将要创建的ListControl控件添加资源编号:为创建和显示控件,在视图的初始化消息函数中添加如下代码:void CAddListView::OnInitialUpdate(){CView::OnInitialUpdate();// TODO: Add your specialized code here and/or call the base classm_cListCtrl.Create( LVS_REPORT,CRect(0,0,800,600),this,IDC_ LISTCTRL);m_cListCtrl.ModifyStyle(0,LVS_REPORT|LVS_SHOWSELALWA YS|LVS_SORTASCENDING);//m_cListCtrl.SendMessage(LVM_SETEXTENDEDLISTVIEWST YLE,0,LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT|LVS_EX_CHECK BOXES);m_cListCtrl.ShowWindow(SW_SHOW);CRect rect;m_cListCtrl.GetClientRect(&rect);int nColInterval = rect.Width()/5;m_cListCtrl.InsertColumn(0, _T("Item Name"), LVCFMT_LEFT, nColInterval*3);m_cListCtrl.InsertColumn(1, _T("Value"), LVCFMT_LEFT, nColInterval);m_cListCtrl.InsertColumn(2, _T("Time"), LVCFMT_LEFT, rect.Width()-4*nColInterval);m_cListCtrl.InsertItem(0,"name");m_cListCtrl.SetItemText(0,1,"value");m_cListCtrl.SetItemText(0,2,"time");}3、在ListView视图中显示ListControl控件:创建单文档应用程序,如下图所示,在选择窗口视图类型时,选择CListView视图:在视图类的初始化消息函数中添加如下显示控件的代码:void CTestlistView::OnInitialUpdate(){CListView::OnInitialUpdate();CDC* dc = GetDC();TEXTMETRIC tm;dc->GetTextMetrics(&tm);GetListCtrl().ModifyStyle(0,LVS_REPORT|LVS_SHOWSELALW AYS|LVS_SORTASCENDING);GetListCtrl().SendMessage(LVM_SETEXTENDEDLISTVIEWSTY LE,0,LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT|LVS_EX_CHECK BOXES|LVS_EX_TRACKSELECT);GetListCtrl().InsertColumn(0,"Name",LVCFMT_LEFT,30*tm.tm AveCharWidth,0);GetListCtrl().InsertColumn(1,"Comment",LVCFMT_LEFT,70*t m.tmAveCharWidth,1);// TODO: You may populate your ListView with items by directly accessing// its list control through a call to GetListCtrl().}二、为ListControl控件添加消息处理函数1、关于ListControl控件消息在ListControl控件中有以下消息(通知)可以发送:NM_CLICK、NM_DBLCLK、NM_RCLICK、LVN_COLUMNCLICK、HDN_ITEMCLICK等。
ListControl控件技巧总汇
ListControl控件技巧总汇一、基本使用1. 创建List Control控件:使用CListCtrl类的Create函数来创建List Control控件,并指定样式。
2. 设置List Control控件的样式:使用SetExtendedStyle函数来设置List Control控件的扩展样式,例如设置带有网格线、可以选择多行等等。
3. 添加列表项:使用InsertItem函数来添加列表项,可以指定列表项的文本和图标等属性。
4. 设置列标题:使用InsertColumn函数来设置列标题,可以指定列的索引、宽度、文本等属性。
二、列表项操作1. 删除列表项:使用DeleteItem函数来删除指定索引的列表项。
2. 修改列表项:使用SetItemText函数来修改指定索引的列表项的文本。
3. 获取列表项:使用GetItemText函数来获取指定索引的列表项的文本。
4. 设置列表项属性:使用SetItemData函数来设置列表项的用户数据,可以将数据与列表项关联起来。
三、列操作1. 删除列:使用DeleteColumn函数来删除指定索引的列。
2. 移动列:使用SetColumnOrderArray函数来移动列的顺序。
3. 调整列宽:使用SetColumnWidth函数来调整列的宽度。
四、列表样式修改1. 修改行高:使用SetItemHeight函数来修改列表项的行高。
2. 修改列宽:使用SetColumnWidth函数来修改列的宽度。
3.绘制单元格背景:使用NM_CUSTOMDRAW消息来绘制列表项的背景,可以自定义背景色或者绘制图标。
五、排序功能1. 启用排序:使用EnableHeaderSorting函数来启用列表项的排序功能。
2.排序回调:使用排序回调函数来自定义排序的规则。
3. 刷新列表:使用SortItems函数来刷新列表,使得排序生效。
六、自定义绘制1.自定义绘制列表项:使用NM_CUSTOMDRAW消息来自定义绘制列表项的外观,例如修改字体、颜色等。
mfc listcontrol 控件使用总结整理
mfc listcontrol 控件使用总结整理MFC List Control控件是MFC框架中的一个常用控件,用于显示和管理列表数据。
以下是对MFC List Control控件使用的一些总结整理:1. 创建控件:通过在对话框资源中添加List Control控件进行创建,或者在代码中使用Create()函数动态创建。
2. 设置风格:可以通过代码设置控件的风格,如设置单选模式、多选模式、网格线等。
3. 设置列头:通过调用InsertColumn()函数来设置控件的列头,可以设置列标题、宽度等属性。
4. 添加项:可以通过调用InsertItem()函数来添加行(项),可以设置每个项的数据、图标等属性。
5. 设置子项:通过调用SetItemText()函数来设置每个项的子项的文本内容。
6. 获取选定项:可以通过调用GetNextItem()函数来获取当前选中项的索引,然后可以通过GetItemText()函数来获取选中项的内容。
7. 删除项:可以通过调用DeleteItem()函数来删除指定的项。
8. 排序:可以通过调用SortItems()函数来对List Control控件的内容进行排序。
9. 自定义绘制:可以通过重写OnCustomDraw()函数来实现对List Control控件的自定义绘制,如改变项的背景颜色、文本颜色等。
10. 列表视图:可以通过设置控件的样式为列表视图,来实现更复杂的数据展示和交互功能,如拖拽、排序、编辑等。
需要注意的是,MFC List Control控件的使用需要注意内存释放和资源管理,例如在动态创建控件时需要手动调用DeleteObject()函数来释放资源。
另外,对于大量数据的操作,可以考虑使用虚拟列表实现数据的延迟加载,以提高性能和响应速度。
MFC中ListControl控件的使用
MFC中ListControl控件的使用以下未经说明,listctrl默认view 风格为report1. CListCtrl 风格LVS_ICON: 为每个item显示大图标LVS_SMALLICON: 为每个item显示小图标LVS_LIST: 显示一列带有小图标的itemLVS_REPORT: 显示item详细资料直观的理解:windows资源管理器,“查看”标签下的“大图标,小图标,列表,详细资料”2. 设置listctrl 风格及扩展风格LONG lStyle;lStyle = GetWindowLong(m_list.m_hWnd, GWL_STYLE);//获取当前窗口stylelStyle &= ~LVS_TYPEMASK; //清除显示方式位lStyle |= LVS_REPORT; //设置styleSetWindowLong(m_list.m_hWnd, GWL_STYLE, lStyle);//设置styleDWORD 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/library/default.asp?url=/libr ary/en-us/wceshellui5/html/wce50lrflistviewstyles.asp3. 插入数据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”);//设置数据4. 一直选中item选中style中的Show selection always,或者在上面第2点中设置LVS_SHOWSELALWAYS5. 选中和取消选中一行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. 得到listctrl中所有行的checkbox的状态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("第%d行的checkbox为选中状态"), i);AfxMessageBox(str);}}7. 得到listctrl中所有选中行的序号方法一:CString str;for(int i=0; i<m_list.GetItemCount(); 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}}8. 得到item的信息TCHAR szBuf[1024];LVITEM lvi;lvi.iItem = nItemIndex;lvi.iSubItem = 0;lvi.mask = LVIF_TEXT;lvi.pszText = szBuf;hTextMax = 1024;m_list.GetItem(&lvi);关于得到设置item的状态,还可以参考msdn文章Q173242: Use Masks to Set/Get Item States in CListCtrl /kb/173242/en-us9. 得到listctrl的所有列的header字符串内容LVCOLUMN lvcol;char str[256];int nColNum;CString strColumnName[4];//假如有4列nColNum = 0;lvcol.mask = LVCF_TEXT;lvcol.pszText = str;hTextMax = 256;while(m_list.GetColumn(nColNum, &lvcol)){strColumnName[nColNum] = lvcol.pszText;nColNum++;}10. 使listctrl中一项可见,即滚动滚动条m_list.EnsureVisible(i, FALSE);11. 得到listctrl列数int nHeadNum = m_list.GetHeaderCtrl()->GetItemCount();12. 删除所有列方法一:while ( m_list.DeleteColumn (0))因为你删除了第一列后,后面的列会依次向上移动。
MFC中ListControl控件的使用及实时显示系统时间的方法
MFC 中 List Control控件的使用及实时显示系统时间的方法 .(一) List Control控件的使用新近开发了一个摄像机标定的MFC 程序,标定完成后期望将求得的摄像机参数直观地显示到应用程序的界面上来。
起初的方案是为每一个参数都建立一个Edit 控件,并对每一个控件设定一个控制变量,将该变量与相应参数对应起来。
这样做是可行的,但当参数众多时比较繁琐。
鉴于此,决定在程序中使用List Control控件,将参数以List 的形式呈现在界面上。
以下是我在基于对话框的MFC 程序中添加List Control 控件的步骤。
1.新加ListControl控件,属性中的style属性页下的View 选择 Report 。
并设置其对应的控制变量如:m_ListCtrl 。
2.初始化,即设置列。
m_ListCtrl.InsertColumn(0," 参数名 "); // 插入列m_ListCtrl.InsertColumn(1," 参数值 ");m_ListCtrl.InsertColumn(2," 备注 "); CRect rect3;m_ListCtrl.GetClientRect(rect3); //获得当前客户区信息m_ListCtrl.SetColumnWidth(0,rect3.Width()/4); //设置列的宽度。
m_ListCtrl.SetColumnWidth(1,rect3.Width()*2/4);m_ListCtrl.SetColumnWidth(2,rect3.Width()/4);这部分初始化操作,最好放在对话框类的OnInitDialog()函数里,自动初始化。
3.插入数据m_ListCtrl.InsertItem(0,"参数1"); //插入第一个数据,即第 0 条数据。
先插入,然后在修改其他的信息。
mfc中listcontrol控件
MFC中ListControl控件介绍MFC(Microsoft Foundation Class)是一种用于开发Windows桌面应用程序的C++框架。
在MFC中,ListControl是一种常用的控件,用于以列表形式显示数据。
本文将介绍MFC中的ListControl控件,包括其基本功能、使用方法以及常见应用场景。
ListControl的基本功能ListControl控件通常用于显示大量数据,并提供滚动、排序和编辑等功能。
它可以容纳多列的数据,在每个单元格中显示文本、图标或其他类型的数据。
ListControl的创建与添加数据在MFC中,创建ListControl控件的方法如下: 1. 在对话框资源中添加一个ListControl控件; 2. 使用Class Wizard添加一个ListControl成员变量,保存对该控件的引用。
ListControl中的数据可以通过以下方式添加: 1. 使用InsertItem函数添加新的行; 2. 使用SetItemText函数设置每个单元格的文本。
ListControl的基本操作ListControl提供了一系列方法和消息来处理用户交互,并对列表数据进行操作。
以下是一些常见的基本操作: - 添加数据:使用InsertItem和SetItemText函数添加新行和设置单元格数据; - 删除数据:使用DeleteItem函数删除选定行; - 编辑数据:使用EditLabel函数编辑选定行的文本; - 排序数据:使用SortItems函数按列排序列表项; - 选择数据:使用SetItemState函数设置选中行; - 获取数据:使用GetItemText函数获取指定行和列的文本数据。
ListControl的高级功能除了基本功能外,ListControl还提供了一些高级功能,可以增强用户体验和数据展示效果:ListControl可以以图标形式显示数据,并提供多种视图模式,如大图标、小图标和列表视图。
mfc list control简单使用说明
mfc list control简单使用说明MFC(Microsoft Foundation Classes)是Microsoft提供的一个C++面向对象的应用程序框架,用于开发Windows应用程序。
MFC的List Control是其中一个常用的控件,用于展示和管理数据列表。
使用MFC List Control之前,需要先创建一个对话框或视图类,并在资源编辑器中添加一个List Control控件。
我们可以通过以下几个步骤简单地使用MFC List Control控件:1. 设置列标头:使用InsertColumn函数,我们可以设置List Control的列标头。
例如,如果我们想要一个包含“姓名”和“年龄”的列表,可以这样做:```cppm_listCtrl.InsertColumn(0, _T("姓名"), LVCFMT_LEFT, 100);m_listCtrl.InsertColumn(1, _T("年龄"), LVCFMT_LEFT, 100);```2. 添加数据项:使用InsertItem函数,我们可以在List Control中添加数据项。
每个数据项将代表列表中一行的数据。
例如,我们可以这样添加一个名为“张三”,年龄为25的数据项:```cppint nIndex = m_listCtrl.InsertItem(0, _T("张三"));m_listCtrl.SetItemText(nIndex, 1, _T("25"));```3. 更改数据项内容:使用SetItemText函数,我们可以修改List Control中数据项的内容。
例如,如果我们想修改“张三”的年龄为30,可以使用以下代码:```cppm_listCtrl.SetItemText(nIndex, 1, _T("30"));```4. 删除数据项:使用DeleteItem函数,我们可以删除List Control中的数据项。
MFC ListControl控件用法
M F C L i s t C o n t r o l控件用法------------------------------------------作者xxxx------------------------------------------日期xxxxMFC ListControl控件用法开发环境:visual studio 20081、新建一个基于对话框的MFC应用程序。
在对话框上拖放一个listcontrol控件和一个button控件,给listcontrol控件添加一个名为m_StoreItems的变量。
添加button的点击响应函数。
void CDepartmentStoreDlg::OnBnClickedNewitem(){// TODO: 在此添加控件通知处理程序代码srand((unsigned)time(NULL));TCHAR strNumber[20];//这里不要定义为char或者CString类型,不然后面 lvItem.pszText会报错。
int number1 = rand() % 100;int number2 = rand() % 100;wsprintf(strNumber,_T("%d-%d"), number1, number2);LVITEM lvItem;lvItem.mask = LVIF_TEXT;lvItem.iItem = 0;lvItem.iSubItem = 0;lvItem.pszText = strNumber;m_StoreItems.InsertItem(&lvItem);}运行效果如下,效果相当于listbox控件,就是一个列表,一行一行的:现在设置为报表视图,显示成表格形式。
在初始化函数里添加:BOOL CDepartmentStoreDlg::OnInitDialog(){CDialog::OnInitDialog();// TODO: 在此添加额外的初始化代码m_StoreItems.SetView(LVS_REPORT);//如果在属性里面设置了listview的view是report,这里就没必要了,默认的是iconLVCOLUMN lvColumn;lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH; //掩码设置了fmt值、显示列标题和指定宽度lvColumn.fmt = LVCFMT_LEFT; //设置作对其模式lvColumn.cx = 120; //设置标题的宽度为120个像素lvColumn.pszText = _T("Full Name"); //设置标题(列名)m_StoreItems.InsertColumn(0, &lvColumn); //添加列,索引为0 lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH; lvColumn.fmt = LVCFMT_LEFT;lvColumn.cx = 100;lvColumn.pszText = _T("Profession");m_StoreItems.InsertColumn(1, &lvColumn);lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH; lvColumn.fmt = LVCFMT_LEFT;lvColumn.cx = 80;lvColumn.pszText = _T("Fav Sport");m_StoreItems.InsertColumn(2, &lvColumn);lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH; lvColumn.fmt = LVCFMT_LEFT;lvColumn.cx = 75;lvColumn.pszText = _T("Hobby");m_StoreItems.InsertColumn(3, &lvColumn);}下面给报表添加点实际的内容。
ListCtrl控件使用方法和经验大全
本文全面介绍了如何编辑List Control里面的任何子项介绍内容有点多,译出来也没多大意思,大致就是说一个VC程序员会常常碰到List Control,List Control有很多方法可以显示数据,可List Control里默认的没有编辑功能,故在此智短文里,那个牛叉的人教你怎么实现这个功能。
这篇文章是基于VC MFC滴,用别的功具的娃们当然也可以看看,呵呵,不多说,先实现图上ok exit两个按钮:void CMultipleColumnsDlg::OK(){CDialog::EndDialog (0); // Add this line}void CMultipleColumnsDlg::OnExit(){CDialog::EndDialog (0); // Add this line}接下来添加一个ListCtrl控件,记得把ListCtrl的style设置成Report,这个是为了实现我们要多行显示的功能然后增加一个文本框Edit Box去掉它的Border style属性增加一个InsertItems() 成员函数,用来写入ListControl的显示内容void CMultipleColumnsDlg::InsertItems(){HWND hWnd = ::GetDlgItem(m_hWnd, IDC_LIST1);// Set the LVCOLUMN structure with the required// column informationLVCOLUMN list;list.mask = LVCF_TEXT |LVCF_WIDTH|LVCF_FMT |LVCF_SUBITEM;list.fmt = LVCFMT_LEFT;list.cx = 50;list.pszText = "S.No";list.iSubItem = 0;//Inserts the column::SendMessage(hWnd,LVM_INSERTCOLUMN, (WPARAM)0,(WPARAM)&list);list.cx = 100;list.pszText = "Name";list.iSubItem = 1;::SendMessage(hWnd ,LVM_INSERTCOLUMN, (WPARAM)1,(WPARAM)&list);list.cx = 100;list.pszText = "Address";list.iSubItem = 2;::SendMessage(hWnd ,LVM_INSERTCOLUMN, (WPARAM)1,(WPARAM)&list);list.cx = 100;list.pszText = "Country";list.iSubItem = 2;::SendMessage(hWnd ,LVM_INSERTCOLUMN, (WPARAM)1,(WPARAM)&list);// Inserts first Row with four columns .SetCell(hWnd,"1",0,0);SetCell(hWnd,"Prabhakar",0,1);SetCell(hWnd,"Hyderabad",0,2);SetCell(hWnd,"India",0,3);// Inserts second Row with four columns .SetCell(hWnd,"2",1,0);SetCell(hWnd,"Uday",1,1);SetCell(hWnd,"Chennai",1,2);SetCell(hWnd,"India",1,3);}自定义的SetCell( ) 函数,用来实现插入数据用的void CMultipleColumnsDlg::SetCell(HWND hWnd1, CString value, int nRow, int nCol){TCHAR szString [256];wsprintf(szString,value ,0);//Fill the LVITEM structure with the//values given as parameters.LVITEM lvItem;lvItem.mask = LVIF_TEXT;lvItem.iItem = nRow;lvItem.pszText = szString;lvItem.iSubItem = nCol;if(nCol >0)//set the value of listItem::SendMessage(hWnd1,LVM_SETITEM,(WPARAM)0,(WPARAM)&lvItem);else//Insert the value into ListListView_InsertItem(hWnd1,&lvItem);}//通过行和列得到项目里面的数据CString CMultipleColumnsDlg::GetItemText(HWND hWnd, int nItem, int nSubItem) const{LVITEM lvi;memset(&lvi, 0, sizeof(LVITEM));lvi.iSubItem = nSubItem;CString str;int nLen = 128;int nRes;do{nLen *= 2;hTextMax = nLen;lvi.pszText = str.GetBufferSetLength(nLen);nRes = (int)::SendMessage(hWnd,LVM_GETITEMTEXT, (WPARAM)nItem,(LPARAM)&lvi);str.ReleaseBuffer();return str;}//为窗口类添加两个成员变量:int nItem, nSubItem;用Class wizard 添加NM_CLICK 响应,当用户点击任何位置时,就会对应出现一个Edit Box,并可以修改数据void CMultipleColumnsDlg::OnClickList(NMHDR* pNMHDR, LRESULT* pResult){Invalidate();HWND hWnd1 = ::GetDlgItem (m_hWnd,IDC_LIST1);LPNMITEMACTIVATE temp = (LPNMITEMACTIVATE) pNMHDR;RECT rect;//get the row numbernItem = temp->iItem;//get the column numbernSubItem = temp->iSubItem;if(nSubItem == 0 || nSubItem == -1 || nItem == -1)return ;//Retrieve the text of the selected subItem//from the listCString str = GetItemText(hWnd1,nItem ,nSubItem);RECT rect1,rect2;// this macro is used to retrieve the Rectanle// of the selected SubItemListView_GetSubItemRect(hWnd1,temp->iItem,temp->iSubItem,LVIR_BOUNDS,&rect);//Get the Rectange of the listControl::GetWindowRect(temp->hdr.hwndFrom,&rect1);//Get the Rectange of the Dialog::GetWindowRect(m_hWnd,&rect2);int x=rect1.left-rect2.left;int y=rect1.top-rect2.top;if(nItem != -1)::SetWindowPos(::GetDlgItem(m_hWnd,IDC_EDIT1),HWND_TOP,rect.left+x,rect.top+4,rect.right-rect.left - 3,rect.bottom-rect.top -1,NULL);::ShowWindow(::GetDlgItem(m_hWnd,IDC_EDIT1),SW_SHOW);::SetFocus(::GetDlgItem(m_hWnd,IDC_EDIT1));//Draw a Rectangle around the SubItem::Rectangle(::GetDC(temp->hdr.hwndFrom),rect.left,rect.top-1,rect.right,rect.bottom);//Set the listItem text in the EditBox::SetWindowText(::GetDlgItem(m_hWnd,IDC_EDIT1),str);*pResult = 0;}To handle the ENTER key we need to write the virtual function OnOk(响应ENTER 键)afx_msg void OnOK();In MultipleColumnsDlg.cpp write the following code.// This function handles the ENTER keyvoid CMultipleColumnsDlg::OnOK(){CWnd* pwndCtrl = GetFocus();// get the control ID which is// presently having the focusint ctrl_ID = pwndCtrl->GetDlgCtrlID();CString str;switch (ctrl_ID){ //if the control is the EditBoxcase IDC_EDIT1://get the text from the EditBoxGetDlgItemText(IDC_EDIT1,str);//set the value in the listContorl with the//specified Item & SubItem valuesSetCell(::GetDlgItem (m_hWnd,IDC_LIST1),str,nItem,nSubItem);::SendDlgItemMessage(m_hWnd,IDC_EDIT1,WM_KILLFOCUS,0,0);::ShowWindow(::GetDlgItem(m_hWnd,IDC_EDIT1),SW_HIDE);break;default:break;}}最后一步在OnInitDialog 中设置List Control的样式ListView_SetExtendedListViewStyle(::GetDlgItem(m_hWnd,IDC_LIST1),LVS_EX_FULLROWSELECT |LVS_EX_GRIDLINES);InsertItems();::ShowWindow(::GetDlgItem(m_hWnd,IDC_EDIT1),SW_HIDE);IntroductionAlmost every one of us who are programming in VC++ , will come across the List control. There are many cases where there is a need to represent data in List Control in multiple columns. By default it is not possible to modify the data in the List control itself. In this small article I am putting a simple way to edit any value in any column in a Report style List control. The logic here is simple, whenever user clicks on an sub-item which he wants to modify at that place I am displaying a edit box and allowing to modify the value. Once modified and by clicking the ENTER key, the updated value is set in the List control. Here I am assuming the user is familiar with VC++ and using Class WizardImplementation steps:ing MFC AppWizard, create a Dialog Based application. Give theapplication nam e as MultipleColumns. By default the wizard adds OK andCancel buttons to the Dialog, Remove these two butt ons.2.Now Add a List-Control and in properties change the style to Report, thisstyle is necessary if we want m ultiple columns3.Add two buttons to the Dialog and name them as OK and Exit4.Add one Edit box and in the properties remove the Border styleing the Class Wizard add the m essage handlers for the OK and ExitButtons. Add the following code to those functionsCollapse Copy Codevoid CMultipleColumnsDlg::OK(){CDialog::EndDialog (0); // Add this line}Collapse Copy Codevoid CMultipleColumnsDlg::OnExit(){CDialog::EndDialog (0); // Add this line}6.Add a function called InsertItems() to the CMulipleColumnsDlg class.Collapse Copy Codevoid InsertItems();In the function handler add the following codeCollapse Copy Code// This function inserts the default values// into the listControlvoid CMultipleColumnsDlg::InsertItems(){HWND hWnd = ::GetDlgItem(m_hWnd, IDC_LIST1);// Set the LVCOLUMN structure with the required// column informationLVCOLUMN list;list.mask = LVCF_TEXT |LVCF_WIDTH|LVCF_FMT |LVCF_SUBITEM;list.fmt = LVCFMT_LEFT;list.cx = 50;list.pszText = "S.No";list.iSubItem = 0;//Inserts the column::SendMessage(hWnd,LVM_INSERTCOLUMN,(WPARAM)0,(WPARAM)&list);list.cx = 100;list.pszText = "Name";list.iSubItem = 1;::SendMessage(hWnd ,LVM_INSERTCOLUMN,(WPARAM)1,(WPARAM)&list);list.cx = 100;list.pszText = "Address";list.iSubItem = 2;::SendMessage(hWnd ,LVM_INSERTCOLUMN,(WPARAM)1,(WPARAM)&list);list.cx = 100;list.pszText = "Country";list.iSubItem = 2;::SendMessage(hWnd ,LVM_INSERTCOLUMN,(WPARAM)1,(WPARAM)&list);// Inserts first Row with four columns .SetCell(hWnd,"1",0,0);SetCell(hWnd,"Prabhakar",0,1);SetCell(hWnd,"Hyderabad",0,2);SetCell(hWnd,"India",0,3);// Inserts second Row with four columns .SetCell(hWnd,"2",1,0);SetCell(hWnd,"Uday",1,1);SetCell(hWnd,"Chennai",1,2);SetCell(hWnd,"India",1,3);// Inserts third Row with four columns .SetCell(hWnd,"3",2,0);SetCell(hWnd,"Saradhi",2,1);SetCell(hWnd,"Bangolore",2,2);SetCell(hWnd,"India",2,3);// Inserts fourth Row with four columns .SetCell(hWnd,"4",3,0);SetCell(hWnd,"Surya",3,1);SetCell(hWnd,"Calcutta",3,2);SetCell(hWnd,"India",3,3);}7.Add another function called SetCell( ) to the CMultipleColumnsDlgclassCollapse Copy Codevoid SetCell(HWND hWnd1, CString value, int nRow, int nCol);In the function handler add the following codeCollapse Copy Code// This function set the text in the specified// SubItem depending on the Row and Column valuesvoid CMultipleColumnsDlg::SetCell(HWND hWnd1,CString value, int nRow, int nCol){TCHAR szString [256];wsprintf(szString,value ,0);//Fill the LVITEM structure with the//values given as parameters.LVITEM lvItem;lvItem.mask = LVIF_TEXT;lvItem.iItem = nRow;lvItem.pszText = szString;lvItem.iSubItem = nCol;if(nCol >0)//set the value of listItem::SendMessage(hWnd1,LVM_SETITEM,(WPARAM)0,(WPARAM)&lvItem);else//Insert the value into ListListView_InsertItem(hWnd1,&lvItem);}8.Add one more function called GetItemText() to the sam e ClassCollapse Copy CodeCString GetItemText(HWND hWnd, int nItem, int nSubItem) const;Inside the function add the following codeCollapse Copy Code//this function will returns the item//text depending on the item and SubItem IndexCString CMultipleColumnsDlg::GetItemText(HWND hWnd, int nItem, int nSubItem) const{LVITEM lvi;memset(&lvi, 0, sizeof(LVITEM));lvi.iSubItem = nSubItem;CString str;int nLen = 128;int nRes;do{nLen *= 2;hTextMax = nLen;lvi.pszText = str.GetBufferSetLength(nLen);nRes = (int)::SendMessage(hWnd,LVM_GETITEMTEXT, (WPARAM)nItem,(LPARAM)&lvi);str.ReleaseBuffer();return str;}9.Also add two m ember variables to the CMultipleColumnsDlg class whichare of type intCollapse Copy Codeint nItem, nSubItem;10.From the Class wizard add NM_CLICK notification to the List control. Insidethe function handler write the following codeCollapse Copy Code//This function Displays an EditBox at the position//where user clicks on a particular SubItem with//Rectangle are equal to the SubItem, thus allows to//modify the valuevoid CMultipleColumnsDlg::OnClickList(NMHDR* pNMHDR, LRESULT* pResult){Invalidate();HWND hWnd1 = ::GetDlgItem (m_hWnd,IDC_LIST1);LPNMITEMACTIVATE temp = (LPNMITEMACTIVATE) pNMHDR;RECT rect;//get the row numbernItem = temp->iItem;//get the column numbernSubItem = temp->iSubItem;if(nSubItem == 0 || nSubItem == -1 || nItem == -1)return ;//Retrieve the text of the selected subItem//from the listCString str = GetItemText(hWnd1,nItem ,nSubItem);RECT rect1,rect2;// this macro is used to retrieve the Rectanle// of the selected SubItemListView_GetSubItemRect(hWnd1,temp->iItem,temp->iSubItem,LVIR_BOUNDS,&rect);//Get the Rectange of the listControl::GetWindowRect(temp->hdr.hwndFrom,&rect1);//Get the Rectange of the Dialog::GetWindowRect(m_hWnd,&rect2);int x=rect1.left-rect2.left;int y=rect1.top-rect2.top;if(nItem != -1)::SetWindowPos(::GetDlgItem(m_hWnd,IDC_EDIT1),HWND_TOP,rect.left+x,rect.top+4,rect.right-rect.left - 3,rect.bottom-rect.top -1,NULL);::ShowWindow(::GetDlgItem(m_hWnd,IDC_EDIT1),SW_SHOW);::SetFocus(::GetDlgItem(m_hWnd,IDC_EDIT1));//Draw a Rectangle around the SubItem::Rectangle(::GetDC(temp->hdr.hwndFrom),rect.left,rect.top-1,rect.right,rect.bottom);//Set the listItem text in the EditBox::SetWindowText(::GetDlgItem(m_hWnd,IDC_EDIT1),str);*pResult = 0;}11.To handle the ENTER key we need to write the virtual function OnOk in theMultipleColumnsDlg.h, so add the following as protected m emberCollapse Copy Codeafx_msg void OnOK();In MultipleColumnsDlg.cpp write the following code.Collapse Copy Code// This function handles the ENTER keyvoid CMultipleColumnsDlg::OnOK(){CWnd* pwndCtrl = GetFocus();// get the control ID which is// presently having the focusint ctrl_ID = pwndCtrl->GetDlgCtrlID();CString str;switch (ctrl_ID){ //if the control is the EditBoxcase IDC_EDIT1://get the text from the EditBoxGetDlgItemText(IDC_EDIT1,str);//set the value in the listContorl with the//specified Item & SubItem valuesSetCell(::GetDlgItem (m_hWnd,IDC_LIST1),str,nItem,nSubItem);::SendDlgItemMessage(m_hWnd,IDC_EDIT1,WM_KILLFOCUS,0,0);::ShowWindow(::GetDlgItem(m_hWnd,IDC_EDIT1),SW_HIDE);break;default:break;}}12.The last step in the im plementation is add the following code in side theOnInitDialog functionCollapse Copy Code//Set the style to listControlListView_SetExtendedListViewStyle(::GetDlgItem(m_hWnd,IDC_LIST1),LVS_EX_FULLROWSELECT |LVS_EX_GRIDLINES);InsertItems();::ShowWindow(::GetDlgItem(m_hWnd,IDC_EDIT1),SW_HIDE);VC/MFC之ListCtrl控件使用经验总结以下未经说明,listctrl默认view 风格为report相关类及处理函数MFC:CListCtrl类SDK:以“ListView_”开头的一些宏。
list control控件 用法
list control控件用法
List 控件是Visual Basic中的一种新的控件,可以把项目以列表的
形式显示出来,可以把相同的项目放在一起,它的提供了四种形式的列表:普通列表框、树形列表框、多项选择列表框和选项卡列表框。
1、普通列表框:此列表框用于显示项目列表,可以在列表中单独选
择一项,或者按住Shift或Ctrl键实现多项选择:
2、树形列表框:此列表框可以用来显示树形结构的数据,可以在节
点展开并选择里面的某一项:
3、多项选择列表框:可以把列表框选择模式由普通模式切换成多项
选择模式,即允许用户在列表框中选择多个项目:
4、选项卡列表框:列表框也可以以选项卡为基础,使用此类列表框
可以在每一个选项卡上显示不同的项目,用户可以点击其中任意一个选项
卡来选择项目:
使用List控件,需要在程序中设置列表中的项目,即:通过使用AddItem 方法向列表控件中添加项目,可以把要列出的所有条目添加到
List控件中。
添加内容的方法也很简单:在List控件的内部把输入的字
符串按照“项目1,项目2”的形式添加进去即可。
