Blame view

central_clearing_system/ETradeClient/mfc_ui/etrade_edit_control.cpp 5.22 KB
1
2
3
4
#include "stdafx.h"
#include "etrade_edit_control.h"
#include "resource.h"
#include "place_order_dlg.h"
5
6
7
#include "etradeclient/utility/win_http.h"
#include "etradeclient/utility/logging.h"
#include "ETradeClient/utility/win_msg_define.h"
8
9
10

IMPLEMENT_DYNAMIC( CEditBox, CMFCMaskedEdit )
11
12
13

CEditBox::CEditBox()
{
14
	m_clrFrame = RGB( 245, 108, 108 );
15
	is_edit_red_ = false;
16
	check_type_ = DoNotCheck;
17
18
19
20
21
22
}

CEditBox::~CEditBox()
{
}
23
BEGIN_MESSAGE_MAP(CEditBox, CEdit)
24
25
	ON_WM_NCPAINT()
	ON_WM_KILLFOCUS()
26
	ON_WM_CHAR()
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
END_MESSAGE_MAP()


void CEditBox::OnNcPaint()
{
	CEdit::OnNcPaint();

	if (is_edit_red_)
	{
		CRect	rcEdit;
		CBrush	brushOut;
		CDC		*pDC = GetWindowDC();

		GetWindowRect(rcEdit);
		ScreenToClient(&rcEdit);
		rcEdit.OffsetRect(CSize(2, 2));

		brushOut.CreateSolidBrush(m_clrFrame);
		pDC->FrameRect(rcEdit, &brushOut);
		ReleaseDC(pDC);
	}

}

void CEditBox::OnKillFocus(CWnd* pNewWnd)
{
	CEdit::OnKillFocus(pNewWnd);

	int dlg_id = GetDlgCtrlID();
56
57
58
59
60
61
62
63
64
65
66
67
68
69
	GetParent()->SendMessage(WM_ORDER_KILL_FOCUS, dlg_id);
}

void CEditBox::SetEditRed(bool is_red)
{
	is_edit_red_ = is_red;
	UpdateWindow();
}

void CEditBox::SetCheck(CheckType type)
{
	check_type_ = type;
}
70
71
72
73
void CEditBox::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	if (check_type_ == DoNotCheck || nChar < 32 || nChar > 126)
74
	{
75
76
77
78
79
80
81
82
83
84
85
86
87
88
		CEdit::OnChar(nChar, nRepCnt, nFlags);
		return;
	}

	CString sBefore, sAfter;
	GetWindowText(sBefore);
	//保存光标位置
	int nPosCurbgn, nPosCurend;
	GetSel(nPosCurbgn, nPosCurend);
	CEdit::OnChar(nChar, nRepCnt, nFlags);

	GetWindowText(sAfter);
	bool result = true;
	switch (check_type_)
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
	case CardNumType:
	{
		result = CheckCardNum(sAfter);
		break;
	}
	case CommNumType:
	{
		result = CheckCommNum(sAfter);
		break;
	}
	case UnitType:
	{
		result = CheckUnitNum(sAfter);
		break;
	}
	case HeavyType:
	{
		result = CheckHeavy(sAfter);
		break;
	}
	case PriceType:
	{
		result = CheckPrice(sAfter);
		break;
	}
	case CountType:
	{
		result = CheckCount(sAfter);
		break;
	}
	}

	if (!result)
	{
		SetWindowText(sBefore);
		SetSel(nPosCurbgn, nPosCurend, true);
	}
}

bool CEditBox::CheckCardNum(CString num)
{
	bool result = true;

	do 
	{
135
		/*if (num.GetLength() > 12)
136
		{
137
138
139
		result = false;
		break;
		}*/
140
141

		if (num.SpanIncluding(L"0123456789") != num)
142
		{
143
144
			result = false;
			break;
145
		}
146
	} while (0);
147
148
149
	return result;
}
150
151
152
153
154
155
bool CEditBox::CheckCommNum(CString num)
{
	bool result = true;

	do 
156
	{
157
158
159
160
161
		if (num.GetLength() > 3)
		{
			result = false;
			break;
		}
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
		if (num.SpanIncluding(L"0123456789") != num)
		{
			result = false;
			break;
		}
	} while (0);

	return result;
}

