0

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

这是带有字体列表的组合B。只需要选择一个按键。谢谢!

4

1 回答 1

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!

于 2012-08-01T11:32:26.213 回答