Registrations are now open. Join us today!
There is still a lot of work to do on the wiki yet! More information about editing can be found here.
Already have an account?

Microsoft KB Archive/92626

From BetaArchive Wiki

HOWTO: Implement a Line-Based Interface for Edit Controls

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:

   LRESULT CALLBACK 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((WNDPROC)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(iLineBeg,iLineEnd));
   #else
        SendMessage(hWnd,EM_SETSEL,iLineBeg,iLineEnd)  /* 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((WNDROC)lpfnOldEditFn,hWnd,wMsg,wParam,lParam));
   };
   return(0);
   } 



If STRICT is defined, the lpfnOldEditFn has the datatype WNDPROC. If STRICT is not defined and also for 16-bit programming lpfnOldEditFn has the data type FARPROC.

The WNDPROC type is declared as follows:
LRESULT (CALLBACK *WNDPROC) (HWND, UINT, WPARAM, LPARAM);

The FARPROC type is declard as follows:
int (FAR WINAPI * FARPROC)();

For more information please take a look at the MSDN documentation for CallWindowProc.

Additional query words:

Keywords : kbCtrl kbEditCtrl kbOSWinNT kbSDKWin32 kbGrpDSUser kbOSWin
Issue type : kbhowto
Technology : kbWin32SDKSearch kbAudDeveloper kbWin3xSearch kbSDKSearch kbWin32sSearch kbWinSDKSearch kbWinSDK310


Last Reviewed: February 6, 2000
© 2001 Microsoft Corporation. All rights reserved. Terms of Use.