-
Notifications
You must be signed in to change notification settings - Fork 52
/
EditBox.cpp
174 lines (159 loc) · 3.82 KB
/
EditBox.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
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
// EditBox.cpp : 实现文件
#include "stdafx.h"
#include "FontMakerApp.h"
#include "EditBox.h"
static int UnicodeToUTF8(const WCHAR* input, char* output, int size)
{
return WideCharToMultiByte(CP_UTF8, 0, input, -1, output, size, NULL, NULL);
}
static int UTF8ToUnicode(const char* input, WCHAR* output, int size)
{
return MultiByteToWideChar(CP_UTF8, 0, input, -1, output, size);
}
// CEditBox 对话框
IMPLEMENT_DYNAMIC(CEditBox, CDialog)
CEditBox::CEditBox(CWnd* pParent /*=NULL*/)
: CDialog(CEditBox::IDD, pParent)
{
m_pzTable = (WCHAR*)malloc(4);
wcscpy_s(m_pzTable, 4, L"0");
}
CEditBox::~CEditBox()
{
if(m_pzTable != NULL)
{
free(m_pzTable);
}
}
void CEditBox::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_EDIT_TABLE, m_editTable);
}
BEGIN_MESSAGE_MAP(CEditBox, CDialog)
ON_BN_CLICKED(IDC_BTN_LOAD, &CEditBox::OnBnClickedBtnLoad)
ON_BN_CLICKED(IDC_BTN_SAVE, &CEditBox::OnBnClickedBtnSave)
ON_BN_CLICKED(IDC_BTN_NUM, &CEditBox::OnBnClickedBtnNum)
ON_BN_CLICKED(IDC_BTN_LETTER, &CEditBox::OnBnClickedBtnLetter)
ON_BN_CLICKED(IDOK, &CEditBox::OnBnClickedOk)
END_MESSAGE_MAP()
// CEditBox 消息处理程序
void CEditBox::OnBnClickedBtnLoad()
{
int len;
CFile cf;
WCHAR *pstr;
CHAR *pbuff;
CString name;
UINT size;
CFileDialog fd(1,NULL,NULL,OFN_HIDEREADONLY|OFN_FILEMUSTEXIST|OFN_ENABLESIZING,L"文本文件|*.txt|所有文件|*.*||");
if(fd.DoModal()!=IDOK)
{
return;
}
name = fd.GetPathName();
if(!cf.Open(name,CFile::modeRead|CFile::shareDenyNone))
{
MessageBox(L"无法打开文件!",L"载入失败",MB_OK|MB_ICONINFORMATION);
return;
}
size = (UINT)cf.GetLength();
pbuff = (CHAR*)malloc(size+2);
if(pbuff == NULL)
{
cf.Close();
MessageBox(L"内存不足!",L"载入失败",MB_OK|MB_ICONINFORMATION);
return;
}
pstr = (WCHAR*)malloc(size*2+2);
if(pstr == NULL)
{
free(pbuff);
cf.Close();
MessageBox(L"内存不足!",L"载入失败",MB_OK|MB_ICONINFORMATION);
return;
}
memset(pbuff,0,size+2);
if(cf.Read(pbuff,size)!=size)
{
free(pbuff);
free(pstr);
cf.Close();
MessageBox(L"读取文件时发生错误!",L"载入失败",MB_OK|MB_ICONINFORMATION);
return;
}
len = UTF8ToUnicode(pbuff,pstr,size*2);
m_editTable.SetWindowText(pstr);
free(pbuff);
free(pstr);
cf.Close();
}
void CEditBox::OnBnClickedBtnSave()
{
int len;
CFile cf;
WCHAR *pstr;
CHAR *pbuff;
CString name;
CFileDialog fd(0,NULL,NULL,OFN_OVERWRITEPROMPT|OFN_ENABLESIZING,L"所有文件|*.*||");
if(fd.DoModal()!=IDOK)
{
return;
}
name = fd.GetPathName();
if(!cf.Open(name,CFile::modeCreate|CFile::modeReadWrite|CFile::shareDenyNone))
{
MessageBox(L"无法创建文件!",L"保存失败",MB_OK|MB_ICONINFORMATION);
return;
}
len = m_editTable.GetWindowTextLength();
pstr = (WCHAR*)malloc(len*2+2);
pbuff= (CHAR*)malloc(len*2+2);
m_editTable.GetWindowText(pstr,len+1);
len = UnicodeToUTF8(pstr,pbuff,len*2+2);
cf.Write(pbuff,len-1);
free(pstr);
free(pbuff);
cf.Close();
}
void CEditBox::OnBnClickedBtnNum()
{
m_editTable.ReplaceSel(L"0123456789");
m_editTable.SetFocus();
}
void CEditBox::OnBnClickedBtnLetter()
{
m_editTable.ReplaceSel(L"abcdefghijklmnopqrstuvwxyz");
m_editTable.ReplaceSel(L"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
m_editTable.SetFocus();
}
void CEditBox::OnBnClickedOk()
{
int len;
len = m_editTable.GetWindowTextLength();
if(len<=0)
{
MessageBox(L"码表不能为空!",L"警告",MB_OK|MB_ICONWARNING);
return;
}
if(m_pzTable!=NULL)
{
free(m_pzTable);
}
m_pzTable = (WCHAR*)malloc(len*2+2);
if(m_pzTable != NULL)
{
m_editTable.GetWindowText(m_pzTable,len+1);
}
OnOK();
}
BOOL CEditBox::OnInitDialog()
{
CDialog::OnInitDialog();
SetWindowText(L"自定义码表");
if(m_pzTable!=NULL)
{
m_editTable.SetWindowText(m_pzTable);
}
return TRUE;
}