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
|
// CExtEdit
// ExtEdit.cpp : implementation file
//
#include "stdafx.h"
#include "Phoenix.h"
#include "Utilities.h"
#include "ExtEdit.h"
IMPLEMENT_DYNAMIC(CExtEdit, CEdit)
CExtEdit::CExtEdit()
{
m_EditType = eDouble;
}
CExtEdit::~CExtEdit()
{
}
BEGIN_MESSAGE_MAP(CExtEdit, CEdit)
ON_WM_GETDLGCODE()
ON_WM_CHAR()
ON_WM_SETFOCUS()
END_MESSAGE_MAP()
// CExtEdit message handlers
UINT CExtEdit::OnGetDlgCode()
{
return CEdit::OnGetDlgCode()+VK_RETURN;
}
void CExtEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
CString str;
GetWindowText(str); // Save string before adding new character
CEdit::OnChar(nChar, nRepCnt, nFlags);
DWORD dwCurPos = GetSel();
double val;
// Verify char according selected type
switch (m_EditType)
{
case eAll:
break;
case eINT:
break;
case eDouble:
{
CString NewStr;
GetWindowText(NewStr);
NewStr.MakeLower();
int l = NewStr.GetLength()-1;
if (l>0)
{
// Remove 'e' if it is located at the end
if (NewStr.Find(_T("e")) == l)
NewStr.Delete(l);
// Remove 'e' & '+' or '-' if it is located at the end
int pos = NewStr.Find(_T("+"));
if (pos == l && NewStr.GetAt(l-1)=='e')
NewStr.Delete(l-1,2);
pos = NewStr.Find(_T("-"));
if (pos == l && NewStr.GetAt(l-1)=='e')
NewStr.Delete(l-1,2);
}
if (l==0) //only one char
{
if (NewStr.GetAt(0)=='+'||NewStr.GetAt(0)=='-')
NewStr.Delete(0);
}
if (ReadDouble(NewStr,&val) == RD_NOT_NUMBER )
{
// Cancel character
SetWindowText(str);
SetSel(dwCurPos,dwCurPos);
// PostMessage(WM_KEYDOWN,VK_END,0);
// PostMessage(EM_SETSEL,0,-1);
}
}
break;
}
// Notify Enter key to the parent window
if (nChar == VK_RETURN)
{
NMHDR hdr;
hdr.hwndFrom = GetSafeHwnd();
hdr.idFrom = GetDlgCtrlID();
hdr.code = WM_CHAR;
GetParent()->SendMessage(WM_NOTIFY, GetDlgCtrlID(), (LPARAM)&hdr);
}
}
void CExtEdit::OnSetFocus(CWnd* pOldWnd)
{
PostMessage(EM_SETSEL,0,-1);
CEdit::OnSetFocus(pOldWnd);
} |
Partager