Speaking of subclassing controls - i have subclassed an edit control to make sure that the user only enters unsigned integers no greater or less than 255 or 0. But the code looks really ugly. Any suggestions for improving this, or perhaps I have overlooked a much simpler way of achieving this.
Code:
LRESULT CALLBACK CApp::EditRedProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
BOOL fError;
UINT value;
LRESULT lresult;
HWND hParent = GetParent(hwnd);
if (msg == WM_CHAR)
{
// We have to allow the backspace character through!
if ((char)wParam == '\b')
return CallWindowProc(OldRedEditProc, hwnd, msg, wParam, lParam);
// Make sure we have an actual digit
if (!isdigit((char)wParam))
return 0;
else
{
lresult = CallWindowProc(OldRedEditProc, hwnd, msg, wParam, lParam);
value = GetDlgItemInt(hParent, IDC_REDEDIT, &fError, FALSE);
if (value > 255 || value < 0 || fError == FALSE)
{
// Clean out the edit control
SendMessage(hwnd, WM_SETTEXT, 0, (LPARAM)TEXT(""));
}
return lresult;
}
}
else
return CallWindowProc(OldRedEditProc, hwnd, msg, wParam, lParam);
}