Microsoft KB Archive/76474

-

{| The information in this article applies to:
 * width="100%"|
 * Microsoft Windows Software Development Kit (SDK) for Windows versions 3.1 and 3.0

SUMMARY
In an application running under Windows version 3.0 or 3.1, when the user interacts with a dialog box and presses the ENTER key, the application accepts any changes made to information in any control of the dialog box and dismisses the dialog box. However, when the dialog box contains a multiline edit control (MLE), it may be considered more intuitive for the user to use the ENTER key to advance to the next line in the edit control. Windows 3.0 and 3.1 use the CTRL+ENTER key combination for this purpose.

Similarly, Windows uses the TAB key to move the input focus to the next control in the tabbing sequence in a dialog box. However, in an edit control, it might be useful for the user to be able to enter TAB characters. Windows uses the CTRL+TAB key combination for this purpose.

Windows version 3.1 defines the ES_WANTRETURN edit control style, which changes the behavior of the ENTER key in an MLE in a dialog box. If an application is designed to run only under Windows versions 3.1 and later, specify the ES_WANTRETURN edit control style. None of the three techniques described below is necessary.

This article discusses three different methods that can be used to modify the behavior of the ENTER key in an MLE in an application's dialog box. These methods are compatible with Windows versions 3.0 and 3.1. Methods 1 and 3 below can be adapted to modify the behavior of the TAB key, if desired.

A major disadvantage of any of these methods is that the ENTER or TAB key will no longer act as it does in other applications, and its behavior no longer will be what an experienced Windows user would expect.

MLEENTER is a file in the Microsoft Software Library that demonstrates two of the methods described in this document.

Download MLEENTER.EXE, a self-extracting file, from the Microsoft Software Library (MSL) on the following services:

  Microsoft Download Service (MSDL) Dial (206) 936-6735 to connect to MSDL Download MLEENTER.EXE (size: 23472 bytes)   Internet (anonymous FTP) ftp ftp.microsoft.com Change to the \SOFTLIB\MSLFILES directory Get MLEENTER.EXE (size: 23472 bytes) 

MORE INFORMATION
Under versions of Windows earlier than 3.0, an edit control subclass procedure could respond to the WM_GETDLGCODE message with DLGC_WANTALLKEYS to receive all keyboard input. This technique is not effective in Windows 3.0 or later.

METHOD 1
This method involves subclassing the MLE. It is most useful when the edit control is already subclassed for another reason because the amount of additional code is minimal. This method involves three steps:


 * 1) Check for a WM_KEYDOWN message with wParam set to VK_RETURN.
 * 2) Post a WM_CHAR message with wParam set to 0x0a to the edit control.
 * 3) Do not pass the WM_KEYDOWN message to the original window procedure by returning immediately from the subclass procedure.

The following code fragment demonstrates this method: // In the subclass procedure switch (msg) {       case WM_KEYDOWN: if (VK_RETURN == wParam) {               PostMessage(hWnd, WM_CHAR, 0x0A, 0L); return 0L; }           break;

...       }

return CallWindowProc(...); The disadvantage to subclassing is that it may be more fragile, with respect to changes in future versions of Windows, than other methods.

METHOD 2
This method uses the DM_GETDEFID message that Windows sends to a dialog box procedure when the user presses the ENTER key in a dialog box. This method has the additional advantage of easily handling multiple multiline edit controls in the same dialog box and involves three steps:


 * 1) Process WM_COMMAND messages with the HIWORD(lParam) set to EN_SETFOCUS and EN_KILLFOCUS to determine if an MLE has the focus. When EN_SETFOCUS signals that a MLE has the focus, set a static flag to TRUE. When EN_KILLFOCUS signals that a MLE has lost the focus, reset the flag to FALSE.
 * 2) When the dialog procedure receives a DM_GETDEFID message, an edit control has the focus, and the ENTER key is down, post a WM_CHAR message with wParam set to 0x0a to the edit control with the focus.
 * 3) If an edit control had the focus, return TRUE from the dialog function; otherwise, the function must return FALSE. Failing to return FALSE will prevent the ENTER key from behaving properly when an edit control does not have the focus.

The following code fragment demonstrates this procedure: static fEditFocus;

switch (msg) {       case WM_COMMAND: // ID_EDIT is a multiline edit control if (ID_EDIT == wParam) {               if (EN_KILLFOCUS == HIWORD(lParam)) fEditFocus = FALSE;

if (EN_SETFOCUS == HIWORD(lParam)) fEditFocus = TRUE; }           else ...           break;

case DM_GETDEFID: /*            * Check if an edit control has the focus and that * the ENTER key is down. DM_GETDEFID may be sent * in other situations when the user did not press * the ENTER key. */           if (fEditFocus && (0x8000 & GetKeyState(VK_RETURN))) {               PostMessage(hEdit, WM_CHAR, 0x0A, 0L); return TRUE; }           break; }   return FALSE;

METHOD 3
Applications that use modeless dialog boxes are required to filter messages through the IsDialogMessage function. The IsDialogMessage function modifies certain messages to implement dialog box behavior. For example, the ENTER key message is modified to generate a WM_COMMAND message with wParam set to IDOK.

An application is, however, free to modify the message before passing it to the IsDialogMessage function. The code example below modifies the WM_KEYDOWN message containing a VK_RETURN to be a EM_REPLACESEL message with a carriage return (CR) and linefeed (LF) combination.

The disadvantage to this method is that it places additional code in the main message loop for the application, slowing the processing of every message. In addition, code in the message loop is far removed from the dialog procedure, and is therefore harder to maintain.

The following code fragment demonstrates this method:

/*    * hWndEditControl is the handle to the multiline edit control. * hWndModeless is the handle to the modeless dialog box. */

while (GetMessage(&msg, NULL, 0, 0)) {       if (hWndEditControl == msg.hWnd                && WM_KEYDOWN == msg.message                && VK_RETURN == msg.wParam) /*            * Normally, Windows will translate this to IDOK. * Perform a custom translation to something more useful * (replace selection with a carriage return-linefeed). */           {            msg.message = EM_REPLACESEL; msg.wParam = 0; msg.lParam = (long)(LPSTR)&quot;\015\012&quot;; }

if (!IsDialogMessage(hWndModeless, &msg)) {           TranslateMessage(&msg); DispatchMessage(&msg); }       } If the functionality of a modal dialog box is desired in an application, it can be simulated by using a modeless dialog box that disables its parent window when the dialog is created.
 * }

-

Last reviewed: February 15, 1996

© 1998 Microsoft Corporation. All rights reserved. Terms of Use.