case IDCALC:
{
WCHAR wszBuffer[40], *pwszEnd;
double InitialValue;
double CurrentValue;
double Value;
/*
* Get the "Initial" value as a string (from the dialog control).
*/
GetDlgItemText(hwndDlg, IDC_INITVALUE, wszBuffer, NELEMS(wszBuffer));
/*
* Convert the "Initial" value to a double-precision floating-point value
* (pwszEnd will point right after the converted characters).
*/
InitialValue = wcstod(wszBuffer, &pwszEnd);
/*
* Check the remaining part of the string (we keep it simple, and just
* check for the string terminator).
*/
if (*pwszEnd != L'\0')
{
/* The value seems wrong. Display an error message, set the input focus
to the problematic field, and leave */
MessageBox(hwndDlg,
L"Sorry, can't understand this value. Try again.",
L"Input error",
MB_OK|MB_SETFOREGROUND);
SetDlgItemFocus(hwndDlg, IDC_INITVALUE);
break;
}
/*
* Get the "Current" value as a string (from the dialog control).
*/
GetDlgItemText(hwndDlg, IDC_CURRVALUE, wszBuffer, NELEMS(wszBuffer));
/*
* Convert the "Current" value to a double-precision floating-point value
* (pwszEnd will point right after the converted characters).
*/
CurrentValue = wcstod(wszBuffer, &pwszEnd);
/*
* Check the remaining part of the string (we keep it simple, and just
* check for the string terminator).
*/
if (*pwszEnd != L'\0')
{
/* The value seems wrong. Display an error message, set the input focus
to the problematic field, and leave */
MessageBox(hwndDlg,
L"Sorry, can't understand this value. Try again.",
L"Input error",
MB_OK|MB_SETFOREGROUND);
SetDlgItemFocus(hwndDlg, IDC_CURRVALUE);
break;
}
/*
* Amazing. We seem to have two correct values. Do the calculation.
*/
Value = ((CurrentValue - InitialValue) / InitialValue) * 100.0;
/*
* Convert the result to a string. We use %G, but other formats are possible.
* (The format must be accepted by the *Microsoft* function swprintf,
* since we are using the Microsoft C runtime on Pocket PC.
*/
swprintf(wszBuffer, L"%G", Value);
/*
* Put the string into the "Percentage Change" dialog field.
*/
SetDlgItemText(hwndDlg, IDC_CHANGE, wszBuffer);
break;
}
case IDCLEAR:
{ SetDlgItemText(hwndDlg, IDC_INITVALUE, L"");
SetDlgItemText(hwndDlg, IDC_CURRVALUE, L"");
SetDlgItemText(hwndDlg, IDC_CHANGE, L"");
break;
} |