123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- #include "stdafx.h"
- #include "autocompl.h"
- #include "LYFZIPManage.h"
- CAutoComplete::CAutoComplete()
- {
- m_bIgnoreChangeMsg=0;
- m_iType = 0;
- m_idMyControl = 0;
- m_iCurString = 0;
- }
- CAutoComplete::~CAutoComplete()
- {
- }
- void CAutoComplete::Init(CWnd* pWnd)
- {
- CSubclassWnd::HookWindow(pWnd->GetParent());
- CString sClassName;
- ::GetClassName(pWnd->GetSafeHwnd(), sClassName.GetBuffer(32), 32);
- sClassName.ReleaseBuffer();
- if (sClassName=="Edit") {
- m_iType = Edit;
- } else if (sClassName=="ComboBox") {
- m_iType = ComboBox;
- }
- m_idMyControl = pWnd->GetDlgCtrlID();
- }
- UINT CAutoComplete::GetMatches(LPCTSTR pszText, CStringArray& arMatches,
- BOOL bFirstOnly)
- {
- arMatches.RemoveAll();
- int nMatch = 0;
- CString s=pszText;
- if (s.GetLength()>0) {
- OnFirstString();
- CString sMatch;
- while (OnNextString(sMatch)) {
- if (OnMatchString(s, sMatch)) {
- TRACE("Add %s\n",(LPCTSTR)sMatch);
- arMatches.Add(sMatch);
- nMatch++;
- if (bFirstOnly)
- break;
- }
- }
- }
- return nMatch;
- }
- BOOL CAutoComplete::OnMatchString(const CString& s, const CString& sMatch)
- {
- return s==sMatch.Left(s.GetLength());
- }
- void CAutoComplete::OnFirstString()
- {
- m_iCurString=0;
- }
- BOOL CAutoComplete::OnNextString(CString& sNext)
- {
- if (m_iCurString < m_arStrings.GetSize()) {
- sNext = m_arStrings[m_iCurString++];
- return TRUE;
- }
- sNext = (LPCTSTR)NULL;
- return FALSE;
- }
- LRESULT CAutoComplete::WindowProc(UINT msg, WPARAM wp, LPARAM lp)
- {
- if ((msg==WM_COMMAND && LOWORD(wp)==m_idMyControl) &&
- ((m_iType==Edit && HIWORD(wp)==EN_CHANGE) ||
- (m_iType==ComboBox && HIWORD(wp)==CBN_EDITCHANGE))) {
-
-
-
- if (!m_bIgnoreChangeMsg++) {
- CString s;
- CWnd* pWnd = CWnd::FromHandle((HWND)lp);
- pWnd->GetWindowText(s);
- OnComplete(pWnd, s);
- }
- m_bIgnoreChangeMsg--;
- }
- return CSubclassWnd::WindowProc(msg, wp, lp);
- }
- void CAutoComplete::OnComplete(CWnd* pWnd, CString m_filter)
- {
- CStringArray arMatches;
-
-
-
- m_filter.TrimLeft ();
- int ii;
- if (m_filter.GetLength()>0)
- {
- m_posarray.RemoveAll ();
- int type=GetType(m_filter);
- if(type==1)
- {
- for(ii=0; ii<m_pArray->GetSize (); ii++)
- {
- if(m_pArray->ElementAt (ii).ElementAt (12).Find (m_filter)!=-1 || \
- m_pArray->ElementAt (ii).ElementAt (13).Find (m_filter)!=-1)
- {
- m_posarray.Add (ii);
- arMatches.Add (m_pArray->ElementAt (ii).ElementAt (1)+m_pArray->ElementAt (ii).ElementAt (2));
- }
- }
- }
- else if(type==2)
- {
- m_filter.MakeUpper ();
- for(ii=0; ii<m_pArray->GetSize (); ii++)
- {
- if(m_pArray->ElementAt (ii).ElementAt (14).Find (m_filter)!=-1 || \
- m_pArray->ElementAt (ii).ElementAt (15).Find (m_filter)!=-1)
- {
- m_posarray.Add (ii);
- arMatches.Add (m_pArray->ElementAt (ii).ElementAt (1)+m_pArray->ElementAt (ii).ElementAt (2));
- }
- }
- }
- else
- {
- for(ii=0; ii<m_pArray->GetSize (); ii++)
- {
- if(m_pArray->ElementAt (ii).ElementAt (0).Find (m_filter)!=-1 ||\
- m_pArray->ElementAt (ii).ElementAt (1).Find (m_filter)!=-1 || \
- m_pArray->ElementAt (ii).ElementAt (2).Find (m_filter)!=-1 || \
- m_pArray->ElementAt (ii).ElementAt (7).Find (m_filter)!=-1 || \
- m_pArray->ElementAt (ii).ElementAt (8).Find (m_filter)!=-1 || \
- m_pArray->ElementAt (ii).ElementAt (9).Find (m_filter)!=-1|| \
- m_pArray->ElementAt (ii).ElementAt (10).Find (m_filter)!=-1|| \
- m_pArray->ElementAt (ii).ElementAt (12).Find (m_filter)!=-1 || \
- m_pArray->ElementAt (ii).ElementAt (13).Find (m_filter)!=-1 )
- {
- m_posarray.Add (ii);
- arMatches.Add (m_pArray->ElementAt (ii).ElementAt (1)+m_pArray->ElementAt (ii).ElementAt (2));
- }
- }
- }
- DoCompletion(pWnd, m_filter, arMatches);
- }
- m_sPrevious=m_filter;
- }
- void CAutoComplete::DoCompletion(CWnd* pWnd, CString s,
- const CStringArray& arMatches)
- {
- if (m_iType==ComboBox) {
-
- CComboBox* pComboBox = (CComboBox*)pWnd;
-
- pComboBox->ResetContent();
- for (int i=0; i<arMatches.GetSize(); i++) {
- pComboBox->AddString(arMatches[i]);
- }
-
- ::SetCursor(LoadCursor(NULL,MAKEINTRESOURCE(IDC_ARROW)));
-
- pComboBox->ShowDropDown();
-
- pComboBox->SetWindowText(s);
- pComboBox->SetEditSel(s.GetLength(),-1);
- } else if (m_iType==Edit && !IgnoreCompletion(s)) {
-
- CEdit* pEdit = (CEdit*)pWnd;
- pEdit->SetWindowText(arMatches[0]);
- pEdit->SetSel(s.GetLength(),-1);
- }
- }
- BOOL CAutoComplete::IgnoreCompletion(CString s)
- {
- return s==m_sPrevious.Left(s.GetLength());
- }
|