XEdit.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // XEdit.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "XEdit.h"
  5. #ifdef _DEBUG
  6. #define new DEBUG_NEW
  7. #undef THIS_FILE
  8. static char THIS_FILE[] = __FILE__;
  9. #endif
  10. UINT WM_XEDIT_KILL_FOCUS = ::RegisterWindowMessage(_T("WM_XEDIT_KILL_FOCUS"));
  11. UINT WM_XEDIT_VK_ESCAPE = ::RegisterWindowMessage(_T("WM_XEDIT_VK_ESCAPE"));
  12. ///////////////////////////////////////////////////////////////////////////////
  13. // message map
  14. BEGIN_MESSAGE_MAP(CXEdit, CEdit)
  15. //{{AFX_MSG_MAP(CXEdit)
  16. ON_WM_CHAR()
  17. ON_WM_CREATE()
  18. ON_WM_DESTROY()
  19. ON_WM_KILLFOCUS()
  20. ON_WM_PAINT()
  21. //}}AFX_MSG_MAP
  22. END_MESSAGE_MAP()
  23. ///////////////////////////////////////////////////////////////////////////////
  24. // ctor
  25. CXEdit::CXEdit(CWnd *pParent, LPCTSTR lpszText) :
  26. m_pParent(pParent),
  27. m_strText(lpszText),
  28. m_bMessageSent(FALSE)
  29. {
  30. XLISTCTRL_TRACE(_T("in CXEdit::CXEdit\n"));
  31. }
  32. ///////////////////////////////////////////////////////////////////////////////
  33. // dtor
  34. CXEdit::~CXEdit()
  35. {
  36. XLISTCTRL_TRACE(_T("in CXEdit::~CXEdit\n"));
  37. }
  38. ///////////////////////////////////////////////////////////////////////////////
  39. // SendRegisteredMessage
  40. void CXEdit::SendRegisteredMessage(UINT nMsg, WPARAM wParam /*= 0*/, LPARAM lParam /*= 0*/)
  41. {
  42. BOOL bMessageSent = m_bMessageSent;
  43. m_bMessageSent = TRUE;
  44. if (m_pParent && ::IsWindow(m_pParent->m_hWnd) && !bMessageSent)
  45. m_pParent->SendMessage(nMsg, wParam, lParam);
  46. }
  47. ///////////////////////////////////////////////////////////////////////////////
  48. // OnCreate
  49. int CXEdit::OnCreate(LPCREATESTRUCT lpCreateStruct)
  50. {
  51. XLISTCTRL_TRACE(_T("in CXEdit::OnCreate\n"));
  52. if (CEdit::OnCreate(lpCreateStruct) == -1)
  53. return -1;
  54. // set the proper font
  55. if (!m_pParent)
  56. {
  57. m_pParent = GetParent();
  58. }
  59. if (m_pParent && ::IsWindow(m_pParent->m_hWnd))
  60. {
  61. CFont * pFont = m_pParent->GetFont();
  62. if (pFont)
  63. SetFont(pFont);
  64. }
  65. else
  66. {
  67. m_pParent = NULL;
  68. }
  69. SetWindowText(m_strText);
  70. SetSel(0, -1);
  71. SetFocus();
  72. SetCapture();
  73. return 0;
  74. }
  75. ///////////////////////////////////////////////////////////////////////////////
  76. // OnPaint
  77. void CXEdit::OnPaint()
  78. {
  79. CRect rect;
  80. GetClientRect(&rect);
  81. rect.right += 2;
  82. CDC * pDC = GetDC();
  83. pDC->FillSolidRect(&rect, ::GetSysColor(COLOR_WINDOW));
  84. ReleaseDC(pDC);
  85. CEdit::OnPaint(); // let CEdit draw the text
  86. //rect.right -= 2;
  87. rect.left -= 1;
  88. rect.top -= 1;
  89. rect.bottom -=1;
  90. pDC = GetDC();
  91. // don't erase the text that CEdit has just drawn
  92. CBrush * pOldBrush = (CBrush *) pDC->SelectStockObject(NULL_BRUSH);
  93. //CPen pen(PS_SOLID, 1, ::GetSysColor(COLOR_BTNSHADOW));
  94. CPen pen(PS_SOLID, 1, ::GetSysColor(COLOR_INACTIVECAPTION)); // same as combobox
  95. CPen *pOldPen = pDC->SelectObject(&pen);
  96. pDC->Rectangle(&rect);
  97. pDC->SelectObject(pOldPen);
  98. pDC->SelectObject(pOldBrush);
  99. ReleaseDC(pDC);
  100. }
  101. ///////////////////////////////////////////////////////////////////////////////
  102. // PreTranslateMessage
  103. BOOL CXEdit::PreTranslateMessage(MSG* pMsg)
  104. {
  105. // handle WM_KEYDOWN in case the edit has focus - otherwise
  106. // it will be sent to parent, bypassing WM_CHAR
  107. if (pMsg->message == WM_KEYDOWN)
  108. {
  109. if (pMsg->wParam == VK_RETURN)
  110. {
  111. SendRegisteredMessage(WM_XEDIT_KILL_FOCUS);
  112. return TRUE;
  113. }
  114. else if (pMsg->wParam == VK_ESCAPE)
  115. {
  116. SendRegisteredMessage(WM_XEDIT_VK_ESCAPE);
  117. return TRUE;
  118. }
  119. }
  120. return CEdit::PreTranslateMessage(pMsg);
  121. }
  122. ///////////////////////////////////////////////////////////////////////////////
  123. // OnChar
  124. void CXEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
  125. {
  126. // handle escape and return, in case edit does NOT have focus
  127. if (m_pParent)
  128. {
  129. if (nChar == VK_ESCAPE)
  130. {
  131. SendRegisteredMessage(WM_XEDIT_VK_ESCAPE);
  132. return;
  133. }
  134. else if (nChar == VK_RETURN)
  135. {
  136. SendRegisteredMessage(WM_XEDIT_KILL_FOCUS);
  137. return;
  138. }
  139. }
  140. CEdit::OnChar(nChar, nRepCnt, nFlags);
  141. }
  142. ///////////////////////////////////////////////////////////////////////////////
  143. // OnKillFocus
  144. void CXEdit::OnKillFocus(CWnd* pNewWnd)
  145. {
  146. CEdit::OnKillFocus(pNewWnd);
  147. SendRegisteredMessage(WM_XEDIT_KILL_FOCUS);
  148. }
  149. ///////////////////////////////////////////////////////////////////////////////
  150. // OnDestroy
  151. void CXEdit::OnDestroy()
  152. {
  153. XLISTCTRL_TRACE(_T("in CXEdit::OnDestroy\n"));
  154. ReleaseCapture();
  155. CEdit::OnDestroy();
  156. }