InPlaceEdit.cpp
上传用户:yatsl7111
上传日期:2007-01-08
资源大小:1433k
文件大小:3k
源码类别:

图形图象

开发平台:

Visual C++

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