-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtendedEdit.cpp
More file actions
95 lines (82 loc) · 2.25 KB
/
ExtendedEdit.cpp
File metadata and controls
95 lines (82 loc) · 2.25 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
#include "pch.h"
#include "ExtendedEdit.h"
#include "mregexp.h"
BEGIN_MESSAGE_MAP(CExtendedEdit, CEdit)
ON_WM_CHAR()
// ON_CONTROL_REFLECT(EN_CHANGE, OnEnChange)
END_MESSAGE_MAP()
CExtendedEdit::CExtendedEdit()
{
m_limited = FALSE;
}
void CExtendedEdit::SetInputLimit(BOOL limited, std::vector<const char*> patterns, std::vector<const char*> reservpatterns)
{
m_limited = limited;
m_patterns = patterns;
m_reservpatterns = reservpatterns;
}
BOOL CExtendedEdit::IsMatchPattern(CString text)
{
int count = m_patterns.size();
int reservcount = m_reservpatterns.size();
for (int i = 0; i < count; i++) {
MRegexp * reg = mregexp_compile(m_patterns[i]);
MRegexpMatch m;
BOOL ret = mregexp_match(reg, text.GetBuffer(), &m);
mregexp_free(reg);
if (ret) {
BOOL reservMatch = FALSE;
for (int i = 0; i < reservcount; i++) {
MRegexp * reg = mregexp_compile(m_reservpatterns[i]);
MRegexpMatch m;
BOOL retReserv = mregexp_match(reg, text.GetBuffer(), &m);
mregexp_free(reg);
if (retReserv) {
reservMatch = TRUE;
}
}
if (!reservMatch) {
// 匹配任意一个正向法则,且不匹配任意一个反向法则,则通过
return TRUE;
}
}
}
return FALSE;
}
void CExtendedEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if (!m_limited) {
CEdit::OnChar(nChar, nRepCnt, nFlags);
return;
}
int posCur = CharFromPos(GetCaretPos());
CString strText;
GetWindowText(strText);
// 允许删除键
if (nChar == VK_BACK) {
// if (posCur > strText.GetLength() - 2) {
// return;
// }
//else
{
CEdit::OnChar(nChar, nRepCnt, nFlags);
return;
}
} else if (!(nChar == '.' || (nChar >= '0' && nChar <= '9') || nChar == ' ')) {
return;
}
if(nChar == '.' && strText.Find('.') == -1) {
CEdit::OnChar(nChar, nRepCnt, nFlags);
return;
}
CString chchar;
chchar.Format("%c", nChar);
CString strTextAfer = strText;
strTextAfer.Insert(posCur, chchar);
if (IsMatchPattern(strTextAfer)) {
CEdit::OnChar(nChar, nRepCnt, nFlags);
} else {
// 不符合模式
MessageBeep(0);
}
}