我有一个类似写字版的程序,但如何才能加入一个类似VC里的自动填词功能?就是说在我写了某个单词的前缀时程序自动搜索我的备选字库如果发现某个前缀匹配就提交整个单词将之显示在当前编辑位置,这个提示窗应该由listBox动态完成,里面存放着我的备选词库,有词匹配时显示,不匹配时自动消失,类似VC 工作环境里那个界面,问题是在我的编辑窗口里如何构造这个动态窗口?谁有例子,将不盛感谢。

解决方案 »

  1.   

    1 产生一个STYLE为WS_CHILD的dialog模板
    2 并把它和一个CDialog派生类关联,派生类的构造函数加上
    Create(类名::IDD);
    3 添加一个处理WM_CHAR的函数,在函数里判断是否显示或隐藏窗口
    4 添加一个函数来确定窗口显示位置4在编辑窗口所在类里添加一个刚才产生类的成员变量指针,在合适的地方new
    5在处理输入单词的函数里面发送WM_CHAR消息给上面的dialog窗口
      

  2.   

    这么复杂能不能有代码?我现在时间不够用,拜托了。
    2 sunyard:
    你的意思是不是匹配一个就要new一个窗口?
    可我听说可以用enableWindow(show或hide)来做比较好。
      

  3.   

    new 一次就可以拉
    很简单的阿
      

  4.   

    你可有现成的码子?可以寄给我。多谢拉
    [email protected]
    大恩必重谢,这对我很重要
      

  5.   

    我临时写了一个很简单的演示代码,主要缺陷:显示窗口位置的计算。我只是简单的把它放在当前输入位置的下面class CMyEditView : public CEditView
    {
    protected: 
    CDialogTip* m_pDlgTip;
    CString m_strWord;
    public:
    virtual void OnInitialUpdate();
    afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
    };//没有考虑Delete键
    void CMyEditView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
    {
    CEditView::OnChar(nChar, nRepCnt, nFlags); CEdit &edit = GetEditCtrl();
    if (nChar == 8)//删除键
    {
    m_strWord.Delete(m_strWord.GetLength() - 1);
    }
    else
    {
    m_strWord += nChar;
    } CDC* pDC = edit.GetDC();
    CSize sz = pDC->GetTextExtent("A");//字符的高度
    edit.ReleaseDC(pDC);

    //计算当前输入字符的位置
    int nPosStart,nPosEnd;
    edit.GetSel(nPosStart,nPosEnd);//得到当前输入位置
    CPoint pt;
    pt.x = edit.PosFromChar(nPosStart - 1).x;//nPosStart always = nPosEnd 
    pt.y = (edit.LineFromChar(-1) + 1) * sz.cy;//edit.LineFromChar(-1) + 1) 计算当前的行数
    m_pDlgTip->EnterChar(m_strWord,pt);
    }void CMyEditView::OnInitialUpdate() 
    {
    CEditView::OnInitialUpdate();
    m_pDlgTip = new CDialogTip(&(this->GetEditCtrl()));
    }
    //点击鼠标,则隐藏窗口
    void CMyEditView::OnLButtonDown(UINT nFlags, CPoint point) 
    {
    // TODO: Add your message handler code here and/or call default
    m_strWord.Empty();
    m_pDlgTip->EnterChar(m_strWord,CPoint(0,0));//隐藏窗口

    CEditView::OnLButtonDown(nFlags, point);
    }
    class CDialogTip : public CDialog
    {
    public:
    void EnterChar(CString& strWord, CPoint pt);
    int m_nShow;
    CListBox m_List;
    };CDialogTip::CDialogTip(CWnd* pParent /*=NULL*/)
    : CDialog(CDialogTip::IDD, pParent)
    {
    //{{AFX_DATA_INIT(CDialogTip)
    // NOTE: the ClassWizard will add member initialization here
    //}}AFX_DATA_INIT
    Create(CDialogTip::IDD,pParent);
    m_nShow = SW_HIDE;
    }void CDialogTip::EnterChar(CString& strWord, CPoint pt) 
    {
    if (strWord.IsEmpty())
    {
    m_nShow = SW_HIDE;
    }
    if (m_List.FindString(-1, strWord) == LB_ERR)//找不到
    {
       strWord.Empty();
       m_nShow = SW_HIDE;
    }
    else if (SW_HIDE == m_nShow)
    {
    m_nShow = SW_SHOW;
    SetWindowPos(&wndTop,pt.x,pt.y,0,0,SWP_NOSIZE|SWP_SHOWWINDOW);
    }
    ShowWindow(m_nShow);
    }BOOL CDialogTip::OnInitDialog() 
    {
    CDialog::OnInitDialog();

    // TODO: Add extra initialization here
    m_List.AddString("aaaaaaa");
    m_List.AddString("bbbbbbb");
    m_List.AddString("ccccccc");
    return TRUE;  // return TRUE unless you set the focus to a control
                  // EXCEPTION: OCX Property Pages should return FALSE
    }
      

  6.   

    在你击剑的时候
    WPARAM wParam;
    LPARAM lparam;
    long pos;
    1)SendMessage 文本框的hwnd, EM_GETSEL, wParam, lParam
       pos = SendMessage(文本框的hwnd, EM_POSFROMCHAR, lParam, 0)
    然后拆分pos的高低位得到光标的位置
    2)然后发送一个
    index = SendMessage(组合框的hwnd, CB_FINDSTRING, -1, "关键字的字符“)
    找到那个index或者向listbox发送相应消息,得到索引,显示窗口。
      

  7.   

    A Spell Checking Engine
    By Matt Gullett 
    http://www.codeproject.com/cpp/spellchecker_mg.asp?target=completion
      

  8.   

    http://www.codeproject.com/cpp/#Spell Checkers