Creatively Using Control IDs

HEXCALC contains no header file with identifiers for all the ID numbers of the child window controls in the dialog box template. We can dispense with this file because the ID number for each of the push-button controls is set to the ASCII code of the text that appears in the control. This means that WndProc can treat WM_COMMAND messages and WM_CHAR messages in much the same way. In each case, wParam is the ASCII code of the button.

Of course, a little massaging of the keyboard messages is necessary. WndProc traps WM_KEYDOWN messages to translate the Left Arrow key to a Backspace key. During processing of WM_CHAR messages, WndProc converts the character code to uppercase and the Enter key to the ASCII code for the Equals key.

The validity of a WM_CHAR message is checked by calling GetDlgItem. If the GetDlgItem function returns 0, then the keyboard character is not one of the ID numbers defined in the dialog box template. If the character is one of the IDs, however, the appropriate button is flashed by sending it a couple of BM_SETSTATE messages:

if (hButton = GetDlgItem (hwnd, wParam))

{

SendMessage (hButton, BM_SETSTATE, 1, 0L) ;

SendMessage (hButton, BM_SETSTATE, 0, 0L) ;

}

This adds a nice touch to HEXCALC's keyboard interface, and with a minimum of effort.

When WndProc processes WM_COMMAND messages, it always sets the input focus to the parent window:

case WM_COMMAND :

SetFocus (hwnd) ;

Otherwise, the input focus would be shifted to one of the buttons whenever it was clicked with the mouse.