Author: Jonas Bilinkevicius
How to set tab stops in a TMemo
Answer:
Solve 1:
To change the tab stops for a multiline edit control (i.e. a TMemo), send the
EM_SetTabStops message to the component. The Tabs array indicates where the tab
stops will be located. Since the WParam parameter to SendMessage is 1, then all tab
stops will be set to the value passed in the Tabs array. Remember to set the
WantTabs property of TMemo to True to enable the tabs.
1 procedure TForm1.FormCreate(Sender: TObject);
2 const
3 TabInc: LongInt = 10;
4 begin
5 SendMessage(Memo1.Handle, EM_SetTabStops, 1, Longint(@TabInc));
6 end;
Solve 2:
For a memo you use the EM_SETTABSTOPS message. Setting decimal tab stops in a memo
control;
7
8 procedure TScratchMain.SpeedButton2Click(Sender: TObject);
9 var
10 tabs: array[0..2] of Integer;
11 begin
12 {set first tabstop at 12, second at 24, third at 44 character position, using the
13 average width as base, converted to dialog units.4 dialog units make
14 one average char width.}
15 tabs[0] := 12 * 4;
16 tabs[1] := 24 * 4;
17 tabs[2] := 44 * 4;
18 Memo1.Clear;
19 Memo1.Lines.Add('01234567890123456789012345678901234567890123456789');
20 Memo1.Lines.Add('Start'#9'One'#9'Two'#9'Three');
21 Memo1.Perform(EM_SETTABSTOPS, 3, LongInt(@tabs));
22 Memo1.Refresh;
23 end;
Note that the message expects the position in an arcane unit called "dialog unit", 4 of which should theoretically equal the average character width of the memos font. But using div 4 does not give the correct positioning, while using div 2 does. Don't ask me why, dialog units are really only sensible in dialogs (which are based on a dialog resource) and are relative to the font used for the dialog itself, not the controls on it.
|