列表框自动匹配

来源:互联网 发布:淘宝公益宝贝标志 编辑:程序博客网 时间:2024/05/16 14:44

转载 : http://topic.csdn.net/u/20100827/14/1d9bb139-c14b-4eb6-bec6-58976318199e.html

新建一个类继承于CComboBox,添加OnCommand消息。在里面添加以下代码即可

BOOL AutoCompletion::OnCommand(WPARAM wParam, LPARAM lParam)
{
 // TODO: Add your specialized code here and/or call the base class
 if ( HIWORD(wParam) == EN_CHANGE )
 {
 //  if (!m_bWorkOnText) 
 //  return true;
  //ShowDropDown(TRUE);
  //::SetCursor(::LoadCursor(NULL, IDC_ARROW));
  //取得编辑框中的文字;
  CString strWindowText;
  GetWindowText(strWindowText);
  int nLength = strWindowText.GetLength();

  //取得当前的选择范围
  DWORD dwCurSel = GetEditSel();
  WORD dStart = LOWORD(dwCurSel);
  WORD dEnd = HIWORD(dwCurSel);

  // 实际做查找和选择的语句
  int nChoice;
  if ((nChoice=SelectString(-1, strWindowText)) == CB_ERR)
  {
  SetWindowText(strWindowText); // 恢复文字
  if (dwCurSel != CB_ERR)
  SetEditSel(dStart, dEnd); // 恢复原先的位置
  }
  else
  {
  ShowDropDown(TRUE); //使用这个鼠标会不见
    ::SetCursor(::LoadCursor(NULL,IDC_ARROW));
    SetCurSel(nChoice);

  }

  // 把添加的文字做为选择的文字
  if (dEnd < nLength && dwCurSel != CB_ERR)
  SetEditSel(dStart, dEnd);
  else
  SetEditSel(nLength, -1);

  return true;
 }
 else
  return CComboBox::OnCommand(wParam, lParam);
}