Handling Notification Messages

All window controls respond to user input or changes to the control by sending a notification message to its parent window. A notification message is a WM_COMMAND message that includes a control identifier and a notification code identifying the nature of the event. An application must collect these notification messages and respond to them.

The following code example shows one method of trapping a WM_COMMAND message.

BOOL CALLBACK AboutDialogProc (
                        HWND hwndDlg,     // Handle to the dialog box
                        UINT uMsg,        // Message
                        WPARAM wParam,    // First message parameter
                        LPARAM lParam)    // Second message parameter
{
  switch (uMsg)
  {
    case WM_INITDIALOG:
      return TRUE;  

    case WM_COMMAND:
      switch (LOWORD (wParam))
      {
        case IDOK:
          EndDialog (hwndDlg, IDOK);
          return TRUE;

        case IDCANCEL:
          EndDialog (hwndDlg, IDCANCEL);
          return TRUE;
      }
      break;
  }
  return FALSE;
}

Some window controls receive messages as well as generate them. Typically, a window procedure sends a message to a control directing it to execute a task. The control processes the message and carries out the requested action. Windows CE has several predefined messages, such as WM_GETTEXT and WM_GETDLGCODE, that it sends to controls. These messages typically correspond to window-management functions that carry out actions on windows. The window procedure for an application-defined control processes any predefined control message that affects the operation of the control. The following table shows these messages.

Message
Recommendation
WM_GETDLGCODE Process if the control uses the ENTER, ESC, TAB, or arrow keys. The IsDialogMessage function sends this message to controls in a dialog box to determine whether to process the keys or pass them to the control.
WM_GETFONT Process if the WM_SETFONT message is also processed.
WM_GETTEXT Process if the control text is not the same as the title specified by the CreateWindowEx function.
WM_GETTEXTLENGTH Process if the control text is not the same as the title specified by the CreateWindowEx function.
WM_KILLFOCUS Process if the control displays a caret, a focus rectangle, or another item to indicate that it has the input focus.
WM_SETFOCUS Process if the control displays a caret, a focus rectangle, or another item to indicate that it has the input focus.
WM_SETTEXT Process if the control text is not the same as the title specified by the CreateWindowEx function.
WM_SETFONT Process if the control displays text. Windows CE sends this message when creating a dialog box that has the DS_SETFONT style.

You can also send messages to a control by calling the SendMessage function. One control that calls the SendMessage function to receive messages is the button control.