2010. augusztus 16., hétfő

How to set tab stops in a TMemo


Problem/Question/Abstract:

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.

procedure TForm1.FormCreate(Sender: TObject);
const
  TabInc: LongInt = 10;
begin
  SendMessage(Memo1.Handle, EM_SetTabStops, 1, Longint(@TabInc));
end;


Solve 2:

For a memo you use the EM_SETTABSTOPS message. Setting decimal tab stops in a memo control;

procedure TScratchMain.SpeedButton2Click(Sender: TObject);
var
  tabs: array[0..2] of Integer;
begin
  {set first tabstop at 12, second at 24, third at 44 character position, using the
  average width as base, converted to dialog units.4 dialog units make
        one average char width.}
  tabs[0] := 12 * 4;
  tabs[1] := 24 * 4;
  tabs[2] := 44 * 4;
  Memo1.Clear;
  Memo1.Lines.Add('01234567890123456789012345678901234567890123456789');
  Memo1.Lines.Add('Start'#9'One'#9'Two'#9'Three');
  Memo1.Perform(EM_SETTABSTOPS, 3, LongInt(@tabs));
  Memo1.Refresh;
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.

Nincsenek megjegyzések:

Megjegyzés küldése