




已阅读5页,还剩8页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
:/KB/list/editing_subitems_in_listcontrol.aspx另自己写了一篇关于一些ListControl的基本操作(如何获得一项的值,如何选中一行而不是一列等)请查看:/anglecloudy/blog/item/d969fd29138093f999250a97.htmlEditing 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 linevoid 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 information LVCOLUMN 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 List ListView_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; lvi.cchTextMax = 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 number nItem = temp-iItem; /get the column number nSubItem = temp-iSubItem; if(nSubItem = 0 | nSubItem = -1 | nItem = -1) return ; /Retrieve the text of the selected subItem /from the list CString str = GetItemText(hWnd1,nItem , nSubItem); RECT rect1,rect2; / this macro is used to retrieve the Rectanle / of the selected SubItem ListView_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 key void CMultipleColumnsDlg:OnOK() CWnd* pwndCtrl = GetFocus(); / get the control ID which is / presently having the focus int ctrl_ID = pwndCtrl-GetDlgCtrlID(); CString str; switch (ctrl_ID) /if the control is the EditBox case IDC_EDIT1: /get the text from the EditBox GetDlgItemText(IDC_EDIT1,str); /set the value in the listContorl with the /specified Item & SubItem values SetCell(: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 Wizard Implementation steps:1. Using MFC AppWizard, create a Dialog Based application. Give the application name as MultipleColumns. By default the wizard adds OK and Cancel buttons to the Dialog, Remove these two buttons. 2. Now Add a List-Control and in properties change the style to Report, this style is necessary if we want multiple columns 3. Add two buttons to the Dialog and name them as OK and Exit 4. Add one Edit box and in the properties remove the Border style 5. Using the Class Wizard add the message handlers for the OK and Exit Buttons. Add the following code to those functions Collapse Copy Codevoid CMultipleColumnsDlg:OK() CDialog:EndDialog (0); / Add this lineCollapse Copy Codevoid CMultipleColumnsDlg:OnExit() CDialog:EndDialog (0); / Add this line6. Add a function called InsertItems() to the CMulipleColumnsDlg class. Collapse Copy Codevoid InsertItems();In the function handler add the following code Collapse 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 information LVCOLUMN 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 CMultipleColumnsDlg class Collapse Copy Codevoid SetCell(HWND hWnd1, CString value, int nRow, int nCol);In the function handler add the following code Collapse 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 List ListView_InsertItem(hWnd1,&lvItem);8. Add one more function called GetItemText() to the same Class Collapse Copy CodeCString GetItemText(HWND hWnd, int nItem, int nSubItem) const;Inside the function add the following code Collapse 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; lvi.cchTextMax = 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 which are of type int Collapse Copy Codeint nItem, nSubItem;10. From the Class wizard add NM_CLICK notification to the List control. Inside the function handler write the following code Collapse 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 number nItem = temp-iItem; /get the column number nSubItem = temp-iSubItem; if(nSubItem = 0 | nSubItem = -1 | nItem = -1) return ; /Retrieve the text of the selected subItem /from the list CString str = GetItemText(hWnd1,nItem , nSubItem); RECT rect1,rect2; / this macro is used to retrieve the Rectanle / of the selected SubItem ListView_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 :Set
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 药理学全套题库及答案
- 2025年上海市浦东模范中学中考数学一模试卷(含答案)
- 抗灾减灾课件
- 2025届广东省佛山市禅城区高三下学期“供题训练”物理试题(含答案)
- 2024-2025学年河南省洛阳市伊滨区科普版(2012)六年级下册期中考试英语试卷(含答案)
- 2025年超二代微通道板合作协议书
- 历年五一建模题目及答案
- 怀化化学题目及答案
- 抗击疫情安全培训课件
- 2025年阻燃ABS热塑性弹性体合作协议书
- 低空飞行器操控考试题及答案
- 2025年秋季学期人教版PEP英语一年级上册教学计划
- 小学《班干部培训》主题班会课件
- 百师联盟2026届高三上学期开学摸底联考数学试题
- 登革热课件PDF教学课件
- 医疗机构睡眠门诊建设和管理专家共识(2025版)解读 3
- 2025年南阳唐河县国有企业公开招聘工作人员8名笔试备考题库及答案解析
- 中山市好小区好房子建设指引(试行)
- 2025年六年级数学培优辅潜工作计划及措施
- 2025年北京市高考语文真题之名著阅读《红楼梦》
- 2025秋人教版(2024)二年级上册数学教学计划
评论
0/150
提交评论