CPChar, CPMoveNext, etc.

<< Click to Display Table of Contents >>

Navigation:  Programming > Move cursor / select text > Move the Cursor >

CPChar, CPMoveNext, etc.

 

WPTools makes it easy for you to loop through all the characters to check for attributes, change attributes or extract or modify text. (Also read about the cursor class TWPRTFDataCursor)

 

Example: Change color of bold text (from Finder demo)

 

 WPRichText1.AttrHelper.Clear;

 WPRichText1.AttrHelper.SetStyles([afsBold]);

 WPRichText1.CPPosition := 0;

repeat

  if WPRichText1.CurrentCharAttr.Contains(

     WPRichText1.AttrHelper) then

     WPRichText1.CurrentCharAttr.SetColor(clRed);

until not WPRichText1.CPMoveNext;

 WPRichText1.Refresh;

 

 

 

Example: Assign the bold attribute to the selected text

 

var i : Integer;

begin

  i := WPRichText1.SelLength;

  WPRichText1.CPPosition := WPRichText1.SelStart;

  while i>0 do

  begin

    WPRichText1.CPAttr.BeginUpdate;

    WPRichText1.CPAttr.IncludeStyle(afsBold);

    // other changes ...

    WPRichText1.CPAttr.EndUpdate;

    if not WPRichText1.CPMoveNext then break;

    Dec(i);

  end;

end;

 

Instead of this complicated code you can also use

 CurrAttr.AddStyle([afsBold])

but the above let you decide for each character which style has to be set.

 

 

Please note when you use WPTools 4 before:

CPChar and CPAttr cannot be used as pointers anymore.

 

Instead CPChar is a property and CPAttr is an object with properties to manipulate the corresponding TAttr value.