-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCEditEx.cpp
92 lines (74 loc) · 2.15 KB
/
CEditEx.cpp
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
// CEditEx.cpp: 实现文件
//
#include "pch.h"
#include "FlagCalculator.h"
#include "CEditEx.h"
// CEditEx
IMPLEMENT_DYNAMIC(CEditEx, CEdit)
CEditEx::CEditEx()
{
}
CEditEx::~CEditEx()
{
}
BEGIN_MESSAGE_MAP(CEditEx, CEdit)
ON_WM_CHAR()
ON_WM_PASTE()
END_MESSAGE_MAP()
// CEditEx 消息处理程序
void CEditEx::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// 输入限制,只能输入0和1和退格键
if ((nChar == '0' || nChar == '1') || nChar == 0x08)
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
void CEditEx::OnPaste()
{
// 复制限制,只接受0和1字符输入
if (OpenClipboard())
{
HANDLE hData = GetClipboardData(CF_TEXT);
if (hData != NULL)
{
char* pszText = static_cast<char*>(GlobalLock(hData));
if (pszText)
{
CString strClipText(pszText), strFiltered;
GlobalUnlock(hData);
for (int i = 0; i < strClipText.GetLength(); ++i)
// 这里过滤,只允许0和1
if (strClipText[i] == '0' || strClipText[i] == '1')
strFiltered.AppendChar(strClipText[i]);
if (!strFiltered.IsEmpty())
ReplaceSel(strFiltered, TRUE); // 粘贴过滤后的文本
}
}
CloseClipboard();
}
}
BOOL CEditEx::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN)
{
UINT nCode = pMsg->wParam;
if (::GetFocus() == m_hWnd && (GetKeyState(VK_CONTROL) & 0xFF00) == 0xFF00)
{
if (nCode == 'A' || nCode == 'a') // 全选
this->SetSel(0, -1);
else if (nCode == 'C' || nCode == 'c') // 复制
this->Copy();
else if (nCode == 'X' || nCode == 'x') // 剪切
this->Cut();
else if (nCode == 'V' || nCode == 'v') // 粘贴
this->Paste();
else if (nCode == 'Z' || nCode == 'z') // 撤销
this->Undo();
return true;
}
}
return CEdit::PreTranslateMessage(pMsg);
}
void CEditEx::OnLButtonDblClk(UINT nFlags, CPoint point)
{
SetSel(0, -1);
}