功能如图:
串操作函数:
CString CDlgTestDlg::GetAutoCompleteStr(CString strCurrent,CStringArray& strArray)
{
CString strReturnValue(_T(""));
LONG strLen;
INT nIndex= -1;
WCHAR chMin;
strCurrent.TrimLeft();
strLen = strCurrent.GetLength();
if (strLen ==0)
{
return strReturnValue;
}
if (m_ArrayStr.GetSize() ==0) //没有可比较的源串
{
return strReturnValue;
}
CStringArray strArrayTemp;
strArrayTemp.Copy(strArray);//不对源串操作
for (int i=0;i< strArrayTemp.GetSize();i++)
{
CString str;
str = strArrayTemp.GetAt(i);
if (strCurrent.Compare(str.Left(strLen)) != 0)
{
strArrayTemp.RemoveAt(i);
--i;
}
}
INT nNewArrayLen = strArrayTemp.GetSize(); // 处理之后的串
if (nNewArrayLen == 0)
{
return strReturnValue;
}
CString str;
str = strArrayTemp.GetAt(0);
chMin = *(str.GetBuffer(str.GetLength()) + strLen);
nIndex = 0;
str.ReleaseBuffer();
for (int j=1;j <nNewArrayLen;j++ )
{
CString str;
WCHAR ch;
str = strArrayTemp.GetAt(j);
ch = *(str.GetBuffer(str.GetLength()) + strLen);
str.ReleaseBuffer();
if (ch < chMin)
{
nIndex = j;
chMin = ch;
}
}
strReturnValue = strArrayTemp.GetAt(nIndex);
return strReturnValue;
}
使用函数:
void CDlgTestDlg::OnEditchangeCombo1()
{
// TODO: Add your control notification handler code here
CString strInput;
CString strAutoComplete;
m_comBox.GetWindowText(strInput);
if (GetKeyState(VK_BACK ) & 0x80)
{
strAutoComplete = GetAutoCompleteStr(_T(""),m_ArrayStr);
}
else
{
strAutoComplete = GetAutoCompleteStr(strInput,m_ArrayStr);
}
if (strAutoComplete.IsEmpty())
{
m_comBox.SetWindowText(strInput);
m_comBox.SetEditSel(strInput.GetLength(),-1);
}
else
{
m_comBox.SetWindowText(strAutoComplete);
m_comBox.SetEditSel(strInput.GetLength(),-1);
}
}