MFC组合框,搜索字体列表

时间:2012-07-31 14:27:21

标签: c++ mfc combobox

如何使用A-z keydown事件在Combobox中实现搜索?更糟糕的是,这是工具栏中的CMFCToolBarButton。

这是comboB的字体列表。只需要选择一个带有keydown的。 谢谢!

1 个答案:

答案 0 :(得分:0)

这是解决方案。 工作良好。

    void C_dropDlg::OnEditUpdate()
{
    if (!m_bAutoComplete) 
      return;

  // Get the text in the edit box
      CString str;
      MyDropDown.GetWindowText(str);
      int nLength = str.GetLength();

      // Currently selected range
      DWORD dwCurSel = MyDropDown.GetEditSel();
      WORD dStart = LOWORD(dwCurSel);
      WORD dEnd   = HIWORD(dwCurSel);

  // Search for, and select in, and string in the combo box that is prefixed
  // by the text in the edit box
      if (MyDropDown.SelectString(-1, str) == CB_ERR)
      {
          SetWindowText(str);       // No text selected, so restore what was there before
          if (dwCurSel != CB_ERR)
            MyDropDown.SetEditSel(dStart, dEnd);   //restore cursor postion
      }

  // Set the text selection as the additional text that we have added
          if (dEnd < nLength && dwCurSel != CB_ERR)
              MyDropDown.SetEditSel(dStart, dEnd);
          else
              MyDropDown.SetEditSel(nLength, -1);

}
    // TODO: Add your control notification handler code here





BOOL C_dropDlg::PreTranslateMessage(MSG* pMsg)
{
    // Need to check for backspace/delete. These will modify the text in
    // the edit box, causing the auto complete to just add back the text
    // the user has just tried to delete. 

    if (pMsg->message == WM_KEYDOWN)
    {
        m_bAutoComplete = TRUE;

        int nVirtKey = (int) pMsg->wParam;
        if (nVirtKey == VK_DELETE || nVirtKey == VK_BACK)
            m_bAutoComplete = FALSE;
        if(nVirtKey == VK_ESCAPE)
        {

        }
    }

    return CDialogEx::PreTranslateMessage(pMsg);

}

我已经失去了与解决方案的联系,但感谢作者。

别忘了在Base类中定义m_AutoComplete!