TRichView - Replace Format (Font,Style,Color etc.)
Posted: Mon Mar 14, 2022 6:47 am
Hello,
if we insert one RTF/DOCX into another - the inserted rtf should inherit the font, size, and style of the surrounding document. For this I have implemented this piece of code.
- First we are loading the piece which will be inserted into a TRichtViewEditor instance.
- instead of useing global variables for the FParent* I would prefer it to make a class with the required values and pass this through
userdata in ApplyStyleConversion(...) but currently userdata is just an Integer - so it will work only for Win32 to cast the class into Integer,
but for Win64 -it won't work that way - are there any plans to change such parameters to NativeInt in the near future?
if we insert one RTF/DOCX into another - the inserted rtf should inherit the font, size, and style of the surrounding document. For this I have implemented this piece of code.
- First we are loading the piece which will be inserted into a TRichtViewEditor instance.
- instead of useing global variables for the FParent* I would prefer it to make a class with the required values and pass this through
userdata in ApplyStyleConversion(...) but currently userdata is just an Integer - so it will work only for Win32 to cast the class into Integer,
but for Win64 -it won't work that way - are there any plans to change such parameters to NativeInt in the near future?
Code: Select all
procedure TForm1.Button1Click(Sender: TObject);
begin
// Fake surrounding Font of the target RTF
FParentFontName := 'Segoe UI';
FParentFontColor := clBlack;
FParentFontBGColor := clNone;
FParentFontSize := 14;
FParentFontStyle := [];
rve.OnStyleConversion := ReplaceTextStyleConversion;
try
rve.SelectAll;
rve.ApplyStyleConversion(0{only int32});
rve.RVData.DeleteUnusedStyles(true, true, true);
finally
rve.OnStyleConversion := nil;
end;
rve.SaveRTF('d:\insertmemo.rtf',false);
end;
procedure TForm1.ReplaceTextStyleConversion(Sender: TCustomRichViewEdit; StyleNo, UserData: Integer; AppliedToText: Boolean;
var NewStyleNo: Integer);
var
FontInfo: TFontInfo;
begin
FontInfo := TFontInfo.Create(nil);
try
// copy fontinfo from the this..
FontInfo.Assign(rvs.TextStyles[StyleNo]);
// replace font if its still the memo default font
if SameText(FontInfo.FontName, DefMemoFontName) then
FontInfo.FontName := FParentFontName;
FontInfo.Size := MulDiv( FParentFontSize, FontInfo.Size, DefMemoFontSize );
if FontInfo.Style = DefMemoFontStyle then
FontInfo.Style := FParentFontStyle;
if FontInfo.Color = DefMemoFontColor then
FontInfo.Color := FParentFontColor;
if FontInfo.BackColor = DefMemoFontBGColor then
FontInfo.BackColor := FParentFontBGColor;
NewStyleNo := rvs.FindTextStyle(FontInfo);
finally
FontInfo.Free;
end;
end;