ComboBoxEx.cpp
上传用户:qzzxgm
上传日期:2009-12-14
资源大小:1882k
文件大小:2k
- #include "stdafx.h"
- #include "ComboBoxEx.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CAutoComplete
- CAutoComplete::CAutoComplete()
- {
- m_bAutoComplete = TRUE;
- }
- CAutoComplete::~CAutoComplete()
- {
- }
- BEGIN_MESSAGE_MAP(CAutoComplete, CComboBox)
- //{{AFX_MSG_MAP(CAutoComplete)
- ON_CONTROL_REFLECT(CBN_EDITUPDATE, OnEditUpdate)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CAutoComplete message handlers
- //处理消息循环
- BOOL CAutoComplete::PreTranslateMessage(MSG* pMsg)
- {
- //捕捉按键消息
- if (pMsg->message == WM_KEYDOWN)
- {
- m_bAutoComplete = TRUE;
- int nVirtKey = (int) pMsg->wParam;
- if (nVirtKey == VK_DELETE || nVirtKey == VK_BACK)
- m_bAutoComplete = FALSE;
- }
- return CComboBox::PreTranslateMessage(pMsg);
- }
- //编辑框更新消息
- void CAutoComplete::OnEditUpdate()
- {
- if (!m_bAutoComplete)
- return;
- //获得编辑框的字符串
- CString str;
- GetWindowText(str);
- int nLength = str.GetLength();
-
- //当前选择范围
- DWORD dwCurSel = GetEditSel();
- WORD dStart = LOWORD(dwCurSel);
- WORD dEnd = HIWORD(dwCurSel);
- //在列表框中搜索与编辑框中匹配的字符串,然后使其被选择
- if (SelectString(-1, str) == CB_ERR)
- {
- SetWindowText(str);
- if (dwCurSel != CB_ERR)
- SetEditSel(dStart, dEnd);
- }
- if (dEnd < nLength && dwCurSel != CB_ERR)
- SetEditSel(dStart, dEnd);
- else
- SetEditSel(nLength, -1);
- }