当前位置:   article > 正文

MFC编程->可编辑List Control控件_mfc listcontrol 可编辑

mfc listcontrol 可编辑

        List Control使用时,要使得单元格内内容可编辑,list控件代码需要重写。


  1. #if !defined(AFX_EDITLISTCTRL_H__A02CE0B0_FF7B_4A54_8570_12B03905EC3E__INCLUDED_)
  2. #define AFX_EDITLISTCTRL_H__A02CE0B0_FF7B_4A54_8570_12B03905EC3E__INCLUDED_
  3. #if _MSC_VER > 1000
  4. #pragma once
  5. #endif // _MSC_VER > 1000
  6. // EditListCtrl.h : header file
  7. // Download by http://www.codefans.net
  8. /
  9. //CItemEdit window
  10. class CItemEdit : public CEdit
  11. {
  12. // Construction
  13. public:
  14. CItemEdit();
  15. // Attributes
  16. public:
  17. // Operations
  18. public:
  19. // Overrides
  20. // ClassWizard generated virtual function overrides
  21. //{{AFX_VIRTUAL(CItemEdit)
  22. //}}AFX_VIRTUAL
  23. // Implementation
  24. public:
  25. int m_iXPos;
  26. virtual ~CItemEdit();
  27. // Generated message map functions
  28. protected:
  29. //{{AFX_MSG(CItemEdit)
  30. afx_msg void OnWindowPosChanging(WINDOWPOS FAR* lpwndpos);
  31. //}}AFX_MSG
  32. DECLARE_MESSAGE_MAP()
  33. };
  34. /
  35. /
  36. // CEditListCtrl window
  37. // Download by http://www.codefans.net
  38. class CEditListCtrl : public CListCtrl
  39. {
  40. // Construction
  41. public:
  42. CEditListCtrl();
  43. // Attributes
  44. public:
  45. // Operations
  46. public:
  47. // Overrides
  48. // ClassWizard generated virtual function overrides
  49. //{{AFX_VIRTUAL(CEditListCtrl)
  50. protected:
  51. virtual void PreSubclassWindow();
  52. //}}AFX_VIRTUAL
  53. // Implementation
  54. public:
  55. virtual ~CEditListCtrl();
  56. // Generated message map functions
  57. protected:
  58. //{{AFX_MSG(CEditListCtrl)
  59. afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
  60. afx_msg void OnPaint();
  61. afx_msg void OnBeginlabeledit(NMHDR* pNMHDR, LRESULT* pResult);
  62. afx_msg void OnEndlabeledit(NMHDR* pNMHDR, LRESULT* pResult);
  63. afx_msg void OnKillFocus(CWnd* pNewWnd);
  64. afx_msg void OnSetFocus(CWnd* pOldWnd);
  65. //}}AFX_MSG
  66. afx_msg void OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult);
  67. DECLARE_MESSAGE_MAP()
  68. private:
  69. int m_iSubItem; //子项标识符
  70. int m_iItem; //主项标识符
  71. BOOL m_bHighLight; //是否高亮文本
  72. BOOL m_bFocus; //是否绘制焦点框
  73. CItemEdit m_edtItemEdit; // 用于子类化EditLabel函数返回的CEdit*指针
  74. };
  75. //{{AFX_INSERT_LOCATION}}
  76. // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
  77. #endif // !defined(AFX_EDITLISTCTRL_H__A02CE0B0_FF7B_4A54_8570_12B03905EC3E__INCLUDED_)


  1. // EditListCtrl.cpp : implementation file
  2. // Download by http://www.codefans.net
  3. #include "stdafx.h"
  4. #include "EditListCtrl.h"
  5. #ifdef _DEBUG
  6. #define new DEBUG_NEW
  7. #undef THIS_FILE
  8. static char THIS_FILE[] = __FILE__;
  9. #endif
  10. /
  11. // CEditListCtrl
  12. CEditListCtrl::CEditListCtrl()
  13. {
  14. m_iItem = -1;
  15. m_iSubItem = -1;
  16. m_bHighLight = FALSE;
  17. m_bFocus = FALSE;
  18. }
  19. CEditListCtrl::~CEditListCtrl()
  20. {
  21. }
  22. BEGIN_MESSAGE_MAP(CEditListCtrl, CListCtrl)
  23. //{{AFX_MSG_MAP(CEditListCtrl)
  24. ON_WM_LBUTTONDOWN()
  25. ON_WM_PAINT()
  26. ON_NOTIFY_REFLECT(LVN_BEGINLABELEDIT, OnBeginlabeledit)
  27. ON_NOTIFY_REFLECT(LVN_ENDLABELEDIT, OnEndlabeledit)
  28. ON_WM_KILLFOCUS()
  29. ON_WM_SETFOCUS()
  30. //}}AFX_MSG_MAP
  31. ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)
  32. END_MESSAGE_MAP()
  33. /
  34. // CEditListCtrl message handlers
  35. void CEditListCtrl::OnLButtonDown(UINT nFlags, CPoint point)
  36. {
  37. // TODO: Add your message handler code here and/or call default
  38. m_bFocus = TRUE;
  39. LVHITTESTINFO lvhit;
  40. lvhit.pt = point;
  41. int item = SubItemHitTest(&lvhit);
  42. //if (over a item/subitem)
  43. if (item != -1 && (lvhit.flags & LVHT_ONITEM))
  44. {
  45. CListCtrl::OnLButtonDown(nFlags, point);
  46. if(m_bHighLight && m_iItem == lvhit.iItem && m_iSubItem == lvhit.iSubItem)
  47. {
  48. //第二次单击
  49. EditLabel(m_iItem);
  50. return;
  51. }
  52. else
  53. {
  54. //第一次单击
  55. m_iItem = lvhit.iItem;
  56. m_iSubItem = lvhit.iSubItem;
  57. m_bHighLight = TRUE;
  58. }
  59. }
  60. else
  61. {
  62. if(m_edtItemEdit.m_hWnd == NULL)
  63. {
  64. //未出现文本编辑框时
  65. m_bHighLight = FALSE;
  66. }
  67. CListCtrl::OnLButtonDown(nFlags, point);
  68. }
  69. Invalidate();
  70. }
  71. //EditLabel() cause this function has been called
  72. void CEditListCtrl::OnBeginlabeledit(NMHDR* pNMHDR, LRESULT* pResult)
  73. {
  74. LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;
  75. // TODO: Add your control notification handler code here
  76. if (m_iSubItem >= 0)
  77. {
  78. ASSERT(m_iItem == pDispInfo->item.iItem);
  79. CRect rcSubItem;
  80. GetSubItemRect( pDispInfo->item.iItem, m_iSubItem, LVIR_BOUNDS, rcSubItem);
  81. //get edit control and subclass
  82. HWND hWnd= (HWND)SendMessage(LVM_GETEDITCONTROL);
  83. ASSERT(hWnd != NULL);
  84. VERIFY(m_edtItemEdit.SubclassWindow(hWnd));
  85. //move edit control text 4 pixel to the right of org label,
  86. //as Windows does it...
  87. m_edtItemEdit.m_iXPos = rcSubItem.left + 4;
  88. m_edtItemEdit.SetWindowText(GetItemText(pDispInfo->item.iItem, m_iSubItem));
  89. }
  90. *pResult = 0;
  91. }
  92. void CEditListCtrl::OnPaint()
  93. {
  94. //CPaintDC dc(this); // device context for painting
  95. if (m_iSubItem >= 0 && m_edtItemEdit.m_hWnd)
  96. {
  97. CRect rect;
  98. CRect rcEdit;
  99. m_edtItemEdit.GetWindowRect(rcEdit);
  100. ScreenToClient(rcEdit);
  101. GetSubItemRect(m_iItem, m_iSubItem, LVIR_LABEL, rect);
  102. //当文本编辑框缩小时,擦除露出的项文本高亮部分
  103. if (rcEdit.right < rect.right)
  104. {
  105. rect.left = rcEdit.right;
  106. CClientDC dc(this);
  107. dc.FillRect(rect, &CBrush(::GetSysColor(COLOR_WINDOW)));
  108. ValidateRect(rect);
  109. }
  110. }
  111. CListCtrl::OnPaint();
  112. }
  113. void CEditListCtrl::OnEndlabeledit(NMHDR* pNMHDR, LRESULT* pResult)
  114. {
  115. LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;
  116. LV_ITEM *plvItem = &pDispInfo->item;
  117. if (m_iSubItem >= 0)
  118. {
  119. if (plvItem->pszText != NULL )
  120. {
  121. SetItemText(plvItem->iItem,m_iSubItem, plvItem->pszText);
  122. }
  123. VERIFY(m_edtItemEdit.UnsubclassWindow()!=NULL);
  124. *pResult = 0;
  125. }
  126. //编辑文本时对控件父窗口操作(如单击其它控件)引发"OnEndlabeledit"时刷新控件
  127. CRect rect;
  128. GetWindowRect(&rect);
  129. CPoint point;
  130. ::GetCursorPos(&point);
  131. if(!rect.PtInRect(point))
  132. {
  133. m_iItem = -1;
  134. m_iSubItem = -1;
  135. m_bFocus = FALSE;
  136. m_bHighLight = FALSE;
  137. }
  138. }
  139. void CEditListCtrl::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
  140. {
  141. //draw each item.set txt color,bkcolor....
  142. NMLVCUSTOMDRAW* pNMLVCustomDraw = (NMLVCUSTOMDRAW*)pNMHDR;
  143. // Take the default processing unless we set this to something else below.
  144. *pResult = CDRF_DODEFAULT;
  145. // First thing - check the draw stage. If it's the control's prepaint
  146. // stage, then tell Windows we want messages for every item.
  147. if (pNMLVCustomDraw->nmcd.dwDrawStage == CDDS_PREPAINT)
  148. {
  149. *pResult = CDRF_NOTIFYITEMDRAW;
  150. }
  151. else if (pNMLVCustomDraw->nmcd.dwDrawStage == CDDS_ITEMPREPAINT)
  152. {
  153. // This is the notification message for an item. We'll request
  154. // notifications before each subitem's prepaint stage.
  155. *pResult = CDRF_NOTIFYSUBITEMDRAW;
  156. }
  157. else if (pNMLVCustomDraw->nmcd.dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM))
  158. {
  159. // store the colors back in the NMLVCUSTOMDRAW struct
  160. // but it's effective only when *pResult = CDRF_DODEFAULT
  161. // pNMLVCustomDraw->clrText = RGB(0, 0, 255);
  162. // pNMLVCustomDraw->clrTextBk = RGB(0, 255, 0);
  163. // *pResult = CDRF_DODEFAULT;
  164. // This is the prepaint stage for a subitem. Here's where we set the
  165. // item's text and background colors. Our return value will tell
  166. // Windows to draw the subitem itself, but it will use the new colors
  167. // we set here.
  168. int iItem = (int)pNMLVCustomDraw->nmcd.dwItemSpec;
  169. int iSubItem = pNMLVCustomDraw->iSubItem;
  170. CDC* pDC = CDC::FromHandle(pNMLVCustomDraw->nmcd.hdc);
  171. CString strItemText = GetItemText(iItem, iSubItem);
  172. CRect rcItem, rcText;
  173. GetSubItemRect(iItem, iSubItem, LVIR_LABEL, rcItem);
  174. rcText = rcItem;
  175. CSize size = pDC->GetTextExtent(strItemText);
  176. if(strItemText == _T(""))
  177. {
  178. size.cx = 41;
  179. }
  180. //设置文本高亮矩形
  181. rcText.left += 4;
  182. rcText.right = rcText.left + size.cx + 6;
  183. if(rcText.right > rcItem.right)
  184. {
  185. rcText.right = rcItem.right;
  186. }
  187. COLORREF crOldTextColor = pDC->GetTextColor();
  188. //绘制项焦点/高亮效果
  189. if(m_bFocus)
  190. {
  191. if((m_iItem == iItem) && (m_iSubItem == iSubItem))
  192. {
  193. if(m_bHighLight)
  194. {
  195. pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
  196. pDC->FillSolidRect(&rcText, ::GetSysColor(COLOR_HIGHLIGHT));
  197. }
  198. pDC->DrawFocusRect(&rcText);
  199. }
  200. }
  201. //绘制项文本
  202. rcItem.left += 6;
  203. pDC->DrawText(strItemText, &rcItem, DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS | DT_NOCLIP);
  204. pDC->SetTextColor(crOldTextColor);
  205. *pResult = CDRF_SKIPDEFAULT;// We've painted everything.
  206. }
  207. }
  208. void CEditListCtrl::PreSubclassWindow()
  209. {
  210. // TODO: Add your specialized code here and/or call the base class
  211. CListCtrl::PreSubclassWindow();
  212. ModifyStyle(0, LVS_EDITLABELS);
  213. }
  214. void CEditListCtrl::OnKillFocus(CWnd* pNewWnd)
  215. {
  216. CListCtrl::OnKillFocus(pNewWnd);
  217. // TODO: Add your message handler code here
  218. CRect rect;
  219. GetWindowRect(&rect);
  220. CPoint point;
  221. ::GetCursorPos(&point);
  222. if(!rect.PtInRect(point) && GetParent()->GetFocus() != NULL)
  223. {
  224. m_iItem = -1;
  225. m_iSubItem = -1;
  226. m_bFocus = FALSE;
  227. m_bHighLight = FALSE;
  228. Invalidate();
  229. }
  230. }
  231. void CEditListCtrl::OnSetFocus(CWnd* pOldWnd)
  232. {
  233. // CListCtrl::OnSetFocus(pOldWnd);
  234. // TODO: Add your message handler code here
  235. }
  236. /
  237. // CItemEdit
  238. CItemEdit::CItemEdit()
  239. {
  240. }
  241. CItemEdit::~CItemEdit()
  242. {
  243. }
  244. BEGIN_MESSAGE_MAP(CItemEdit, CEdit)
  245. //{{AFX_MSG_MAP(CItemEdit)
  246. ON_WM_WINDOWPOSCHANGING()
  247. //}}AFX_MSG_MAP
  248. END_MESSAGE_MAP()
  249. /
  250. // CItemEdit message handlers
  251. void CItemEdit::OnWindowPosChanging(WINDOWPOS FAR* lpwndpos)
  252. {
  253. lpwndpos->x = m_iXPos;
  254. CEdit::OnWindowPosChanging(lpwndpos);
  255. // TODO: Add your message handler code here
  256. }


