etrade_edit_control.cpp 5.22 KB
#include "stdafx.h"
#include "etrade_edit_control.h"
#include "resource.h"
#include "place_order_dlg.h"
#include "etradeclient/utility/win_http.h"
#include "etradeclient/utility/logging.h"
#include "ETradeClient/utility/win_msg_define.h"


IMPLEMENT_DYNAMIC( CEditBox, CMFCMaskedEdit )

CEditBox::CEditBox()
{
	m_clrFrame = RGB( 245, 108, 108 );
	is_edit_red_ = false;
	check_type_ = DoNotCheck;
}

CEditBox::~CEditBox()
{
}

BEGIN_MESSAGE_MAP(CEditBox, CEdit)
	ON_WM_NCPAINT()
	ON_WM_KILLFOCUS()
	ON_WM_CHAR()
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();
	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;
}


void CEditBox::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	if (check_type_ == DoNotCheck || nChar < 32 || nChar == 127)
	{
		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_)
	{
	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 
	{
		/*if (num.GetLength() > 12)
		{
		result = false;
		break;
		}*/

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

	return result;
}

bool CEditBox::CheckCommNum(CString num)
{
	bool result = true;
	
	do 
	{
		if (num.GetLength() > 3)
		{
			result = false;
			break;
		}

		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;
	}

	return result;
}

bool CEditBox::CheckHeavy(CString heavy)
{
	bool result = true;

	do 
	{
		int length = heavy.GetLength();
		if (length > 8)
		{
			result = false;
			break;
		}

		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)
		{
			result = false;
			break;
		}
		//小数点不在首位
		if (length > 0 && heavy.Left(1) == ".")
		{
			result = false;
			break;
		}

		//首位为0时,次首位只能是小数点
		if (length == 2 && heavy.Left(1) == "0")
		{
			if (heavy.Right(1) != ".")
			{
				result = false;
				break;
			}
		}

		//只有一个小数点
		if (heavy.Replace(L".", L".") > 1)
		{
			result = false;
			break;
		}

	} while (0);

	return result;
}

bool CEditBox::CheckPrice(CString price)
{
	bool result = true;
	
	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;
		}

		//首位为0时,次首位只能是小数点
		if (length == 2 && price.Left(1) == "0")
		{
			if (price.Right(1) != ".")
			{
				result = false;
				break;
			}
		}

		//只有一个小数点
		if (price.Replace(L".", L".") > 1)
		{
			result = false;
			break;
		}

	} while (0);

	return result;
}

bool CEditBox::CheckCount(CString count)
{
	bool result = true;

	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;
		}

	} while (0);

	return result;
}

bool CEditBox::IsEditRed()
{
	return is_edit_red_;
}