MFCCListCtrl控件使用总结整理_第1页
MFCCListCtrl控件使用总结整理_第2页
MFCCListCtrl控件使用总结整理_第3页
MFCCListCtrl控件使用总结整理_第4页
MFCCListCtrl控件使用总结整理_第5页
已阅读5页,还剩26页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、MFC ListCtrl控件使用总结整理m_Local_FileList.EnsureVisible(FileList_row, FALSE; /使该行显示可见1、获取控件原来风格DWORD dwStyle = m_list.GetExtendedStyle(;2、更改控件扩展风格m_list.SetExtendedStyle(m_list.GetExtendedStyle( | LVS_EX_CHECKBOXES;注:若手动创建控件时,在create时的样式里,不能包含以上,应在create后,在更改控件扩展风格样式类型:dwStyle |= LVS_EX_FULLROWSELECT;/选中

2、某行使整行高亮(只适用与report风格的listctrl)dwStyle |= LVS_EX_GRIDLINES;/网格线(只适用与report风格的listctrl)dwStyle |= LVS_EX_ONECLICKACTIVATE ; /设置单击激活样式dwStyle |= LVS_EX_CHECKBOXES; 设置checkbox样式dwStyle |= LVS_EX_INFOTIP;/设备列表项显示提示信息m_Local_FileList.SetExtendedStyle(dwStyle;3、插入列表列名m_AlarmList.InsertColumn(0, 编号, LVCFMT_

3、LEFT, width/4;列名文字位置:LVCFMT_LEFT 居左 LVCFMT_RIGHT 居右 LVCFMT_CENTER居中4、设置列宽m_list.SetColumnWidth(列值, 宽度;5、插入列表第一行值,设置列表该行其他列值Int nRow = m_AlarmList.InsertItem(0, 信息一;m_AlarmList.SetItemText(nRow, 1, xx路口有滞留物!; int nItem = m_list.GetItemCount(; m_list.InsertItem(nItem, strFileName, nIcon; m_list.SetIte

4、mText(nItem, 1, strSize;6、设置指定行的绑定值,获取指定项绑定值m_list.SetItemData(int nItem, DWORD dwData ;m_list.GetItemData(int nItem;m_list.SetImageList(&m_listSmall,LVSIL_SMALL;/设置ImageListm_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,LV

5、CFMT_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;ListCtrl的使用获取当前被选中的行, 并取出该行指定列的值int nSelect = m_FileList.GetSelectionMark(;m_FileList.GetItemText(nSelect, 1, m_SelectData, sizeof(m_SelectData;/设置列表文字颜色m_AlarmLogList.SetTe

6、xtColor(RGB(0, 0, 255;/设置列表文字大小CFont font;VERIFY(font.CreateFont(12, / nHeight0, / nWidth0, / nEscapement0, / nOrientationFW_NORMAL, / nWeightFALSE, / bItalicFALSE, / bUnderline0, / cStrikeOutANSI_CHARSET, / nCharSetOUT_DEFAULT_PRECIS, / nOutPrecisionCLIP_DEFAULT_PRECIS, / nClipPrecisionDEFAULT_QUAL

7、ITY, / nQualityDEFAULT_PITCH | FF_SWISS, / nPitchAndFamily_T(Arial; / lpszFacenamem_AlarmLogList.SetFont(&font, TRUE;int nSelect = m_listDevice.GetSelectionMark(;MFC 中的CListCtrl简介 (2008-08-12 10:12 分类: MFC编程 以下未经说明,listctrl默认view 风格为report相关类及处理函数MFC:CListCtrl类SDK:以 “ListView_”开头的一些宏。如 ListView_Inse

8、rtColumnvoid OnDblclkList2(NMHDR* pNMHDR, LRESULT* pResult;ON_NOTIFY(NM_DBLCLK, IDC_LIST2, OnDblclkList2void CDlgTest:OnDblclkList2(NMHDR* pNMHDR, LRESULT* pResult / TODO: Add your control notification handler code here*pResult = 0;1. CListCtrl 风格 LVS_ICON: 为每个item显示大图标 LVS_SMALLICON: 为每个item显示小图标 L

9、VS_LIST: 显示一列带有小图标的item LVS_REPORT: 显示item详细资料 直观的理解:windows资源管理器,“查看”标签下的“大图标,小图标,列表,详细资料”2. 设置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;/设

10、置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 3. 插入数据 m_list.I

11、nsertColumn( 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(nIn

12、dex, 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

13、(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; AfxMessageBox(str; 方法二: POSITION pos = m_list.GetFirstSelectedItemPosition(; if (pos = NULL TRACE0(No

14、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 szBuf1024; LVITEM lvi; lvi.iItem = nItemIndex; lvi.iSubItem = 0; lvi.mask = LVIF_TEXT; lvi.pszText = szBuf; lvi

15、.cchTextMax = 1024; m_list.GetItem(&lvi; 关于得到设置item的状态,还可以参考msdn文章 Q173242: Use Masks to Set/Get Item States in CListCtrl 9. 得到listctrl的所有列的header字符串内容 LVCOLUMN lvcol; char str256; int nColNum; CString strColumnName4;/假如有4列 nColNum = 0; lvcol.mask = LVCF_TEXT; lvcol.pszText = str; lvcol.cchTextMax =

16、 256; while(m_list.GetColumn(nColNum, &lvcol strColumnNamenColNum = 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 nCol

17、umns = 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

18、 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

19、 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(dwP

20、os, 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; 15. 右键点击listctrl的item弹出菜单 添加li

21、stctrl控件的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

22、.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 =

23、(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 & (pNMListVi

24、ew-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 & (pNMLis

25、tView-uNewState & LVIS_SELECTED = LVIS_SELECTED sTemp.Format(%d got selected,pNMListView-iItem; *pResult = 0; 17. 得到另一个进程里的listctrl控件的item内容18. 选中listview中的item Q131284: How To Select a Listview Item Programmatically19. 如何在CListView中使用CListCtrl的派生类20. listctrl的subitem添加图标 m_list.SetExtendedStyle(LVS

26、_EX_SUBITEMIMAGES; m_list.SetItem(.; /具体参数请参考msdn21. 在CListCtrl显示文件,并根据文件类型来显示图标 网上找到的代码,share BOOL CTest6Dlg:OnInitDialog( CDialog:OnInitDialog(; HIMAGELIST himlSmall; HIMAGELIST himlLarge; SHFILEINFO sfi; char cSysDirMAX_PATH; CString strBuf; memset(cSysDir, 0, MAX_PATH; GetWindowsDirectory(cSysDi

27、r, MAX_PATH; strBuf = cSysDir; sprintf(cSysDir, %s, strBuf.Left(strBuf.Find(+1; himlSmall = (HIMAGELISTSHGetFileInfo (LPCSTRcSysDir, 0, &sfi, sizeof(SHFILEINFO, SHGFI_SYSICONINDEX | SHGFI_SMALLICON ; himlLarge = (HIMAGELISTSHGetFileInfo(LPCSTRcSysDir, 0, &sfi, sizeof(SHFILEINFO, SHGFI_SYSICONINDEX |

28、 SHGFI_LARGEICON; if (himlSmall & himlLarge :SendMessage(m_list.m_hWnd, LVM_SETIMAGELIST, (WPARAMLVSIL_SMALL, (LPARAMhimlSmall; :SendMessage(m_list.m_hWnd, LVM_SETIMAGELIST, (WPARAMLVSIL_NORMAL, (LPARAMhimlLarge; return TRUE; / return TRUE unless you set the focus to a control void CTest6Dlg:AddFile

29、s(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

30、 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, strS

31、ize; 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_SMALL

32、ICON | 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

33、; 22. listctrl内容进行大数据量更新时,避免闪烁 m_list.SetRedraw(FALSE; /更新内容 m_list.SetRedraw(TRUE; m_list.Invalidate(; m_list.UpdateWindow(; 或者参考 23. listctrl排序 Q250614:How To Sort Items in a CListCtrl in Report View24. 在listctrl中选中某个item时动态改变其icon或bitmapQ141834: How to change the icon or the bitmap of a CListCtrl

34、 item in Visual C+How to change the icon or the bitmap of a CListCtrl item in Visual C+View products that this article applies to.Article ID:141834Last Review:June 2, 2005Revision:3.0This article was previously published under Q141834NOTE: Microsoft Visual C+ NET (2002 supported both the managed cod

35、e model that is provided by the .NET Framework and the unmanaged native Windows code model. The information in this article applies to unmanaged Visual C+ code only. T SUMMARYThis article shows how to change the icon or bitmap of a CListCtrl item when it is selected. MORE INFORMATIONWhen you initial

36、ize the CListCtrl by calling CListCtrl:InsertItem(, you can pass in a value of I_IMAGECALLBACK for the index of the image. This means that the system expects you to fill in the image index when you get an LVN_GETDISPINFO notification. Inside of the handler for LVN_GETDISPINFO, you can check if the i

37、tem is selected and set the appropriate image index. Sample CodeBEGIN_MESSAGE_MAP(CTestView, CView/AFX_MSG_MAP(CTestViewON_WM_CREATE(/AFX_MSG_MAPON_NOTIFY (LVN_GETDISPINFO, IDI_LIST, OnGetDispInfoEND_MESSAGE_MAP(int CTestView:OnCreate(LPCREATESTRUCT lpCreateStructif (CView:OnCreate(lpCreateStruct =

38、-1return -1;/ m_pImage is a CTestViews member variable of type CImageList*/ create the CImageList with 16x15 imagesm_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;/ Th

39、is will automatically parse the bitmap into nine images.m_pImage-Add (&bm, RGB (192, 192, 192;/ m_pList is CTestViews 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, t

40、his, 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 szTemp10;for (int iCntr = 0; iCntr InsertItem (LVIF_IMAGE | LVIF_TEXT,iCntr, szTemp, 0, 0, I_IMAGECALLBACK, 0L;return 0;void CTestView:OnGetDispInfo (NMHDR* pnmhdr, LRESULT* pResultLV_DISPINFO* pdi = (LV_DISPINFO * pnmhdr;/ Fill in t

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论