Insert Unicode in RichText
Posted: Fri Apr 10, 2009 7:14 pm
Delphi 7, I have a problem where I need to replace some plain text in a Rich Text with unicode characters, the replacement is done correctly, the problem is when I save the Rich text to a File and when I reload it to the RichViewEdit, the replaced unicode is displayed as ???????, if I open the same file in Wordpad the unicode looks ok.
Code: Select all
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, RVScroll, RichView, RVStyle, StdCtrls, Buttons, RVEdit, ExtCtrls, RVTypes;
type
TForm1 = class(TForm)
RichViewEdit1: TRichViewEdit;
RVStyle1: TRVStyle;
btbtnLoadFile: TBitBtn;
ledFileName: TLabeledEdit;
btbtnInsertUnicode: TBitBtn;
btbtnSearchAndReplace: TBitBtn;
btbtnSaveFile: TBitBtn;
ledOutputFileName: TLabeledEdit;
btntReloadoutput: TBitBtn;
procedure btbtnLoadFileClick(Sender: TObject);
procedure btbtnInsertUnicodeClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure btbtnSearchAndReplaceClick(Sender: TObject);
procedure btbtnSaveFileClick(Sender: TObject);
procedure btntReloadoutputClick(Sender: TObject);
private
{ Private declarations }
function GetDummyUnicodeText: TRVUnicodeString;
procedure LoadFile(aFileName: String);
procedure CreateUniCodeStyle;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormShow(Sender: TObject);
begin
RichViewEdit1.RTFReadProperties.TextStyleMode := rvrsAddIfNeeded;
CreateUniCodeStyle;
LoadFile(ledFileName.Text);
end;
procedure TForm1.LoadFile(aFileName: String);
begin
RichViewEdit1.Clear;
RichViewEdit1.RTFReadProperties.TextStyleMode := rvrsAddIfNeeded;
RichViewEdit1.LoadRTF(aFileName);
RichViewEdit1.Format;
end;
procedure TForm1.btbtnLoadFileClick(Sender: TObject);
begin
LoadFile(ledFileName.Text);
end;
procedure TForm1.btbtnInsertUnicodeClick(Sender: TObject);
Var
sText : TRVUnicodeString;
begin
sText := GetDummyUnicodeText;
RichViewEdit1.InsertTextW(sText);
end;
function TForm1.GetDummyUnicodeText: TRVUnicodeString;
begin
//Thai Characters
Result := #$0E26;
Result := Result+#$0E27;
Result := Result+#$0E28;
Result := Result+#$0E29;
Result := Result+#$0E30;
Result := Result+#$0E32;
Result := Result+#$0E27;
Result := Result+#$0E28;
Result := Result+#$0E29;
Result := Result+#$0E30;
Result := Result+#$0E32;
Result := Result+#$0E27;
Result := Result+#$0E28;
Result := Result+#$0E29;
Result := Result+#$0E30;
Result := Result+#$0E32;
end;
procedure TForm1.btbtnSearchAndReplaceClick(Sender: TObject);
var
sTextToSearch: String;
sTextToInsert : TRVUnicodeString;
begin
sTextToSearch := '<Name>';
RichViewEdit1.SetSelectionBounds(0, RichViewEdit1.GetOffsBeforeItem(0), 0, RichViewEdit1.GetOffsBeforeItem(0));
if RichViewEdit1.SearchText(sTextToSearch, [rvseoWholeWord, rvseoDown]) then
begin
RichViewEdit1.ApplyTextStyle(0);
sTextToInsert := GetDummyUnicodeText;
RichViewEdit1.InsertTextW(sTextToInsert);
end;
end;
procedure TForm1.btbtnSaveFileClick(Sender: TObject);
begin
RichViewEdit1.SaveRTF(ledOutputFileName.Text, False);
end;
procedure TForm1.CreateUniCodeStyle;
var
aFontInfo: TFontInfo;
begin
RichViewEdit1.Clear;
aFontInfo := RVStyle1.TextStyles.Add;
aFontInfo.Unicode := True;
aFontInfo.StyleName := 'UniCode';
RVStyle1.DefUnicodeStyle := RVStyle1.TextStyles.Count-1;
end;
procedure TForm1.btntReloadoutputClick(Sender: TObject);
begin
LoadFile(ledOutputFileName.Text);
end;
end.