How To Implement a Line-Based Interface for Edit Controls (92626)
The information in this article applies to:
- Microsoft Platform Software Development Kit (SDK) 1.0
- Microsoft Windows Software Development Kit (SDK) 3.1
This article was previously published under Q92626 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.
Modification Type: | Minor | Last Reviewed: | 7/11/2005 |
---|
Keywords: | kbCtrl kbEditCtrl kbhowto KB92626 |
---|
|