PropertyGridInplaceEdit.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include "stdafx.h"
  2. #include "PropertyGridInplaceEdit.h"
  3. #include "PropertyGridInplaceButton.h"
  4. #include "PropertyGridInplaceList.h"
  5. #include "PropertyGridItem.h"
  6. #include "PropertyGrid.h"
  7. /////////////////////////////////////////////////////////////////////////////
  8. // CPropertyGridInplaceEdit
  9. CPropertyGridInplaceEdit::CPropertyGridInplaceEdit()
  10. : m_pItem(0)
  11. , m_pGrid(0)
  12. , m_nDefaultMaxLength(0)
  13. {
  14. }
  15. CPropertyGridInplaceEdit::~CPropertyGridInplaceEdit()
  16. {
  17. }
  18. IMPLEMENT_DYNAMIC(CPropertyGridInplaceEdit, CEdit)
  19. BEGIN_MESSAGE_MAP(CPropertyGridInplaceEdit, CEdit)
  20. //{{AFX_MSG_MAP(CPropertyGridInplaceEdit)
  21. ON_WM_CTLCOLOR_REFLECT()
  22. ON_CONTROL_REFLECT(EN_KILLFOCUS, OnEnKillfocus)
  23. ON_CONTROL_REFLECT(EN_SETFOCUS, OnEnSetfocus)
  24. ON_WM_LBUTTONDBLCLK()
  25. //}}AFX_MSG_MAP
  26. END_MESSAGE_MAP()
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CPropertyGridInplaceEdit message handlers
  29. void CPropertyGridInplaceEdit::SetValue(CString strValue)
  30. {
  31. m_strValue = strValue;
  32. }
  33. void CPropertyGridInplaceEdit::HideWindow()
  34. {
  35. if (m_hWnd)
  36. {
  37. ShowWindow(SW_HIDE);
  38. }
  39. }
  40. void CPropertyGridInplaceEdit::Create(CPropertyGridItem* pItem, CRect rect, UINT nMaxLength)
  41. {
  42. ASSERT(pItem && pItem->GetGrid());
  43. m_pGrid = pItem->GetGrid();
  44. if (!m_hWnd)
  45. {
  46. CEdit::Create(WS_CHILD|ES_AUTOHSCROLL, rect, m_pGrid, 0);
  47. m_nDefaultMaxLength = GetLimitText();
  48. SetFont(m_pGrid->GetFont());
  49. }
  50. SetWindowText(m_strValue);
  51. SetWindowPos(0, rect.left, rect.top, rect.Width(), rect.Height(), SWP_NOZORDER|SWP_SHOWWINDOW);
  52. SetMargins(3, 0);
  53. m_pItem = pItem;
  54. // 设置编辑框的最大字符数
  55. if (nMaxLength == 0)
  56. SetLimitText(m_nDefaultMaxLength); // 无限制
  57. else
  58. SetLimitText(nMaxLength); // 有限制
  59. }
  60. HBRUSH CPropertyGridInplaceEdit::CtlColor(CDC* pDC, UINT /*nCtlColor*/)
  61. {
  62. COLORREF clr = ((CPropertyGridView*)m_pGrid)->m_clrBack;
  63. //pDC->SetBkMode(TRANSPARENT);
  64. pDC->SetTextColor(GetStyle() & ES_READONLY? GetSysColor(COLOR_GRAYTEXT): ((CPropertyGridView*)m_pGrid)->m_clrFore);
  65. pDC->SetBkColor(clr);
  66. m_brBack.DeleteObject();
  67. m_brBack.CreateSolidBrush(clr);
  68. return m_brBack;
  69. }
  70. void CPropertyGridInplaceEdit::OnEnSetfocus()
  71. {
  72. m_pGrid->Invalidate(FALSE);
  73. }
  74. void CPropertyGridInplaceEdit::OnEnKillfocus()
  75. {
  76. if (m_pItem)
  77. {
  78. m_pItem->OnValidateEdit();
  79. m_pGrid->Invalidate(FALSE);
  80. }
  81. }
  82. BOOL CPropertyGridInplaceEdit::PreTranslateMessage(MSG* pMsg)
  83. {
  84. if ((GetFocus() == this) && (pMsg->message == WM_KEYDOWN))
  85. {
  86. if (pMsg->wParam == VK_ESCAPE)
  87. {
  88. SetWindowText(m_strValue);
  89. m_pGrid->SetFocus();
  90. return TRUE;
  91. }
  92. else if (pMsg->wParam == VK_RETURN)
  93. {
  94. m_pGrid->SetFocus();
  95. return TRUE;
  96. }
  97. else if (pMsg->wParam == VK_DOWN || pMsg->wParam == VK_UP)
  98. {
  99. if (SelectConstraint(pMsg->wParam == VK_DOWN? +1: -1, FALSE))
  100. {
  101. SetSel(0, -1);
  102. return TRUE;
  103. }
  104. }
  105. }
  106. return CEdit::PreTranslateMessage(pMsg);
  107. }
  108. BOOL CPropertyGridInplaceEdit::SelectConstraint(int nDirection, BOOL bCircle)
  109. {
  110. CPGItemConstraints* pList = m_pItem->GetConstraints();
  111. if (pList->IsEmpty())
  112. return FALSE;
  113. CString str;
  114. GetWindowText(str);
  115. int nIndex = pList->FindConstraint( str );
  116. nIndex+= nDirection;
  117. if (nIndex >= pList->GetCount()) nIndex = bCircle ? 0 : (ULONG)pList->GetCount() - 1;
  118. if (nIndex < 0) nIndex = bCircle ? (ULONG)pList->GetCount() - 1 : 0;
  119. POSITION pos = pList->FindIndex(nIndex);
  120. SetWindowText( pList->GetAt(pos) );
  121. return TRUE;
  122. }
  123. void CPropertyGridInplaceEdit::OnLButtonDblClk(UINT nFlags, CPoint point)
  124. {
  125. // 修改后,增加了对只读属性的判断
  126. // if (SelectConstraint(+1, TRUE))
  127. if (m_pItem != NULL && !m_pItem->GetReadOnly() && SelectConstraint(+1, TRUE))
  128. {
  129. m_pGrid->SetFocus();
  130. return;
  131. }
  132. CEdit::OnLButtonDblClk(nFlags, point);
  133. }
  134. void CPropertyGridInplaceEdit::DestroyItem()
  135. {
  136. // reset variables to defaults.
  137. m_pItem = NULL;
  138. m_pGrid = NULL;
  139. m_strValue.Empty();
  140. m_brBack.DeleteObject();
  141. // destroy the window.
  142. DestroyWindow( );
  143. }