Create Textbox

<< Click to Display Table of Contents >>

Navigation:  Programming > WPTools Premium > Text boxes >

Create Textbox

To create a box use code like this:

 

procedure TForm1.Button1Click(Sender: TObject);

var obj : TWPTextObj;  

    block : TWPRTFDataBlock;

begin

obj := WPRichText1.TextObjects.InsertTextBox(1000, 1000, block);

obj.PositionMode := wpotPage;

if block<>nil then

   block.RtfText.AsString := 'Hello World';

end;

 

Alternatively the position mode can also be wpotPar. The optional reference to the TWPRTFDataBlock makes it possible to create text right after the creation.

 

block.RtfText.AsString := 'Hello World';

 

Text boxes are implemented as a special TWPObject class, TWPORTFTextBox from unit WPOBJ_TextBox. As mentioned in the manual the TWPObject instances are referenced by an instance of the TWPTextObj class which is hosted by the TParagraph which owns it. The TWPORTFTextBox does not store the text, that is a data block in the data collection which owns the object and the host paragraph.

So actually it would have been possible to paint the boxes without implementing the TWPORTFTextBox class too.

But the TWPObject concept is very useful since it offers a number of important public and internal procedures, last but not least the procedure WriteRTFData which is used when the text is written. This concept makes sure the logic is abstracted enough to make modifications easy. So you can implement modifications in the quite easy to understand unit WPOBJ_TextBox, instead to deal with the RTF reader or writer or even the RTF engine with its thousands of code lines.  

You can also modify the paint routine to paint a background pattern.

 

 

If you need to create the text box with low level code in a certain paragraph "par" to avoid changing the cursor position you can use:

 

textobj := WPRichText1.TextObjects.InsertTextBox(3000,1000,rtfdata, par);