HOWTO: Implement a Line-Based Interface for Edit Controls

ID: Q92626

The information in this article applies to:

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:

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 
Issue type        : kbhowto

Last Reviewed: December 26, 1998