WPRichTextLabel programmicly change colors

  • Hello i need some help with changing all the colors on a WPRichTextLabel.

    Here is an exsample of a code i used to try and nothing happens,

    Code
    procedure TForm1.Button1Click(Sender: TObject);
    begin
     WPRichTextLabel1.Color := clRed;
     WPRichTextLabel1.Font.Color := clBlue;
     WPRichTextLabel1.PaperColor := clGreen;
    end;

    If someone can help would be greatly appreciated, I have tried to find it in the forum and cannot find any refrence to it nor can i find it in the demo's supplyed with WPTools 5
    Thank you.

    • Offizieller Beitrag

    A TWPRichTextLabel does not have the CurrAttr object the TWPRichText (=memo) has. So the 'Cursor' must be used directly.

    Please try this:

    Code
    // Only PaperColor can be used!
     WPRichTextLabel1.Transparent := false;
     WPRichTextLabel1.PaperColor := clYellow;
     // Since a label does not have a focus this properties must be set explizitely
     WPRichTextLabel1.Cursor.RTFEngine := WPRichTextLabel1.Memo;
     WPRichTextLabel1.Cursor.RTFData := WPRichTextLabel1.Body;
     // Select all and use SelectedTextAttr
     WPRichTextLabel1.Cursor.SetSelPosLen(0, MaxInt); // = SelectAll
     WPRichTextLabel1.Cursor.SelectedTextAttr.SetColor(clBlue);
     WPRichTextLabel1.Invalidate;

    Note: The LayoutMode must be 'normal'. If you need multipage view or full layout use the TWPRichText and make it readonlay (doubleclick on TWPRichText object shows expert)

    Regards,
    Julian Ziersch

  • Sorry am back again the code you just gave me works great but i now have another proble in reversing it as i need to call the color of the label back so i need to be able to see witch label has what color in the PageColor and in the font color so i can save them to an ini file and recal then at runtime thanks

    this is the code i have tryed

    procedure TF1.Button1Click(Sender: TObject);
    var FColor,BGColor : String;
    begin
    FBGColor := ColorToString(WPRichTextLabel1.PaperColor);
    FColor := ColorToString(WPRichTextLabel1.Font.Color);
    end;

    but no go i am imagining it will be something similer to the previous code you have giving me to set the colors but i am un sure on how to go about doing it any help would be greatly apreciated thanks

    this is the previous code given

    // Only PaperColor can be used!
    WPRichTextLabel1.Transparent := False;
    WPRichTextLabel1.PaperColor := clYellow;
    // Since a label does not have a focus this properties must be set explizitely
    WPRichTextLabel1.Cursor.RTFEngine := WPRichTextLabel1.Memo;
    WPRichTextLabel1.Cursor.RTFData := WPRichTextLabel1.Body;
    // Select all and use SelectedTextAttr
    WPRichTextLabel1.Cursor.SetSelPosLen(0, MaxInt); // = SelectAll
    WPRichTextLabel1.Cursor.SelectedTextAttr.SetColor(clBlue);
    WPRichTextLabel1.Invalidate;

  • sorry i am not following

    // Only PaperColor can be used!
    WPRichTextLabel1.Transparent := False;
    WPRichTextLabel1.PaperColor := clYellow;
    // Since a label does not have a focus this properties must be set explizitely
    WPRichTextLabel1.Cursor.RTFEngine := WPRichTextLabel1.Memo;
    WPRichTextLabel1.Cursor.RTFData := WPRichTextLabel1.Body;
    // Select all and use SelectedTextAttr
    WPRichTextLabel1.Cursor.SetSelPosLen(0, MaxInt); // = SelectAll
    WPRichTextLabel1.Cursor.SelectedTextAttr.SetColor(clBlue);
    WPRichTextLabel1.Invalidate;


    that code there will SET the color i chose and works great but the problem i am haveing is that i need to find the color(s) on all labels on a form then save each one. The code i have is

    procedure TF2.ApplyColorsClick(Sender: TObject);
    var i : Integer;
    begin
    Try
    myINI := TINIFile.Create(ExtractFilePath(Application.ExeName)+ 'TempTheme.ini');
    myINI.WriteString('Theme','Title',CurrentTheme);
    with F1 do begin // Form one
    for i:=0 to ComponentCount-1 do begin
    if Components[i] is TWPRichTextLabel then begin
    myINI.WriteString('Labels'+IntToStr(i),'Color',ColorToString(TWPRichTextLabel(Components[i]).PaperColor));
    myINI.WriteString('Labels'+IntToStr(i),'Name',TWPRichTextLabel(Components[i]).Name);
    myINI.WriteString('Labels'+IntToStr(i),'Font',ColorToString(TWPRichTextLabel(Components[i]).Font.Color));
    end;
    end;
    end;
    end;
    end;


    and this does not return the colors i have prevously defined by colorpicker to set the colors, i run the code above after the user has set all the colors they would like to change so it will save haveing to apply the code each time they change a color and confems that they want that look before saveing the colors so im not sure what i am doing wrong or is there another way of going about doing this? The PaperColor saves and loads fine its just not the font colors,

    Thanks Julian

    Scott.

    • Offizieller Beitrag

    Hi,

    It would be possible to read PaperColor but for the text colors you will need to save the last applied color somewhere else. The easiest way is inherit a new component from TWPRichTextLabel and add the required properties to that. Then you can save those.

    Font.Color cannot be used. It has that Font property because if inherits from a TGraphic control, but that is not usable.

    And instead of using INI files you should check out the provided XML classes, the TWPXMLTree is very nice to save and load properties in a nested way (form1, form2 ...)

    Julian