How to Handle the Back Key for Dialog Boxes Without Edit Controls

4/19/2010

When a user presses the Back key in a dialog box that does not have any edit controls, the Back key sends a Windows Embedded CE WM_COMMAND message with the command ID IDCANCEL to the dialog box. The following code example illustrates how to handle the WM_COMMAND message.

BOOL rb;
int rc;

{
    switch(LOWORD(wParam))
    {
        // User closes the dialog box by pressing the Back key.
        {
            rb = EndDialog(hwnd, 0);
            if (rb == FALSE)  // EndDialog failed.
            {
                rc = MessageBox(NULL, _T("Could not destroy dialog box."),
                                _T("Error"), MB_OK);
                if (rc == 0)  // Not enough memory to create MessageBox.
                    return E_OUTOFMEMORY;
                return E_FAIL;  // Replace with specific error handling.
            }
            else  // EndDialog succeeded.
                return S_OK;
        }  // case IDCANCEL
    }  // switch
    break;  // from case WM_COMMAND
}

See Also

Concepts

Navigation Keys