我有一个类似写字版的程序,但如何才能加入一个类似VC里的自动填词功能?就是说在我写了某个单词的前缀时程序自动搜索我的备选字库如果发现某个前缀匹配就提交整个单词将之显示在当前编辑位置,这个提示窗应该由listBox动态完成,里面存放着我的备选词库,有词匹配时显示,不匹配时自动消失,类似VC 工作环境里那个界面,问题是在我的编辑窗口里如何构造这个动态窗口?谁有例子,将不盛感谢。
解决方案 »
- 既然WM_DESTROY已经销毁了窗口,那么谁来执行WM_NCDESTROY?
- WM_RBUTTONUP和WM_LBUTTONUP的消息问题
- SetWindowPos怎么没起作用呀?晕菜!!
- 如何用VC判断当前机器内存的使用情况?
- CString 不能在 dll 中使用?奇怪了
- 请教,如何用VC++编写传真软件?
- ListCtrl视图中的数据怎么重新写入数据库呢,请教,在线等待!!
- 救命呀,这个gdi+怎么编译通过呀?看了以前的帖子也没有帮助
- Scan Disk is now checking drive C for errors 。开机“滴“一声,
- 高分请教关于prn的问题
- 如何自定义一个基于对话框的程序的ClassName或WindowName?
- 100分求救,错误:unresolved external symbol "void __cdecl swap(class IntArray &,int,int)" (?swap@@YAXAAVIntArray@@HH@Z) 如何解决
2 并把它和一个CDialog派生类关联,派生类的构造函数加上
Create(类名::IDD);
3 添加一个处理WM_CHAR的函数,在函数里判断是否显示或隐藏窗口
4 添加一个函数来确定窗口显示位置4在编辑窗口所在类里添加一个刚才产生类的成员变量指针,在合适的地方new
5在处理输入单词的函数里面发送WM_CHAR消息给上面的dialog窗口
2 sunyard:
你的意思是不是匹配一个就要new一个窗口?
可我听说可以用enableWindow(show或hide)来做比较好。
很简单的阿
[email protected]
大恩必重谢,这对我很重要
{
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
}
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发送相应消息,得到索引,显示窗口。
By Matt Gullett
http://www.codeproject.com/cpp/spellchecker_mg.asp?target=completion