HOWTO: Implement a Line-Based Interface for Edit Controls

ID: Q92626


The information in this article applies to:
  • Microsoft Win32 Software Development Kit (SDK)
  • Microsoft Windows Software Development Kit (SDK) 3.1


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 query words:

Keywords : kbCtrl kbEditCtrl kbNTOS kbGrpUser kbWinOS
Version : WINDOWS:3.1
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: December 3, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.