Author: Jonas Bilinkevicius
On a TRichEdit component a line can be selected by moving the cursor to the very
left of the line (the cursor will change to a right pointing arrow) and click the
mouse. It seems that the position of the cursor must be in the left most pixel
(i.e. only one pixel wide) so is often difficult to use. I would like to extend
this functionality to be available for the full width of the border, but I cannot
find out where this functionality is implemented. It is not available on the TMemo
component so I presume it must be implemented in the TCustomRichEdit component but
I can't find it. Can some please point me in the right direction or any suggestions
on how to extend this functionality to be applied to the full border width.
Answer:
First, the width of the area where the TRichEdit control allows you to select a
whole line is defined by the formatting rectangle of the control. The difference
between ClientRect left and formatting rectangle left coordinates normally is 1
pixel, but you can move the left side of the formatting rectangle and the area
where the cursor changes to a right pointing arrow and the RichEdit allows to
select a line would be wider. You can do it both in the form OnCreate event and in
the code of a TRichEdit descendant.
1 { ... }
2 var
3 XRect: TRect;
4 begin
5 SendMessage(MyRichEdit1.Handle, EM_GETRECT, 0, integer(@XRect));
6 XRect.Left := XRect.Left + 10; {set new left border for formatting rectangle}
7 SendMessage(MyRichEdit1.Handle, EM_SETRECT, 0, integer(@XRect));
8 { ... }
Regarding the selection of a whole line by clicking on the border, you should
respond to the WM_NCHITTEST message and return a HTCLIENT constant in the message
result in case the cursor is over border. The example below is a TRichEdit which
has a LeftIndent property. By selecting it, you can set the left side of the
formatting rectangle of the TRichEdit control.
9 { ... }
10 TMyRichEdit = class(TRichEdit)
11 protected
12 FLeftIndent: integer;
13 procedure WMNCHitTest(var message: TWMNCHitTest); message WM_NCHITTEST;
14 procedure SetEditRect;
15 procedure SetLeftIndent(AValue: integer);
16 public
17 constructor Create(AOwner: TComponent); override;
18 published
19 property LeftIndent: integer read FLeftIndent write SetLeftIndent;
20 end;
21
22 { ... }
23
24 constructor TMyRichEdit.Create(AOwner: TComponent);
25 begin
26 inherited Create(AOwner);
27 FLeftIndent := 0;
28 end;
29
30 procedure TMyRichEdit.WMNCHitTest(var message: TWMNCHitTest);
31 begin
32 inherited;
33 if (message.Result = HTBORDER) then
34 message.Result := HTCLIENT;
35 end;
36
37 procedure TMyRichEdit.SetEditRect;
38 var
39 XRect: TRect;
40 begin
41 SendMessage(Handle, EM_GETRECT, 0, integer(@XRect));
42 XRect.Left := FLeftIndent;
43 SendMessage(Handle, EM_SETRECT, 0, integer(@XRect));
44 end;
45
46 procedure TMyRichEdit.SetLeftIndent(AValue: integer);
47 begin
48 if FLeftIndent <> AValue then
49 begin
50 FLeftIndent := AValue;
51 SetEditRect;
52 end;
53 end;
|