AutoCompletionComboBox.cpp
资源名称:ComboBox.rar [点击查看]
上传用户:hbjdyb2005
上传日期:2021-01-26
资源大小:168k
文件大小:2k
源码类别:
组合框控件
开发平台:
Visual C++
- // AutoCompletionComboBox.cpp : implementation file
- //
- #include "stdafx.h"
- #include "AutoCompletionComboBox.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CAutoCompletionComboBox
- CAutoCompletionComboBox::CAutoCompletionComboBox()
- : CComboBox(),
- m_pEdit(NULL)
- {
- }
- CAutoCompletionComboBox::~CAutoCompletionComboBox()
- {
- }
- BEGIN_MESSAGE_MAP(CAutoCompletionComboBox, CComboBox)
- //{{AFX_MSG_MAP(CAutoCompletionComboBox)
- // NOTE - the ClassWizard will add and remove mapping macros here.
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CAutoCompletionComboBox message handlers
- void CAutoCompletionComboBox::HandleCompletion()
- {
- // Make sure we can 'talk' to the edit control
- if ( m_pEdit == NULL )
- {
- m_pEdit = new CEdit();
- m_pEdit->SubclassWindow(GetDlgItem(1001)->GetSafeHwnd());
- }
- // Save the state of the edit control
- CString windowtext;
- m_pEdit->GetWindowText(windowtext);
- int start,end;
- m_pEdit->GetSel(start,end);
- // Perform actual completion
- int bestindex = -1;
- int bestfrom = INT_MAX;
- for ( int x = 0; x < GetCount(); x++ )
- {
- CString s;
- GetLBText(x,s);
- int from = s.Find(windowtext);
- if ( from != -1 && from < bestfrom )
- {
- bestindex = x;
- bestfrom = from;
- }
- }
- if ( bestindex != -1 && GetCurSel() != bestindex )
- {
- // Select the matching entry in the list
- ShowDropDown(TRUE);
- SetCurSel(bestindex);
- // Restore the edit control
- m_pEdit->SetWindowText(windowtext);
- m_pEdit->SetSel(start,end);
- }
- }
- BOOL CAutoCompletionComboBox::OnCommand(WPARAM wParam, LPARAM lParam)
- {
- if ( HIWORD(wParam) == EN_CHANGE )
- {
- HandleCompletion();
- return true;
- }
- return CComboBox::OnCommand(wParam, lParam);
- }