Repository navigation
Expand file tree
/
Copy pathAutoComplete.cpp
More file actions
277 lines (162 loc) · 5.02 KB
/
Copy pathAutoComplete.cpp
File metadata and controls
277 lines (162 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// AutoComplete.cpp: implementation of the CAutoComplete class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "AutoComplete.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CAutoComplete::CAutoComplete()
{
m_bIgnoreChangeMsg=0;
m_iType = 0;
m_idMyControl = 0;
m_iCurString = 0;
}
CAutoComplete::~CAutoComplete()
{
}
////////////////////////////////////////////////////////////////////////
// 安装 hook. 初始化控制 ID 和基于类名的控制类型
//
void CAutoComplete::Init(CWnd* pWnd)
{
CSubclassWnd::HookWindow(pWnd->GetParent());
CString sClassName;
::GetClassName(pWnd->GetSafeHwnd(), sClassName.GetBuffer(32), 32);
sClassName.ReleaseBuffer();
if (sClassName=="Edit") {
m_iType = Edit;
} else if (sClassName=="ComboBox") {
m_iType = ComboBox;
}
m_idMyControl = pWnd->GetDlgCtrlID();
}
//////////////////////////////////////////////////////////////////////////
// 扫描串数组中匹配的文本,并添加匹配项到一个新数组,返回匹配项的数目。
// 对于编辑控制而言, 只需要找到第一个串,在这里使用了一个BOOL参数。
//
//
UINT CAutoComplete::GetMatches(LPCTSTR pszText, CStringArray& arMatches,
BOOL bFirstOnly)
{
arMatches.RemoveAll();
int nMatch = 0;
CString s=pszText;
if (s.GetLength()>0)
{
OnFirstString();
CString sMatch;
while (OnNextString(sMatch))
{
if (OnMatchString(s, sMatch))
{
TRACE("Add %s\n",(LPCTSTR)sMatch);
arMatches.Add(sMatch);
nMatch++;
if (bFirstOnly)
break;
}
}
}
return nMatch;
}
///////////////////////////////////////////////////////////////////////////
// 这个虚拟函数的参数是两个字符串,如果有匹配则返回 TRUE. 缺省情况下实现普通的
// 前缀比较-你可以重载它,例如,在忽略掉两个串中的‘www’字符。
//
//
BOOL CAutoComplete::OnMatchString(const CString& s, const CString& sMatch)
{
return s==sMatch.Left(s.GetLength());
}
void CAutoComplete::OnFirstString()
{
m_iCurString=0;
}
BOOL CAutoComplete::OnNextString(CString& sNext)
{
if (m_iCurString < m_arStrings.GetSize())
{
sNext = m_arStrings[m_iCurString++];
return TRUE;
}
sNext = (LPCTSTR)NULL;
return FALSE;
}
////////////////////////////////////////////////////////////////////////
// "hook" 函数截取发送到编辑控制和组合框的消息.我只对 EN_CHANGE 或
// CBN_EDITCHANGE感兴趣: 编辑控制的内容已经改变.
//
LRESULT CAutoComplete::WindowProc(UINT msg, WPARAM wp, LPARAM lp)
{
if ((msg==WM_COMMAND && LOWORD(wp)==m_idMyControl) &&
((m_iType==Edit && HIWORD(wp)==EN_CHANGE) ||
(m_iType==ComboBox && HIWORD(wp)==CBN_EDITCHANGE))) {
// 因为我要改变控制的内容,它将触发更多的EN_CHANGE消息,
// 当我获得控制时使用 m_bIgnoreChangeMsg 结束处理.
if (!m_bIgnoreChangeMsg++) {
CString s;
CWnd* pWnd = CWnd::FromHandle((HWND)lp);
pWnd->GetWindowText(s);
OnComplete(pWnd, s);
}
m_bIgnoreChangeMsg--;
}
return CSubclassWnd::WindowProc(msg, wp, lp);
}
////////////////////////////////////////////////////////////////////////
// 这是实现输入完成的主函数.
//
void CAutoComplete::OnComplete(CWnd* pWnd, CString s)
{
CStringArray arMatches; // 匹配串
GetMatches(s,arMatches);
if (s.GetLength() && arMatches.GetSize()&&m_iType)
DoCompletion(pWnd, s, arMatches);
m_sPrevious=s; // 记住当前串
}
void CAutoComplete::DoCompletion(CWnd* pWnd, CString s,
const CStringArray& arMatches)
{
if (m_iType==ComboBox)
{
// 这种强制转换从技术上讲是不正确的,但它是一个标准的MFC诀窍.
CComboBox* pComboBox = (CComboBox*)pWnd;
// 更新下拉框以反映可能的匹配
pComboBox->ResetContent();
for (int i=0; i <arMatches.GetSize();i++)
pComboBox->AddString(arMatches[i]);
// 用户箭头光标,这样用户才能选择
::SetCursor(LoadCursor(NULL,MAKEINTRESOURCE(IDC_ARROW)));
// 显示下拉框
pComboBox->ShowDropDown();
pComboBox->SetWindowText(IgnoreCompletion(s) ? s : arMatches[0]);
pComboBox->SetEditSel(s.GetLength(),-1); //设置高亮
}
else if (m_iType==Edit && !IgnoreCompletion(s))
{
// 这种强制转换从技术上讲是不正确的,但它是一个标准的MFC诀窍.
CEdit* pEdit = (CEdit*)pWnd;
pEdit->SetWindowText(arMatches[0]);
pEdit->SetSel(s.GetLength(),-1);
}
}
////////////////////////////////////////////////////////////////////////
// 当用户按下Backspace键删除敲入的字符时,这个函数用于关闭输入完成特性。
// 这时,当前的串将匹配最后输入的(前一个)串。在这种情况下,输入完成特性
// 是被屏蔽掉的。例如,如果用户敲如‘foo’,程序将自动完成‘foobar’,字符
// ‘bar’是高亮,如果用户按下Backspace键或者Delete键删除‘bar’时,程序
// 不会再次完成foobar,而是保留‘foo’,即便是用户继续按Backspace键也一样。
// 这个函数是我写的唯一一个说明比代码本身还长函数:)!
//
//
BOOL CAutoComplete::IgnoreCompletion(CString s)
{
return s==m_sPrevious.Left(s.GetLength());
}