// PropTreeItemEdit.cpp : implementation file // // Copyright (C) 1998-2001 Scott Ramsay // sramsay@gonavi.com // http://www.gonavi.com // // This material is provided "as is", with absolutely no warranty expressed // or implied. Any use is at your own risk. // // Permission to use or copy this software for any purpose is hereby granted // without fee, provided the above notices are retained on all copies. // Permission to modify the code and to distribute modified code is granted, // provided the above notices are retained, and a notice that the code was // modified is included with the above copyright notice. // // If you use this code, drop me an email. I'd like to know if you find the code // useful. #include "stdafx.h" #include "PropCtrl.h" #include "PropItemEdit.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif CPropItemEdit::CPropItemEdit(CString* pText) { InitVariable(); m_pText = pText; } CPropItemEdit::CPropItemEdit(double* pDouble) { InitVariable(); m_pDouble = pDouble; } CPropItemEdit::CPropItemEdit(int* pInt) { InitVariable(); m_pInt = pInt; } void CPropItemEdit::InitVariable() { m_pText = NULL; m_pDouble = NULL; m_pInt = NULL; m_bPassword = FALSE; m_nItemType = ITEMTYPE_EDIT; } CPropItemEdit::~CPropItemEdit() { } BEGIN_MESSAGE_MAP(CPropItemEdit, CEdit) //{{AFX_MSG_MAP(CPropItemEdit) ON_WM_GETDLGCODE() ON_WM_KEYDOWN() ON_CONTROL_REFLECT(EN_KILLFOCUS, OnKillfocus) ON_CONTROL_REFLECT(EN_CHANGE, OnEditChanged) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CPropItemEdit message handlers void CPropItemEdit::DrawAttribute(CDC* pDC, const RECT& rc) { ASSERT(m_pProp!=NULL); // pDC->SelectObject(IsReadOnly() ? m_pProp->GetNormalFont() : m_pProp->GetBoldFont()); pDC->SelectObject(m_pProp->GetNormalFont()); if (IsReadOnly()) pDC->SetTextColor(RGB(140,140,140)); else pDC->SetTextColor(RGB(0,0,0)); pDC->SetBkMode(TRANSPARENT); CRect r = rc; TCHAR ch; // can't use GetPasswordChar(), because window may not be created yet ch = (m_bPassword) ? '*' : '\0'; CString csText = GetValueText(); if (ch) { for (LONG i=0; i < csText.GetLength();i++) csText.SetAt(i, ch); } pDC->DrawText(csText, r, DT_SINGLELINE|DT_VCENTER); } void CPropItemEdit::SetAsPassword(BOOL bPassword) { m_bPassword = bPassword; } void CPropItemEdit::OnMove() { if (IsWindow(m_hWnd)) SetWindowPos(NULL, m_rc.left, m_rc.top, m_rc.Width(), m_rc.Height(), SWP_NOZORDER|SWP_NOACTIVATE); } void CPropItemEdit::OnActivate() { // Check if the edit control needs creation if (!IsWindow(m_hWnd)) { DWORD dwStyle; dwStyle = WS_CHILD|ES_AUTOHSCROLL; Create(dwStyle, m_rc, m_pProp->GetCtrlParent(), GetCtrlID()); SendMessage(WM_SETFONT, (WPARAM)m_pProp->GetNormalFont()->m_hObject); } SetPasswordChar((TCHAR)(m_bPassword ? '*' : 0)); UpdateData(TRUE); SetWindowText(GetValueText()); SetSel(0, -1); SetWindowPos(NULL, m_rc.left, m_rc.top, m_rc.Width(), m_rc.Height(), SWP_NOZORDER|SWP_SHOWWINDOW); // 原来有按钮的话先隐藏先隐藏 ShowButton(FALSE); ShowSpinCtrl(FALSE); // add button on right if (m_bHasBt) CreateButton(this); if (m_bHasSpinCtrl) CreateSpinCtrl(this); SetFocus(); } UINT CPropItemEdit::OnGetDlgCode() { return CEdit::OnGetDlgCode()|DLGC_WANTALLKEYS; } /*----------------------------------------------------------------------------* * 描述: 添加响应Shift Tab Up Down键盘 切换焦点事件 * 备注: -----------------------------------------------------------------------------*/ void CPropItemEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) { CPropItem* pItem = NULL; // if (nChar==VK_RETURN) // CommitChanges(); if((nChar == VK_TAB && !GetAsyncKeyState(VK_SHIFT)) || nChar == VK_DOWN || nChar==VK_RETURN) pItem = m_pProp->FocusNext(); else if (nChar == VK_TAB || nChar == VK_UP) pItem = m_pProp->FocusPrev(); if (NULL != pItem && !pItem->IsReadOnly()) pItem->Activate(); CEdit::OnKeyDown(nChar, nRepCnt, nFlags); } void CPropItemEdit::OnRefresh() { } /*----------------------------------------------------------------------------* * 描述: 把值写入变量 * 备注: -----------------------------------------------------------------------------*/ void CPropItemEdit::OnCommit() { // hide edit control ShowWindow(SW_HIDE); // store edit text for GetItemValue CString csText; GetWindowText(csText); if (NULL != m_pText) *m_pText = csText; else if (NULL != m_pDouble) *m_pDouble = _tstof(csText); else if (NULL != m_pInt) *m_pInt = _ttoi(csText); } void CPropItemEdit::OnKillfocus() { CommitChanges(); CWnd* pWnd = m_pProp->GetParent(); if (NULL != pWnd) { ::SendMessage(pWnd->m_hWnd, WM_EBKILLFOCUS, (WPARAM)ITEMTYPE_EDIT, (LPARAM)this); } } void CPropItemEdit::OnEditChanged() { } /*----------------------------------------------------------------------------* * 描述: 得到要Draw画出显示的字符 * 备注: -----------------------------------------------------------------------------*/ CString CPropItemEdit::GetValueText() { CString csText; if (NULL != m_pText) csText = *m_pText; else if (NULL != m_pDouble) { csText.Format(_T("%.10f"), *m_pDouble); csText.TrimRight(_T("0")); csText.TrimRight(_T(".")); } else if (NULL != m_pInt) csText.Format(_T("%d"), *m_pInt); return csText; } /*----------------------------------------------------------------------------* * 描述: 设置当前值 * 备注: -----------------------------------------------------------------------------*/ void CPropItemEdit::SetValueText(CString csValue) { CString csText; if (NULL != m_pText) *m_pText = csValue; else if (NULL != m_pDouble) *m_pDouble = _tstof(csValue); else if (NULL != m_pInt) *m_pInt = _ttoi(csValue); }