VC++中listcontrol的用法

合集下载

VC++中list的使用方法总结 .

VC++中list的使用方法总结 .

这几天在做图像处理方面的研究,其中有一部分是关于图像分割方面的,图像目标在分割出来之后要做进一步的处理,因此有必要将目标图像的信息保存在一个变量里面,一开始想到的是数组,但是马上就发现使用数组的缺点:数组长度固定,动态分配内存很容易导致错误发生。

最重要的一点是我要保存目标图像的每一点的坐标值,使用数组就有点无能为力了。

因此到百度、Google大神上面找思路,终于被我发现在c++的标准库里面还有这么一个模板类:list,下面就是对找到的资料的汇总和加工。

vc6自带的msdn帮助文档的解释以下是引自msdn帮助文档(中文是我自己翻译的,错误之处请包涵。

):The template class describes an object that controls a varying-length sequence of elements of type T. The sequence is stored as a bidirectional linked list of elements, each containing a member of type T.本模板类描述了一个对象,这个对象是类型为T的可变长度的序列元素。

这个序列采用双向链表的方式存储每一个元素,其中每一个元素的数据流行都是T。

The object allocates and frees storage for the sequence it controls through a protected object named allocator, of class A. Such an allocator object must have the same external interface as an object of template class allocator. Note that allocatoris not copied when the object is assigned.对序列对象的分配和释放操作通过一个受保护的对象allocator进行。

VC6.0常用控件使用方法

VC6.0常用控件使用方法

VC6.0常用控件使用方法 26、List Box---------------列表框控件--类CListBox(1)插入项m_list.AddString(string);(2)设置当前选择项m_list.SetCurSel(项索引);(3)获取当前选择项int nIndex = m_list.GetCurSel();m_list.GetText(nIndex, string);(4)删除一项m_list.DeleteString(项索引);(5)删除所有项m_list.ResetContent();(6)获取总项数m_list.GetCount()(7)List Box的选项前面加复选框(Check Box)a.风格声明时用类CCheckListBox代替CListBox,即CCheckListBox m_list;而不是CListBox m_list;属性对话框->Styles->Owner draw设为Fixed属性对话框->Styles->勾选Has stringsb.设置选择void SetCheck( int nIndex, int nCheck );ParametersnIndexIndex of the item whose check box is to be set.nCheckState of the check box: 0 for clear, 1 for checked, and 2 for indeterminate.c.获取选择int GetCheck( int nIndex );ParametersnIndexIndex of the item whose check status is to be retrieved.Return ValueZero if the item is not checked, 1 if it is checked, and 2 if it is indeterminate.7、List Control----------列表框扩展控件--类CListCtrl(1)样式:属性对话框框->Styles->Format有4,分别是Icon/Small Icon/List/Report;(2)Report格式设置扩展风格DWORD dwStyle = m_list.GetExtendedStyle();dwStyle |= LVS_EX_FULLROWSELECT; // 选中某行使整行高亮(只适用与report风格的listctrl)dwStyle |= LVS_EX_GRIDLINES; // 网格线(只适用与report风格的listctrl)m_list.SetExtendedStyle(dwStyle);(3)Report格式插入列m_list.InsertColumn(1, "列一", LVCFMT_RIGHT, 150);m_list.InsertColumn(2, "列二", LVCFMT_LEFT, 100);m_list.InsertColumn(3, "列三", LVCFMT_LEFT, 100);m_list.InsertColumn(4, "列四", LVCFMT_LEFT, 200);m_list.InsertColumn(5, "ID", LVCFMT_CENTER, 0);(4)Report格式插入一行数据int nIndex = m_list.GetItemCount();m_list.InsertItem(nIndex, s1);m_list.SetItemText(nIndex, 1, s2);m_list.SetItemText(nIndex, 2, s3);m_list.SetItemText(nIndex, 3, s4);m_list.SetItemText(nIndex, 4, s5);(5)Report格式删除所有行m_list.DeleteAllItems();(6)Report格式获取某行某列数据CString sID = m_list.GetItemText(行索引,列索引);(7)Report格式删除选择行,多选时可用循环。

VC中CListCtrl中的LVCOLUMN和LVITEM详细介绍

VC中CListCtrl中的LVCOLUMN和LVITEM详细介绍

