listeditctrl.cpp
上传用户:meggie0806
上传日期:2007-01-02
资源大小:87k
文件大小:3k
源码类别:

ListView/ListBox

开发平台:

Visual C++

  1. // ListEditCtrl.cpp : implementation file
  2. //
  3. // The code contained in this file is based on the original
  4. // CInPlaceEdit from http://www.codeguru.com/listview/edit_subitems.shtml
  5. #include "stdafx.h"
  6. #include "SuperGrid.h"
  7. #include "ListEditCtrl.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CListEditCtrl
  15. BEGIN_MESSAGE_MAP(CListEditCtrl, CEdit)
  16. //{{AFX_MSG_MAP(CListEditCtrl)
  17. ON_WM_KILLFOCUS()
  18. ON_WM_NCDESTROY()
  19. ON_WM_CHAR()
  20. ON_WM_CREATE()
  21. //}}AFX_MSG_MAP
  22. END_MESSAGE_MAP()
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CListEditCtrl message handlers
  25. CListEditCtrl::CListEditCtrl(int iItem, int iSubItem, CString sInitText):m_strInitText(sInitText)
  26. {
  27. m_iItem = iItem;
  28. m_iSubItem = iSubItem;
  29. m_bVK_ESCAPE = 0;
  30. }
  31. int CListEditCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
  32. {
  33. if (CEdit::OnCreate(lpCreateStruct) == -1)
  34. return -1;
  35. CFont* font = GetParent()->GetFont();
  36. SetFont(font);
  37. SetWindowText(m_strInitText);
  38. SetFocus();
  39. SetSel(0, 0);
  40. return 0;
  41. }
  42. BOOL CListEditCtrl::PreTranslateMessage(MSG* pMsg)
  43. {
  44. if( pMsg->message == WM_KEYDOWN )
  45. {
  46. if(pMsg->wParam == VK_RETURN || pMsg->wParam == VK_DELETE || pMsg->wParam == VK_ESCAPE || GetKeyState( VK_CONTROL))
  47. {
  48. ::TranslateMessage(pMsg);
  49. ::DispatchMessage(pMsg);
  50. return 1;
  51. }
  52. }
  53. return CEdit::PreTranslateMessage(pMsg);
  54. }
  55. void CListEditCtrl::OnKillFocus(CWnd* pNewWnd)
  56. {
  57. CEdit::OnKillFocus(pNewWnd);
  58. CString str; GetWindowText(str);
  59. // Send Notification to parent of ListView ctrl
  60. LV_DISPINFO lvDispInfo;
  61. lvDispInfo.hdr.hwndFrom = GetParent()->m_hWnd;
  62. lvDispInfo.hdr.idFrom = GetDlgCtrlID();
  63. lvDispInfo.hdr.code = LVN_ENDLABELEDIT;
  64. lvDispInfo.item.mask = LVIF_TEXT;
  65. lvDispInfo.item.iItem = m_iItem;
  66. lvDispInfo.item.iSubItem = m_iSubItem;
  67. lvDispInfo.item.pszText = m_bVK_ESCAPE ? NULL : LPTSTR((LPCTSTR)str);
  68. lvDispInfo.item.cchTextMax = str.GetLength();
  69. GetParent()->GetParent()->SendMessage( WM_NOTIFY, GetParent()->GetDlgCtrlID(),(LPARAM)&lvDispInfo);
  70. DestroyWindow();
  71. }
  72. void CListEditCtrl::OnNcDestroy()
  73. {
  74. CEdit::OnNcDestroy();
  75. delete this;
  76. }
  77. void CListEditCtrl::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
  78. {
  79. if( nChar == VK_ESCAPE || nChar == VK_RETURN)
  80. {
  81. if( nChar == VK_ESCAPE)
  82. m_bVK_ESCAPE = 1;
  83. GetParent()->SetFocus();
  84. return;
  85. }
  86. CEdit::OnChar(nChar, nRepCnt, nFlags);
  87. // Resize edit control if needed
  88. // Get text extent
  89. CString str;
  90. GetWindowText( str );
  91. CWindowDC dc(this);
  92. CFont *pFont = GetParent()->GetFont();
  93. CFont *pFontDC = dc.SelectObject(pFont);
  94. CSize size = dc.GetTextExtent(str);
  95. dc.SelectObject(pFontDC);
  96. size.cx += 5; // add some extra buffer
  97. // Get client rect
  98. CRect rect, rcParent;
  99. GetClientRect(&rect);
  100. GetParent()->GetClientRect(&rcParent);
  101. // Transform rect to parent coordinates
  102. ClientToScreen(&rect);
  103. GetParent()->ScreenToClient(&rect);
  104. // Check whether control needs to be resized
  105. // and whether there is space to grow
  106. if(size.cx > rect.Width())
  107. {
  108. if(size.cx + rect.left < rcParent.right)
  109. rect.right = rect.left + size.cx;
  110. else
  111. rect.right = rcParent.right;
  112. MoveWindow(&rect);
  113. }
  114. }