Implementing a Line-Based Interface for Edit Controls

Last reviewed: November 2, 1995
Article ID: Q92626
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) versions 3.1
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT versions 3.5 and 3.51
        - Microsoft Windows 95 version 4.0
    

SUMMARY

In specific situations, it may be desirable to make multiline edit controls behave similar to list boxes, such that entries can be selected and manipulated on a per-line basis. This article describes how to implement the line-based interface.

MORE INFORMATION

A multiline edit control must be subclassed to achieve the desired behavior. The subclass function is outlined below.

Most of the work necessary to implement a line-based interface is done by the predefined window function of the edit control class. With the return value from the EM_LINEINDEX message, the offset of the line under the caret can be determined; with the length of that line retrieved via the EM_LINELENGTH message, the EM_SETSEL message can be used to highlight the current line.

There are two problems with this approach:

  • The first problem is that the EM_LINEINDEX message, when sent to the control with wParam=-1, returns the line index of the caret, which is not necessarily the same as the current mouse position. Thus, upon receiving the WM_LBUTTONDOWN message, the subclass function should first call the old window function, which will set the caret to the character under the current mouse position, then compute the beginning and ending offsets of the corresponding line, and eventually set the selection to that line.
  • The other problem is that the WM_MOUSEMOVE message should be ignored by the subclassing function because otherwise the built-in selection mechanism will change the selection when the mouse is being dragged with the left mouse button pressed, thus defeating the purpose.

Following is the subclassing function that follows from this discussion:

WNDPROC EditSubClassProc(HWND hWnd,

               UINT wMsg,
               WPARAM wParam,
               LPARAM lParam)
{ int iLineBeg, iLineEnd;
  long lSelection;
  switch (wMsg)
  { case WM_MOUSEMOVE:
     break;                       /* Swallow mouse move messages.  */
    case WM_LBUTTONDOWN:          /* First pass on, then process.  */
     CallWindowProc((FARPROC)lpfnOldEditFn,hWnd,wMsg,wParam,lParam);
     iLineBeg = SendMessage(hWnd,EM_LINEINDEX,-1,0);
     iLineEnd=iLineBeg+SendMessage(hWnd,EM_LINELENGTH,iLineBeg,0);
#ifndef WIN32
     SendMessage(hWnd,EM_SETSEL,0,MAKELPARAM(iLeneBeg,iLineEnd));
#else
     SendMessage(hWnd,EM_SETSEL,iLineBeg,iLine)  /* Win 32 rearranges
                      parameters.  */
#endif
     break;
    case WM_LBUTTONDBLCLK:
     lSelection = SendMessage(hWnd,EM_GETSEL,0,0);
     /* Now we have the indices to the beginning and end of the line in
        the LOWORD and HIWORD of lSelection, respectively.
        Do something with it... */
     break;
    default:

return(CallWindowProc((FARPROC)lpfnOldEditFn,hWnd,wMsg,wParam,lParam)); }; return(0); }


Additional reference words: 3.10 3.50 3.51 4.00 95
KBCategory: kbui
KBSubcategory: UsrCtl


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 2, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.