Die Funktion OnChar überwacht die Eingabe. In Abhängigkeit von dem eingegebenen Zeichen reagiert sie unterschiedlich:
void CHertzEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if (iscntrl(nChar))
{
CEdit::OnChar(nChar, nRepCnt, nFlags);
return;
}
CString str_nChar(nChar);
CString strZiel;
if (m_bNegativAllowed)
strZiel=str_nChar.SpanIncluding("0123456789kKmMhHzZ.-");
else
strZiel=str_nChar.SpanIncluding("0123456789kKmMhHzZ.");
// Ist nChar aus dem erlaubten Zeichensatz(s.o.) ?
if (strZiel.IsEmpty())
return;
// Ist nChar eine Zahl ?
strZiel=str_nChar.SpanIncluding(".kKmMhHzZ");
if (strZiel.IsEmpty()) {
// Zahl ?, ja dann // Basisklasse
CEdit::OnChar(nChar, nRepCnt, nFlags);
return;
}
// Hole den Inhalt des Edit-Controls
int nTextLen=GetWindowTextLength();
char* pChar=new char[nTextLen+1]; // incl. abschliessender \0
GetWindowText(pChar, nTextLen+1);
CString strText(pChar);
delete[] pChar;
if (nChar=='.') {
// ist schon ein Punkt da
if (strText.Find('.')== -1) {
// nein -> Basisklasse
CEdit::OnChar(nChar, nRepCnt, nFlags);
return;
} else {
// ja -> Abgang
return;
}
}
BOOL wasK, wasM, wasHz;
// Einheit sukzessive abschneiden und testen
if (strText.Remove('m') || strText.Remove('M')) wasM=TRUE;
else wasM=FALSE;
if (strText.Remove('k') || strText.Remove('K')) wasK=TRUE;
else wasK=FALSE;
if (strText.Remove('h') || strText.Remove('H')) wasHz=TRUE;
else wasHz=FALSE;
if ((strText.Remove('z') || strText.Remove('Z')) && wasHz)
wasHz=TRUE;
else
wasHz=FALSE;
strText.Remove(' ');
if (nChar=='k' || nChar=='K') {
strText+=" KHz";
SetWindowText(strText);
SetModify();
return;
}
if (nChar=='m' || nChar=='M') {
strText+=" MHz";
SetWindowText(strText);
SetModify();
return;
}
if (nChar=='h' || nChar=='H' || nChar=='z' || nChar=='Z') {
if (wasK) {
strText+=" KHz";
SetWindowText(strText);
SetModify();
return;
}
if (wasM) {
strText+=" MHz";
SetWindowText(strText);
SetModify();
return;
}
strText+=" Hz";
SetWindowText(strText);
SetModify();
return;
}
ASSERT(FALSE); // sollte nie erreicht werden
}