在对话框dlg.h文件里添加以下代码:

        CEditListCtrl m_EditListCtrl;

在对话框dlg.cpp文件里

//m_EditListCtrl初始化
m_EditListCtrl.InsertColumn(0, _T("参数名"), LVCFMT_LEFT, 110);//单元格宽度
m_EditListCtrl.InsertColumn(1, _T("参数"), LVCFMT_LEFT, 110);
m_EditListCtrl.InsertColumn(2, _T("参数名"), LVCFMT_LEFT, 110);
m_EditListCtrl.InsertColumn(4, _T("参数"), LVCFMT_LEFT, 110);

m_EditListCtrl.InsertItem(0, _T("min_win"));
m_EditListCtrl.SetItemText(0, 2, _T("patch_size"));
m_EditListCtrl.SetItemText(0, 1, _T("15"));
m_EditListCtrl.SetItemText(0, 3, _T("15"));

        m_EditListCtrl.InsertItem(1, _T("ncc_thesame"));
m_EditListCtrl.SetItemText(1, 2, _T("valid"));
m_EditListCtrl.SetItemText(1, 1, _T("0.95"));
m_EditListCtrl.SetItemText(1, 3, _T("0.5"));





     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);



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/301842
推荐阅读
相关标签
  

闽ICP备14008679号