2010. augusztus 16., hétfő

How to set tab stops in a TMemo


How to set tab stops in a TMemo


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);
  TabInc: LongInt = 10;
  SendMessage(Memo1.Handle, EM_SetTabStops, 1, Longint(@TabInc));

Solve 2:

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

procedure TScratchMain.SpeedButton2Click(Sender: TObject);
  tabs: array[0..2] of Integer;
  {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.Perform(EM_SETTABSTOPS, 3, LongInt(@tabs));

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