Windows with Delphi Windows API (94) Windows Filesystem (41) Windows Forms (69) Windows Graphics (38)
Exchange Links About this site Links to us 
|
Highlighting HTML-Tags in TRichEdit
This article has not been rated yet. After reading, feel free to leave comments and rate it.
You need a quick and dirty syntax highlighting to display HTML?
Use the procedure HTMLSyntax() - provided below - as shown in the example at the bottom. To optimize the display, suppress updates by calling the TRichEdit's BeginUpdate() and EndUpdate. This code is too slow to be used in an OnChange() event handler. Another problem is that there is not much of error recovery if you have to handle faulty HTML. But it is suitable to display existing HTML files only.
If you need a real syntax highlighting editor for your FrontPage clone project, I recommend the excellent open source tool mwEdit. This project is asleep since approximately 2001 when it was considered mature and complete. I still have the source code because I use it in some of my own projects (SourceCoder for example). mwEdit is lightning fast and supports many many grammars (languages, e.g. Pascal, C, perl, SQL.. maybe 20 languages). Write me (ptiemann_2000@yahoo.com) if you have a problem finding it.
 | |  | | procedure HTMLSyntax(RichEdit: TRichEdit; TextCol, TagCol, DopCol: TColor);
var
i,
iDop : integer;
s : string;
Col : TColor;
isTag,
isDop: boolean;
begin
iDop := 0;
isDop := False;
isTag := False;
Col := TextCol;
RichEdit.SetFocus;
for i := 0 to Length(RichEdit.Text) do
begin
RichEdit.SelStart := i;
RichEdit.SelLength := 1;
s := RichEdit.SelText;
if (s='<') or (s=char(123)) then
isTag := True;
if isTag then
if (s=char(34)) then
if not isDop then
begin
iDop := 1;
isDop := True
end
else
isDop := False;
if isTag then
if isDop then
begin
if iDop<>1 then
Col := DopCol
end
else
Col := TagCol
else
Col := TextCol;
RichEdit.SelAttributes.Color := Col;
iDop := 0;
if (s='>') or (s=char(125)) then
isTag := False
end;
RichEdit.SelLength := 0
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
RichEdit1.Lines.BeginUpdate;
HTMLSyntax(RichEdit1, clBlue, clRed, clGreen);
RichEdit1.Lines.EndUpdate
end; | |  | |  |
Comments:
|