#include "stdafx.h" #include "KTInplaceComboBox.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif KTInplaceComboBox::KTInplaceComboBox() { } KTInplaceComboBox::KTInplaceComboBox( int nItem, int nSubItem, CStringList *psList) { m_nItem = nItem; m_nSubItem = nSubItem; if ( psList != NULL) { m_sList.AddTail( psList ); } m_bVK_ESCAPE = FALSE; } KTInplaceComboBox::~KTInplaceComboBox() { } BEGIN_MESSAGE_MAP(KTInplaceComboBox, CComboBox) //{{AFX_MSG_MAP(KTInplaceComboBox) ON_WM_CREATE() ON_WM_NCDESTROY() ON_WM_KILLFOCUS() ON_WM_SIZE() ON_CONTROL_REFLECT(CBN_CLOSEUP, OnCloseup) ON_WM_CHAR() //}}AFX_MSG_MAP END_MESSAGE_MAP() BOOL KTInplaceComboBox::PreTranslateMessage(MSG* pMsg) { if( pMsg->message == WM_KEYDOWN ) { if(pMsg->wParam == VK_RETURN || pMsg->wParam == VK_ESCAPE) { ::TranslateMessage(pMsg); ::DispatchMessage(pMsg); return 1; } } return CComboBox::PreTranslateMessage(pMsg); } int KTInplaceComboBox::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CComboBox::OnCreate(lpCreateStruct) == -1) return -1; CFont* font = GetParent()->GetFont(); SetFont(font); POSITION pos = m_sList.GetHeadPosition(); while(pos != NULL) { AddString((LPCTSTR)(m_sList.GetNext(pos))); } SetFocus(); return 0; } void KTInplaceComboBox::OnNcDestroy() { CComboBox::OnNcDestroy(); delete this; } void KTInplaceComboBox::OnKillFocus(CWnd* pNewWnd) { int nIndex = GetCurSel(); CComboBox::OnKillFocus(pNewWnd); CString str; GetWindowText(str); LV_DISPINFO lvDispinfo; lvDispinfo.hdr.hwndFrom = GetParent()->m_hWnd; lvDispinfo.hdr.idFrom = GetDlgCtrlID(); lvDispinfo.hdr.code = LVN_ENDLABELEDIT; lvDispinfo.item.mask = LVIF_TEXT | LVIF_PARAM; lvDispinfo.item.iItem = m_nItem; lvDispinfo.item.iSubItem = m_nSubItem; lvDispinfo.item.pszText = m_bVK_ESCAPE ? NULL : LPTSTR((LPCTSTR)str); lvDispinfo.item.cchTextMax = str.GetLength(); lvDispinfo.item.lParam = GetItemData(GetCurSel()); CWnd * pPParent = GetParent()->GetParent(); int nCtrlID = GetParent()->GetDlgCtrlID(); if(nIndex != CB_ERR && pPParent != NULL) { pPParent->SendMessage(WM_NOTIFY,nCtrlID,(LPARAM)&lvDispinfo); } PostMessage(WM_CLOSE); } void KTInplaceComboBox::OnSize(UINT nType, int cx, int cy) { CComboBox::OnSize(nType, cx, cy); } void KTInplaceComboBox::OnCloseup() { GetParent()->SetFocus(); } void KTInplaceComboBox::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) { if(nChar == VK_ESCAPE || nChar == VK_RETURN) { if( nChar == VK_ESCAPE) m_bVK_ESCAPE = 1; GetParent()->SetFocus(); return; } CComboBox::OnChar(nChar, nRepCnt, nFlags); }