InPlaceCombo.cpp
上传用户:flyanjing
上传日期:2015-10-09
资源大小:649k
文件大小:4k
开发平台:

Visual C++

  1. /*******************************************************************************
  2. Author : Aravindan Premkumar
  3. Unregistered Copyright 2003 : Aravindan Premkumar
  4. All Rights Reserved
  5. This piece of code does not have any registered copyright and is free to be 
  6. used as necessary. The user is free to modify as per the requirements. As a
  7. fellow developer, all that I expect and request for is to be given the 
  8. credit for intially developing this reusable code by not removing my name as 
  9. the author.
  10. *******************************************************************************/
  11. #include "stdafx.h"
  12. #include "InPlaceCombo.h"
  13. #ifdef _DEBUG
  14. #define new DEBUG_NEW
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18. /////////////////////////////////////////////////////////////////////////////
  19. // CInPlaceCombo
  20. CInPlaceCombo* CInPlaceCombo::m_pInPlaceCombo = NULL; 
  21. CInPlaceCombo::CInPlaceCombo()
  22. {
  23. m_iRowIndex = -1;
  24. m_iColumnIndex = -1;
  25. m_bESC = FALSE;
  26. }
  27. CInPlaceCombo::~CInPlaceCombo()
  28. {
  29. }
  30. BEGIN_MESSAGE_MAP(CInPlaceCombo, CComboBox)
  31. //{{AFX_MSG_MAP(CInPlaceCombo)
  32. ON_WM_CREATE()
  33. ON_WM_KILLFOCUS()
  34. ON_WM_CHAR()
  35. ON_CONTROL_REFLECT(CBN_CLOSEUP, OnCloseup)
  36. //}}AFX_MSG_MAP
  37. END_MESSAGE_MAP()
  38. /////////////////////////////////////////////////////////////////////////////
  39. // CInPlaceCombo message handlers
  40. int CInPlaceCombo::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  41. {
  42. if (CComboBox::OnCreate(lpCreateStruct) == -1)
  43. {
  44. return -1;
  45. }
  46. // Set the proper font
  47. CFont* pFont = GetParent()->GetFont();
  48. SetFont(pFont);
  49. SetFocus();
  50. ResetContent(); 
  51. for (POSITION Pos_ = m_DropDownList.GetHeadPosition(); Pos_ != NULL;)
  52. {
  53. AddString((LPCTSTR) (m_DropDownList.GetNext(Pos_)));
  54. }
  55. return 0;
  56. }
  57. BOOL CInPlaceCombo::PreTranslateMessage(MSG* pMsg) 
  58. {
  59. // If the message if for "Enter" or "Esc"
  60. // Do not process
  61. if (pMsg->message == WM_KEYDOWN)
  62. {
  63. if(pMsg->wParam == VK_RETURN || pMsg->wParam == VK_ESCAPE)
  64. {
  65. ::TranslateMessage(pMsg);
  66. ::DispatchMessage(pMsg);
  67. // DO NOT process further
  68. return TRUE;
  69. }
  70. }
  71. return CComboBox::PreTranslateMessage(pMsg);
  72. }
  73. void CInPlaceCombo::OnKillFocus(CWnd* pNewWnd) 
  74. {
  75. CComboBox::OnKillFocus(pNewWnd);
  76. // Get the current selection text
  77. CString str;
  78. GetWindowText(str);
  79. // Send Notification to parent of ListView ctrl
  80. LV_DISPINFO dispinfo;
  81. dispinfo.hdr.hwndFrom = GetParent()->m_hWnd;
  82. dispinfo.hdr.idFrom = GetDlgCtrlID();
  83. dispinfo.hdr.code = LVN_ENDLABELEDIT;
  84. dispinfo.item.mask = LVIF_TEXT;
  85. dispinfo.item.iItem = m_iRowIndex;
  86. dispinfo.item.iSubItem = m_iColumnIndex;
  87. dispinfo.item.pszText = m_bESC ? LPTSTR((LPCTSTR)m_strWindowText) : LPTSTR((LPCTSTR)str);
  88. dispinfo.item.cchTextMax = m_bESC ? m_strWindowText.GetLength() : str.GetLength();
  89. GetParent()->SendMessage(WM_NOTIFY, GetParent()->GetDlgCtrlID(), (LPARAM)&dispinfo);
  90. // Close the control
  91. PostMessage(WM_CLOSE);
  92. }
  93. void CInPlaceCombo::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
  94. {
  95. // If the key is "Esc" set focus back to the list control
  96. if (nChar == VK_ESCAPE || nChar == VK_RETURN)
  97. {
  98. if (nChar == VK_ESCAPE)
  99. {
  100. m_bESC = TRUE;
  101. }
  102. GetParent()->SetFocus();
  103. return;
  104. }
  105. CComboBox::OnChar(nChar, nRepCnt, nFlags);
  106. }
  107. void CInPlaceCombo::OnCloseup() 
  108. {
  109. // Set the focus to the parent list control
  110. GetParent()->SetFocus();
  111. }
  112. CInPlaceCombo* CInPlaceCombo::GetInstance()
  113. {
  114. if(m_pInPlaceCombo == NULL)
  115. {
  116. m_pInPlaceCombo = new CInPlaceCombo;
  117. }
  118. return m_pInPlaceCombo;
  119. }
  120. void CInPlaceCombo::DeleteInstance()
  121. {
  122. delete m_pInPlaceCombo;
  123. m_pInPlaceCombo = NULL;
  124. }
  125. BOOL CInPlaceCombo::ShowComboCtrl(DWORD dwStyle, const CRect &rCellRect, CWnd* pParentWnd, UINT uiResourceID,
  126.   int iRowIndex, int iColumnIndex, CStringList* pDropDownList, 
  127.   CString strCurSelecetion /*= ""*/, int iCurSel /*= -1*/)
  128. {
  129. m_iRowIndex = iRowIndex;
  130. m_iColumnIndex = iColumnIndex;
  131. m_bESC = FALSE;
  132. m_DropDownList.RemoveAll(); 
  133. m_DropDownList.AddTail(pDropDownList);
  134. BOOL bRetVal = TRUE;
  135. if (-1 != iCurSel)
  136. {
  137. GetLBText(iCurSel, m_strWindowText);
  138. }
  139. else if (!strCurSelecetion.IsEmpty()) 
  140. {
  141. m_strWindowText = strCurSelecetion;
  142. }
  143. if (NULL == m_pInPlaceCombo->m_hWnd) 
  144. {
  145. bRetVal = m_pInPlaceCombo->Create(dwStyle, rCellRect, pParentWnd, uiResourceID); 
  146. }
  147. SetCurSel(iCurSel);
  148. return bRetVal;
  149. }