bool CEditBox::CheckUnitNum(CString num)
{
	bool result = true;

	if (num.Compare(L"1") != 0 && num.Compare(L"2") != 0)
	{
		result = false;
180
	}
181
182

	return result;
183
184
}
185
bool CEditBox::CheckHeavy(CString heavy)
186
187
188
189
190
{
	bool result = true;

	do 
	{
191
192
		int length = heavy.GetLength();
		if (length > 8)
193
194
195
196
197
		{
			result = false;
			break;
		}
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
		if (heavy.SpanIncluding(L"0123456789.") != heavy)
		{
			result = false;
			break;
		}

		double num = _ttof(heavy);
		if (num > 100000)
		{
			result = false;
			break;
		}

		//如果精度超过设置的精度则返回
		int dec_pos = heavy.Find(L".");
		if (dec_pos != CB_ERR && length - dec_pos - 1 > 1)
214
215
216
217
		{
			result = false;
			break;
		}
218
219
220
221
222
223
		//小数点不在首位
		if (length > 0 && heavy.Left(1) == ".")
		{
			result = false;
			break;
		}
224
225
226
227
228
229
230
231
232
233
234

		//首位为0时,次首位只能是小数点
		if (length == 2 && heavy.Left(1) == "0")
		{
			if (heavy.Right(1) != ".")
			{
				result = false;
				break;
			}
		}
235
236
237
238
239
240
241
		//只有一个小数点
		if (heavy.Replace(L".", L".") > 1)
		{
			result = false;
			break;
		}
242
	} while (0);
243
244
245
246
247
248
249

	return result;
}

bool CEditBox::CheckPrice(CString price)
{
	bool result = true;
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
276
277
278
279
280
281
282
283
284
285
	do
	{
		int length = price.GetLength();
		if (length > 8)
		{
			result = false;
			break;
		}

		if (price.SpanIncluding(L"0123456789.") != price)
		{
			result = false;
			break;
		}

		double num = _ttof(price);
		if (num > 50000)
		{
			result = false;
			break;
		}

		//如果精度超过设置的精度则返回
		int dec_pos = price.Find(L".");
		if (dec_pos != CB_ERR && length - dec_pos - 1 > 2)
		{
			result = false;
			break;
		}
		//小数点不在首位
		if (length > 0 && price.Left(1) == ".")
		{
			result = false;
			break;
		}
286
287
288
289
290
291
292
293
294
295
296

		//首位为0时,次首位只能是小数点
		if (length == 2 && price.Left(1) == "0")
		{
			if (price.Right(1) != ".")
			{
				result = false;
				break;
			}
		}
297
298
299
300
301
302
303
304
305
		//只有一个小数点
		if (price.Replace(L".", L".") > 1)
		{
			result = false;
			break;
		}

	} while (0);
306
307
308
	return result;
}
309
bool CEditBox::CheckCount(CString count)
310
311
312
{
	bool result = true;
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
	do
	{
		int length = count.GetLength();
		if (length > 8)
		{
			result = false;
			break;
		}

		if (count.SpanIncluding(L"0123456789.") != count)
		{
			result = false;
			break;
		}

		double num = _ttof(count);
		if (num > 100000)
		{
			result = false;
			break;
		}

		//如果精度超过设置的精度则返回
		int dec_pos = count.Find(L".");
		if (dec_pos != CB_ERR && length - dec_pos - 1 > 1)
		{
			result = false;
			break;
		}
		//小数点不在首位
		if (length > 0 && count.Left(1) == ".")
		{
			result = false;
			break;
		}
		//只有一个小数点,这里有个坑,必须用L".",不能用'.',否则始终返回0
		if (count.Replace(L".", L".") > 1)
		{
			result = false;
			break;
		}
354
355
	} while (0);
356
357
358
359

	return result;
}
360
bool CEditBox::IsEditRed()
361
{
362
	return is_edit_red_;
363
}