VC中CListCtrl中的LVCOLUMN和LVITEM详细介绍2011-10-09 18:56列表视图控件(List Control)列表视图控件是一种非常常用的控件,在需要以报表形式显示数据时,列表控件通常是最好的选择,许多专用的数据报表控件,也是在它的基础上派生而来。

与树视图类似,列表控件可以由多个子项目组成,并且支持大图标、小图标、列表和报表4种方式显示信息,如图1所示。

图1 列表视图的4种显示方式列表视图包含一个项目列表,而其中每个项目由图标、项目名称和多个子项组成,每一个子项所包含的项目的数目必须相同,属性相同的每个子项显示在同一个列中。

列表视图控件有两个重要的数据结构LVCOLUMN和LVITEM。

LVCOLUMN用于定义报表方式下的“列”的结构;LVITEM用于定义“项”的结构。

这两个结构的定义及说明如下:typedef struct _LVCOLUMN {UINT mask; //说明此结构中哪些成员是有效的int fmt; //列的对齐方式int cx; //列的初始宽度LPTSTR pszText; //列的标题int cchTextMax; //pszText所指向的缓冲区的大小int iSubItem; //与列关联的子项的索引值,从0开始int iImage; //与列关联的图像列表中指定图像的索引值int iOrder; //第几列,0代表最左一列} LVCOLUMN, FAR *LPLVCOLUMN;typedef struct _LVITEM {UINT mask; //说明LVITEM结构中哪些成员有效int iItem; //项目的索引值(可以视为行号)从0开始int iSubItem; //子项的索引值(可以视为列号)从0开始UINT state; //子项的状态UINT stateMask; //状态有效的屏蔽位LPTSTR pszText; //主项或子项的名称int cchTextMax; //pszText所指向的缓冲区大小int iImage; //关联图像列表中指定图像的索引值LPARAM lParam; //程序定义的32位参数int iIndent; //表示图像位置缩进的单位} LVITEM, FAR *LPLVITEM;列表项常用的属性如下:View指定程序运行后列表视图控件最初显示的方式,可以设置为Icon(大图标)、SmallIcon(小图标)、List(列表)或Report(报表);Single selection表示每次只能选中一个项;Auto arrange使得项目在Icon和Small Icon显示方式下能够自动排序;Edit Labels表示可以编辑项目的卷标;No column header表示取消控件所有列的标题。

MFCCListCtrl控件使用总结整理

MFCCListCtrl控件使用总结整理

MFCCListCtrl控件使用总结整理一、基本操作:1. 创建CListCtrl实例,可以通过对话框资源添加控件或代码创建:```cppCListCtrl m_listCtrl;```2.设置列表样式,例如设置为报表样式:```cppm_listCtrl.Create(WS_VISIBLE , WS_CHILD , LVS_REPORT, CRect(10, 10, 500, 300), this, IDC_LIST_CTRL);```3.设置列表列头:```cppm_listCtrl.InsertColumn(0, _T("序号"), LVCFMT_LEFT, 50);m_listCtrl.InsertColumn(1, _T("姓名"), LVCFMT_LEFT, 100);m_listCtrl.InsertColumn(2, _T("年龄"), LVCFMT_LEFT, 50);```4.添加行数据:```cppint nItemIndex = m_listCtrl.InsertItem(0, _T("1"));m_listCtrl.SetItemText(nItemIndex, 1, _T("张三"));m_listCtrl.SetItemText(nItemIndex, 2, _T("20"));``````cppm_listCtrl.DeleteItem(nItemIndex); // 删除```二、高级操作:1.设置列宽:```cppm_listCtrl.SetColumnWidth(0, LVSCW_AUTOSIZE_USEHEADER);m_listCtrl.SetColumnWidth(1, LVSCW_AUTOSIZE);m_listCtrl.SetColumnWidth(2, LVSCW_AUTOSIZE);```2.设置行高:```cppm_listCtrl.SetItemHeight(0, 50);```3.设置行选中状态:```cppm_listCtrl.SetItemState(0, LVIS_SELECTED, LVIS_SELECTED);```4.设置行背景色:```cppm_listCtrl.SetItemState(0, LVIS_SELECTED , LVIS_FOCUSED, LVIS_SELECTED , LVIS_FOCUSED);m_listCtrl.SetSelectionMark(0);m_listCtrl.SetItemState(0, LVIS_SELECTED, LVIS_SELECTED);```5.设置行图标:```cppm_listCtrl.SetItemImage(nItemIndex, nImageIndex);```6.获取行数和列数:```cppint nItemCount = m_listCtrl.GetItemCount(;int nColumnCount = m_listCtrl.GetHeaderCtrl(->GetItemCount(;```7.获取行数据:```cppCString strItemText = m_listCtrl.GetItemText(nItemIndex, nSubItemIndex);```三、自定义操作:1.列排序:```cpp```2.行拖拽排序:```cppm_listCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT ,LVS_EX_GRIDLINES , LVS_EX_HEADERDRAGDROP);``````cppm_listCtrl.ModifyStyle(0, LVS_EDITLABELS);```4.行点击事件:```cppON_NOTIFY(NM_CLICK, IDC_LIST_CTRL, OnLButtonDown)```5.自定义绘制:```cppON_WM_DRAWITEM```四、注意事项和优化建议:1. 大量数据时,慎用SetRedraw方法来控制刷新。

mfc listctrl 选择单元格

mfc listctrl 选择单元格

mfc listctrl 选择单元格MFC(Microsoft Foundation Classes)是微软基于Windows操作系统提供的一套C++类库,用于开发图形用户界面(GUI)应用程序。

其中的ListCtrl是MFC中的一个列表控件,用于展示和编辑表格数据。

在ListCtrl中,选择单元格是一个常见的需求,本文将介绍MFC中如何选择单元格,并讨论一些相关的技术和问题。

ListCtrl提供了一种双击单元格进行编辑的机制,但没有直接支持选择单元格的功能。

为了实现选择单元格,我们可以借助一些其他的技术,在ListCtrl中模拟出选择单元格的效果。

一种常见的方法是通过自定义绘制来实现选择单元格的效果。

当鼠标点击ListCtrl时,我们可以获取到鼠标的位置,并根据位置计算出相应的行和列。

然后,我们可以通过对指定单元格的绘制进行特殊处理,比如修改背景颜色或者绘制边框,来表示该单元格被选中。

此外,我们还可以记录当前选中的单元格,以便后续的处理。

另一种方法是使用自定义的消息处理机制来实现选择单元格的功能。

当鼠标点击ListCtrl时,我们可以捕获鼠标消息,并通过对消息进行特殊处理来实现选择单元格的效果。

例如,我们可以在鼠标点击消息的处理函数中,获取到鼠标的位置,并根据位置计算出相应的行和列。

然后,我们可以通过修改ListCtrl的状态,将指定的单元格设置为选中状态。

这样,我们就可以在后续的操作中,根据ListCtrl的状态来判断是否选择了特定的单元格。

在实现选择单元格的过程中,还可能涉及到以下几个问题。

首先是如何处理鼠标事件。

由于ListCtrl是一个窗口部件,它可以接收鼠标事件。

我们可以在ListCtrl的父窗口中捕获和处理这些事件,以实现选择单元格的功能。

其次是如何获取到ListCtrl中的行和列的信息。

ListCtrl通常是以行为单位展示数据,我们可以通过调用ListCtrl的相关函数,如GetItemCount()和GetColumnCount()来获取到行数和列数。

MFC_VC_Tree_Control控件使用 详解

MFC_VC_Tree_Control控件使用 详解

MFC Tree Control控件详解1.InsertItem 添加节点参数: 文字,图标,父节点返回: HTREEITEM示例: 添加一系列节点HTREEITEM hItem = m_Tree.InsertItem("root",NULL,NULL);///root就是节点的标题int i,j;for (i=0;i<4;i++){HTREEITEM hSubItem = m_Tree.InsertItem("item",NULL,NULL,hItem);for (j=0;j<3;j++){m_Tree.InsertItem("subitem",NULL,NULL,hSubItem);}}InsertItem函数的第一个参数就是设置他的节点标题2.ModifyStyle 设置风格参数: 取消的风格,增加的风格示例: 在对话框初始化时设置风格BOOL CMfc1Dlg::OnInitDialog(){//...m_Tree.ModifyStyle(NULL,TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESA TROOT); }3.DeleteItem 删除节点4.DeleteAllItems 删除全部节点5.Expand 展开/收缩节点参数: 节点HTREEITEM,展开/收缩示例:m_Tree.Expand(hItem,TVE_EXPAND);/////////////////////////////////////////////////////////CTreeCtrl的概述cc++vcCTreeCtrl在三种不同情况下创建的方式1、如果要在对话框窗口上创建树形控件,你需要在对话框类中定义一个CTreeCtrl类型的成员变量。

2、如果树形控件是一个子窗口,你可以使用CTreeCtrl::Create()来构建树形空间对象。

mfc中listcontrol控件

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 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中的数据项。

VC list control控件数据导出到excel

VC list control控件数据导出到excel

VC list control控件数据导出到excelList control控件数据导出到excel,可采用下面的自定义导出函数.int ExportToExcel(CListCtrl *listctrl, CString sheetname){try{_Application excelapp;Workbooks books;_Workbook book;Worksheets sheets;_Worksheet sheet;Range usedRange;Range range;LPDISPATCH lpDisp;COleVariant vResult;COleVariant covTrue((short)TRUE),covFalse((short)FALSE),covOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);//初始化ole控件if (!AfxOleInit()){return FALSE;}//启动excel程序if(!excelapp.CreateDispatch("Excel.Application")){excelapp.ReleaseDispatch();return 1;}//创建单个工作表,并重命名为sheetnamebooks = excelapp.GetWorkbooks();book = books.Add(covTrue);sheets = book.GetWorksheets();// while(sheets.GetCount()!=1)// {// sheet = sheets.GetItem(COleVariant((short)1));// sheet.Delete();// }sheet = book.GetActiveSheet();sheet.SetName(sheetname);range = sheet.GetCells();//导出控制列标题到excelCHeaderCtrl *pHead = listctrl->GetHeaderCtrl();for(int i=0;i<pHead->GetItemCount();i++){LVCOLUMN lvc;char text[100];lvc.mask = LVCF_TEXT|LVCF_SUBITEM;lvc.pszText = text;hTextMax = 100;listctrl->GetColumn(i,&lvc);range.SetItem(COleVariant((long)1),COleVariant((long)(i+1)),COleVariant(lvc.pszText));}//导出控制数据到excelfor(int j=0;j<listctrl->GetItemCount();j++)for(i=0;i<pHead->GetItemCount();i++){char tmp[100];listctrl->GetItemText(j,i,tmp,100);range.SetItem(COleVariant((long)(j+2)),COleVariant((long)(i+1)),COleVariant(tmp));}//保存excel到指定位置excelapp.SetAlertBeforeOverwriting(FALSE);excelapp.SetDisplayAlerts(FALSE);CFileDialog file(FALSE,".xls",NULL,OFN_EXTENSIONDIFFERENT,"Workbooks files (*.xls)|*.xls|All Files(*.*)|*.*|",AfxGetMainWnd());CString m_filePath;if(file.DoModal()==IDOK){m_filePath=file.GetPathName();}book.SaveAs((COleVariant)(m_filePath),covOptional,covOptional,covOptional,covOptional,covOptional,0,covOptional,covOptional,covOptional,covOptional,covOptional);books.Close();excelapp.Quit();excelapp.ReleaseDispatch();return 0;}catch(…){return -1;}}。

mfc list control复选框的用法

mfc list control复选框的用法

mfc list control复选框的用法MFC(Microsoft Foundation Classes)提供了一种用于创建应用程序用户界面的C++类库。

MFC List Control是MFC库中的一个控件,用于显示和编辑多行列的文本或图标。

下面是MFC List Control复选框的用法:1. 创建MFC对话框应用程序或MFC窗口应用程序。

2. 打开资源编辑器,将一个List Control控件从工具栏拖放到对话框或窗口上。

3. 右键单击List Control控件,选择"属性",然后在属性窗口中设置List控件的样式为“Owner Draw Variable”。

4. 在类视图中双击对话框或窗口类,打开对应的.cpp文件。

5. 在对话框或窗口类的成员变量中添加一个List Control控件的变量,例如:CListCtrl m_listCtrl。

6. 在OnInitDialog或OnInitialUpdate函数中添加以下代码来设置List Control的列标题和样式:m_listCtrl.InsertColumn(0, _T("Column 1"), LVCFMT_LEFT, 100);m_listCtrl.InsertColumn(1, _T("Column 2"), LVCFMT_LEFT, 100);m_listCtrl.SetExtendedStyle(LVS_EX_CHECKBOXES);其中,InsertColumn函数用于插入列标题,第一个参数为列索引,第二个参数为列标题,第三个参数为列对齐方式,第四个参数为列宽度。

SetExtendedStyle 函数用于启用List Control的复选框功能。

7. 使用InsertItem函数添加列表项,使用SetItemText函数设置列表项的文本:int itemIndex = m_listCtrl.InsertItem(0, _T("Item 1"));m_listCtrl.SetItemText(itemIndex, 1, _T("Item 1 T ext"));其中,第一个参数为项索引,第二个参数为项文本。

listcontrol控件用法

listcontrol控件用法

listcontrol控件用法List Control控件是一个可用于显示和管理数据的Windows控件。

它提供了一种在列表中显示多个项目和对项目进行操作的方式。

以下是关于List Control控件的用法的详细解释。

一、List Control控件的基本用法2. 设置样式:通过设置控件的样式,可以控制List Control的外观和行为。

比如可以选择单选或多选模式,设置是否显示网格线等。

3. 添加列:使用InsertColumn函数可以添加列,每个列表项可以有多个列。

可以设置列的宽度、标题、对齐方式等属性。

4. 添加项:使用InsertItem函数可以向List Control控件中添加项。

可以设置每个列对应的文本、图标、状态等。

6. 删除项:使用DeleteItem函数可以删除选定的项。

7. 排序:可以通过SortItems函数对列表项进行排序,根据指定的列和排序方式进行排序。

8. 选择项:可以通过SetItemState函数设置列表项的选中状态,可以选择单个或多个项。

9. 获取项的数据:使用GetItemText函数可以获取列表项的文本值,也可以使用GetItemData函数获取项的附加数据。

二、List Control控件的高级用法1. 图标视图:通过设置控件的样式为LVS_ICON,可以在List Control中显示项目的图标。

可以使用SetImageList函数来设置图标显示时使用的图像列表。

2. 详细视图:通过设置控件的样式为LVS_REPORT,可以在List Control中以表格形式显示项目的详细信息。

可以使用SetExtendedStyle 函数设置列表的扩展样式。

3. 多选模式:通过设置控件的样式为LVS_MULTIPLESEL,可以启用多选模式。

可以使用GetSelectedCount和GetSelectedItem函数获取选中的项。

4. 拖放操作:通过处理控件的通知消息,可以实现拖放操作。

mfc中listcontrol用法

mfc中listcontrol用法

mfc中listcontrol用法MFC中的List Control是一种典型的列表框控件,它可以显示多列数据,并具备排序、选择、编辑等功能。

这篇文章将会阐述MFC中List Control的用法,以帮助读者更好地理解和运用这个控件。

一、创建List Control在MFC中,可以使用控件向导创建List Control,具体步骤如下:1. 打开Visual Studio;2. 创建一个MFC应用程序;3. 在创建对话框的过程中,添加一个List Control控件;4. 在对话框的OnInitDialog函数中,添加以下代码:CListCtrl* pListCtrl =(CListCtrl*)GetDlgItem(IDC_LIST1);pListCtrl->InsertColumn(0, _T("列1"), LVCFMT_LEFT, 60);pListCtrl->InsertColumn(1, _T("列2"), LVCFMT_LEFT, 100);pListCtrl->InsertColumn(2, _T("列3"), LVCFMT_LEFT, 100);pListCtrl->InsertItem(0, _T("第一行"));pListCtrl->SetItemText(0, 1, _T("第一行第二列"));pListCtrl->SetItemText(0, 2, _T("第一行第三列"));二、添加数据在创建好List Control之后,就可以通过以下代码添加数据:CListCtrl* pListCtrl =(CListCtrl*)GetDlgItem(IDC_LIST1);CString strText;strText = _T("第二行");int nRow = pListCtrl->InsertItem(1, strText);strText = _T("第二行第二列");pListCtrl->SetItemText(nRow, 1, strText);strText = _T("第二行第三列");pListCtrl->SetItemText(nRow, 2, strText);三、排序List Control可以通过自带的“排序”功能,对列表中的数据进行排序。

VC-MFCListcontrl实现分页显示

VC-MFCListcontrl实现分页显示

VC-MFCListcontrl实现分页显示vc中我在一个form里用ListCtrl通过ADO访问数据记录集并显示,发现当表的记录条数非常多的时候,切换到该form时候比较慢,我是在form的OnInitialUpdate()函数里打开记录集的m_pRecordsetCustomer->Open(_variant_t("Customer"),_variant_t((IDispatch*)m_pCo nnection,true),adOpen Keyset,adLockOptimistic,adCmdT able);请问是否有办法让程序切换到该form的时候快一些,画面没有停滞的感觉ListCtrl控件里我已经做了折中,是分页显示的,form初始化的时候只显示前100条记录,这个应该不影响速度的吧---------------------------------------------------------------主要问题在于10000条数据插入ListCtrl是非常缓慢的,你可以采用线程来取记录,实现相对复杂,也可以把插入数据的代码不要放在InittialUpdate中,因为这样你必须在数据全部插入后才可以看到窗口。

解决的方法是你可以在InittialUpdate中启动timer,在timer 中插入数据,而且第一次ontimer时就应该kill该timer。

辅助的措施还有:先m_list.SetRedraw(FALSE)数据插入完成后m_list.SetRedraw(TRUE)当然,你也需要一些表示你正在读取数据的辅助措施---------------------------------------------------------------如果一定要用listctrl可以用ado分页,速度能很大程度提升。

用Recordset的PageSize指定每页记录数操作AbsolutePage属性设置当前的页面。

list_control_使用技巧

list_control_使用技巧

c++:list control的用法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:为分配给该列的列索引。

VC中ListCtrl经验总结

VC中ListCtrl经验总结
lvitem.mask = LVIF_TEXT;
lvitem.iItem = nIndex ;
lvitem.iSubItem = 1;
lvitem.pszText =_T("毛毛2");
m_listctrl.SetItem(&lvitem);
LV_ITEM lvitem = {0};
lvitem.mask = LVIF_TEXT;
lvitem.iItem = nIndex ;
lvitem.iSubItemห้องสมุดไป่ตู้= 1;
lvitem.pszText =_T("毛毛2");
{
// TODO: Add your control notification handler code here
int nItem = -1;
LPNMITEMACTIVATE lpNMItemActivate = (LPNMITEMACTIVATE)pNMHDR;
TCHAR rgtsz[2][10] = {_T("列头1"), _T("列头2")};
LV_COLUMN lvcolumn;
CRect rect;
m_listctrl.GetWindowRect(&rect);
lvitemAdd.lParam = (LPARAM)hwnd;(某个窗口的窗口句柄)
if (m_listctrl.InsertItem(&lvitemAdd) != -1)
{
LV_ITEM lvitem = {0};
m_listctrl.InsertColumn(i, &lvcolumn);

ListCtrl控件数据动态修改(List Control)

ListCtrl控件数据动态修改(List Control)

:/KB/list/editing_subitems_in_listcontrol.aspx另自己写了一篇关于一些ListControl的基本操作(如何获得一项的值,如何选中一行而不是一列等)请查看:/anglecloudy/blog/item/d969fd29138093f999250a97.html Editing Sub-Items in List ControlBy s.prabhakarreddy本文全面介绍了如何编辑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 name as MultipleColumns. By default the wizard adds OK andCancel buttons to the Dialog, Remove these two buttons.2.Now Add a List-Control and in properties change the style to Report, thisstyle is necessary if we want multiple 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 message 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 same 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 member 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 memberCollapse 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 implementation 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);。

mfc的listctrl使用

mfc的listctrl使用

mfc的listctrl使用摘要:1.MFC 的ListCtrl 简介2.ListCtrl 的基本操作3.ListCtrl 的常用属性4.ListCtrl 的事件处理5.ListCtrl 的示例代码正文:一、MFC 的ListCtrl 简介MFC(Microsoft Foundation Class)是微软提供的一个用于开发Windows 应用程序的类库。

在MFC 中,ListCtrl 是一个常用的控件,用于显示一个列表,允许用户进行选择和编辑操作。

ListCtrl 控件的功能强大,使用灵活,广泛应用于各种应用程序中。

二、ListCtrl 的基本操作1.创建ListCtrl 控件在MFC 应用程序中,可以通过创建一个CListCtrl 对象来创建一个ListCtrl 控件。

例如:```cppCListCtrl m_listCtrl;```2.插入数据可以使用`InsertItem()`方法向ListCtrl 中插入数据。

例如:```cppm_listCtrl.InsertItem(0, _T("Item 1"));m_listCtrl.InsertItem(1, _T("Item 2"));```3.删除数据可以使用`DeleteItem()`方法从ListCtrl 中删除数据。

例如:```cppm_listCtrl.DeleteItem(0);```4.选择数据可以使用`SetCurSel()`方法设置当前选择的数据。

例如:```cppm_listCtrl.SetCurSel(0);```5.获取选中数据可以使用`GetCurSel()`方法获取当前选中的数据。

例如:```cppint nSel = m_listCtrl.GetCurSel();CString strSel = m_listCtrl.GetItemText(nSel, 0);```三、ListCtrl 的常用属性1.ListCtrl 的样式ListCtrl 的样式可以通过`SetStyle()`方法设置。

VC 虚拟列表

VC 虚拟列表

[VC/MFC]虚拟列表控件2008-10-07 19:12一、什么是虚拟列表控件虚拟列表控件是指带有LVS_OWNERDA TA风格的列表控件。

二、为什么使用虚拟列表控件我们知道,通常使用列表控件CListCtrl,需要调用InsertItem把要显示的数据插入列表中,之后我们就不必关心数据在哪里了,这是因为控件自己开辟了内存空间来保存这些数据。

现在假设我们要显示一个数据库,里面的信息量很大,有几十万条记录。

通常有两种方法解决这个问题:1是仅仅在ListCtrl中插入少量的数据,比如100个,然后通过[上一页][下一页]两个按钮进行控制,某一时刻显示的只是从xxx到xxx+100之间的记录。

2是把所有数据全部插入到ListCtrl中,然后让用户通过滚动来查看数据。

无疑,很多用户喜欢采用第二种方式,特别是对于已经排序的数据,用户只需用键盘输入某行的开头字符,就可以快速定位到某一行。

但是,如果这样做,InsertItem插入数据的过程将是很漫长的,而且用户会看到ListCtrl 刷新速度也很慢,而且所有数据都位于内存中消耗了大量的内存,当数据多达上万以后几乎是不能忍受的。

为此,mfc特别提供了虚拟列表的支持。

一个虚拟列表看起来和普通的ListCtrl一样,但是不用通过InsertItem来插入数据,它仅仅知道自己应该显示多少数据。

但是它如何知道要显示什么数据呢?秘密就在于当列表控件需要显示某个数据的时候,它向父窗口要。

假设这个列表控件包含100个元素,第10到20个元素(行)是可见的。

当列表控件重画的时候,它首先请求父窗口给它第10个元素的数据,父窗口收到请求以后,把数据信息填充到列表提供的一个结构中,列表就可以用来显示了,显示第10个数据后,列表会继续请求下一个数据。

在虚拟的样式下,ListCtrl可以支持多达DWORD个数据项。

(缺省的listctrl控件最多支持int个数据项)。

但是,虚拟列表的最大优点不在于此,而是它仅仅需要在内存中保持极少量的数据,从而加快了显示的速度。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

CListCtrl 使用技巧以下未经说明,listctrl默认view 风格为report相关类及处理函数MFC:CListCtrl类SDK:以“ListView_”开头的一些宏。

如ListView_InsertColumn1. 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=/library/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.SetItemT ext(nRow, 1, “jacky”);//设置数据4. 一直选中item选中style中的Show selection always,或者在上面第2点中设置LVS_SHOWSELALWAYS 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. 得到listctrl中所有行的checkbox的状态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);}}7. 得到listctrl中所有选中行的序号方法一:CString str;for(int i=0; i {if( m_list.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED ){str.Format(_T("选中了第%d行"), i);nbsp; 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);9. 得到listctrl的所有列的header字符串内容LVCOLUMN lvcol;char str[256];int nColNum;CString strColumnName[4];//假如有4列nColNum = 0;lvcol.mask = LVCF_TEXT;lvcol.pszText = str;hT extMax = 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))因为你删除了第一列后,后面的列会依次向上移动。

方法二:int nColumns = 4;for (int i=nColumns-1; i>=0; i--)m_list.DeleteColumn (i);13. 得到单击的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;}14. 判断是否点击在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;nbsp; lvinfo.pt = point;lvinfo.flags = LVHT_ABOVE;UINT nFlag;int nItem = m_list.HitT est(point, &nFlag);//判断是否点在checkbox上if(nFlag == LVHT_ONITEMSTATEICON){AfxMessageBox("点在listctrl的checkbox上");}*pResult = 0;}15. 右键点击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;}16. 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 hereCString sT emp;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。

相关文档
最新文档