Author: Jonas Bilinkevicius
How can I set the positions for tabstops in general? I mean, they should be active
when a new TRichEdit is opened or when an open TRichEdit is filled with text via
LoadFromFile. I tried it with paragraph.tab but it doesn't do what I want.
Answer:
The property is somewhat screwed up, best use the API way directly: The positions
need to be specified in twips (1/1440 inch) for the EM_SETPARAFORMAT message. The
following method sets tabstops every 5 average character positions, based on the
current paragraphs font.
1
2 procedure TForm1.Button2Click(Sender: TObject);
3 const
4 tabs: array[0..5] of Integer = (5, 10, 15, 20, 25, 30);
5 teststring = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
6 var
7 pf: TParaFormat;
8 i: Integer;
9 charwidth: Integer;
10 begin
11 FillChar(pf, sizeof(pf), 0);
12 pf.cbSize := SizeOf(pf);
13 pf.dwmask := PFM_TABSTOPS;
14 pf.cTabCount := 6;
15 Canvas.Font.Assign(richedit1.SelAttributes);
16 {average charwidth in twips}
17 charwidth := (Canvas.TextWidth(teststring) * 1440) div (Screen.PixelsPerInch *
18 Length(teststring));
19 for i := 0 to High(tabs) do
20 pf.rgxTabs[i] := tabs[i] * charwidth;
21 if richedit1.perform(EM_SETPARAFORMAT, 0, Integer(@pf)) = 0 then
22 ShowMessage('Failed');
23 end;
Add the Richedit unit to your Uses clause. If you do this setting on an empty richedit control it will become the default for new text entered. If you read in formatted text you would have to do a selectAll, then set the tabstops,to make them effective for the loaded text.
|