这对我非常有用,没有使用TWebBrowser。
但是从HTML到RichEdit。
希望有人会发现它很有用。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls;
type
TForm1 = class(TForm)
txtHTML: TMemo;
Button1: TButton;
rchHTML: TRichEdit;
procedure Button1Click(Sender: TObject);
procedure DisplayText(Tag: string; Buf:string);
procedure FontTags(Tag: string; Buf:string);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
Buf : string;
Bumf : string;
tag : string;
begin
rchHTML.text := '';
rchHTML.SelAttributes.style := [];
rchHTML.selattributes.color := clBlack;
rchHTML.SelAttributes.Name := 'MS Sans Serif';
rchHTML.SelAttributes.Size := 8;
Bumf := stringreplace(txtHTML.Text, #13#10, '', [rfReplaceAll]);
if pos('<', Bumf) = 0 then
begin
Bumf := stringreplace(Bumf, '<', '<', [rfReplaceAll]);
Bumf := stringreplace(Bumf, '>', '>', [rfReplaceAll]);
Bumf := stringreplace(Bumf, '&', '&', [rfReplaceAll]);
rchHTML.SelText := Bumf;
exit;
end;
Buf := copy(Bumf, 0, pos('<', Bumf) - 1);
Buf := stringreplace(Buf, '<', '<', [rfReplaceAll]);
Buf := stringreplace(Buf, '>', '>', [rfReplaceAll]);
Buf := stringreplace(Buf, '&', '&', [rfReplaceAll]);
rchHTML.SelText := Buf;
delete(Bumf, 1 ,pos('<', Bumf) - 1);
If pos('>', Bumf) = 0 then
begin
rchHTML.SelText := Bumf;
exit;
end;
While length(Bumf) > 0 do
begin
Tag := copy(Bumf, 2, pos('>', Bumf) - 2);
Buf := copy(Bumf, pos('>', Bumf) + 1, length(Bumf) - pos('>', Bumf));
Bumf := '';
while pos('<', tag) > 0 do
begin
rchhtml.SelText := '<' + copy(Tag, 1, pos('<', Tag) - 1);
Tag := copy(Tag, pos('<', Tag) + 1, length(Tag) - pos('<', Tag));
End;
If pos('<', Buf) > 0 then
begin
Bumf := copy(Buf, pos('<', Buf), (length(Buf) - pos('<', Buf)) + 1);
Buf := copy(Buf, 1, pos('<', Buf) - 1);
end;
DisplayText(Tag, Buf);
end;
end;
procedure TForm1.DisplayText(Tag: string; Buf:string);
begin
If Buf = '' then Buf := #12;
Buf := stringreplace(Buf, '<', '<', [rfReplaceAll]);
Buf := stringreplace(Buf, '>', '>', [rfReplaceAll]);
Buf := stringreplace(Buf, '&', '&', [rfReplaceAll]);
Tag := stringreplace(Tag, '<', '<', [rfReplaceAll]);
Tag := stringreplace(Tag, '>', '>', [rfReplaceAll]);
Tag := stringreplace(Tag, '&', '&', [rfReplaceAll]);
If copy(lowercase(Tag), 0, 5) = 'font ' then
begin
FontTags(Tag, Buf);
exit;
end;
if lowercase(Tag) = 'red' then
rchHTML.SelAttributes.Color := clRed
else if lowercase(Tag) = 'black' then
rchHTML.SelAttributes.Color := clBlack
else if lowercase(Tag) = 'blue' then
rchHTML.SelAttributes.Color := clBlue
else if lowercase(Tag) = 'cyan' then
rchHTML.SelAttributes.Color := clAqua
else if ((lowercase(Tag) = 'gray') or (lowercase(Tag) = 'grey')) then
rchHTML.SelAttributes.Color := clGray
else if lowercase(Tag) = 'green' then
rchHTML.SelAttributes.Color := clGreen
else if lowercase(Tag) = 'pink' then
rchHTML.SelAttributes.Color := clFuchsia
else if lowercase(Tag) = 'purple' then
rchHTML.SelAttributes.Color := clPurple
else if lowercase(Tag) = 'yellow' then
rchHTML.SelAttributes.Color := clYellow
else if lowercase(Tag) = 'b' then
rchHTML.SelAttributes.Style := rchHTML.SelAttributes.Style + [fsBold]
else if lowercase(Tag) = '/b' then
rchHTML.SelAttributes.Style := rchHTML.SelAttributes.Style - [fsBold]
else if lowercase(Tag) = 'i' then
rchHTML.SelAttributes.Style := rchHTML.SelAttributes.Style + [fsItalic]
else if lowercase(Tag) = '/i' then
rchHTML.SelAttributes.Style := rchHTML.SelAttributes.Style - [fsItalic]
else if lowercase(Tag) = 'u' then
rchHTML.SelAttributes.Style := rchHTML.SelAttributes.Style + [fsUnderline]
else if lowercase(Tag) = '/u' then
rchHTML.SelAttributes.Style := rchHTML.SelAttributes.Style - [fsUnderline]
else if lowercase(Tag) = 'br' then
Buf := #13#10 + Buf
else Buf := '<' + Tag + '>' + Buf;
rchHTML.SelText := Buf;
end;
procedure TForm1.FontTags(Tag: string; Buf:string);
var
a: integer;
tag2: String;
begin
Delete(Tag, 1, 5);
If pos('face', lowercase(Tag)) > 0 then
begin
a := pos('face', lowercase(Tag));
tag2 := copy(Tag, a, length(Tag) - (a - 1));
a := pos('"', Tag2) + 1;
Tag2 := copy(Tag2, a, length(Tag2) - (a - 1));
a := pos('"', Tag2) - 1;
Tag2 := copy(Tag2, 1, a);
rchHTML.SelAttributes.Name := Tag2;
end;
If pos('size', lowercase(Tag)) > 0 then
begin
a := pos('size', lowercase(Tag));
tag2 := copy(Tag, a, length(Tag) - (a - 1));
a := pos('"', Tag2) + 1;
Tag2 := copy(Tag2, a, length(Tag2) - (a - 1));
a := pos('"', Tag2) - 1;
Tag2 := copy(Tag2, 1, a);
rchHTML.SelAttributes.Size := strtoint(Tag2);
end;
rchHTML.seltext := Buf;
end;
end.
这个页面
找到它;-) - TLama