DelphiFAQ Home Search:
General :: Windows :: Programming :: Windows with Delphi :: Windows Graphics
Graphics programming under Windows with Delphi.


This list is sorted by recent document popularity (not total page views).
New documents will first appear at the bottom.

Featured Article

Highlighting HTML-Tags in TRichEdit

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 ( if you have a problem finding it.

procedure HTMLSyntax(RichEdit: TRichEdit; TextCol, TagCol, DopCol: TColor);
   iDop : integer;
   s    : string;
   Col  : TColor;
   isDop: boolean;
 begin { HTMLSyntax }
   iDop := 0; 
   isDop := False; 
   isTag := False; 
   Col := TextCol; 
   for i := 0 to Length(RichEdit.Text) do 
     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 
           iDop := 1; 
           isDop := True
         end { not isDop } 
           isDop := False; 
     if isTag then 
       if isDop then 
         if iDop<>1 then 
           Col := DopCol
       end { isDop } 
         Col := TagCol 
       Col := TextCol; 
     RichEdit.SelAttributes.Color := Col; 
     iDop := 0; 
     if (s='>') or (s=char(125)) then 
       isTag := False
   end; { for i } 
   RichEdit.SelLength := 0
 end; { HTMLSyntax } 
 procedure TForm1.Button1Click(Sender: TObject); 
 begin { TForm1.Button1Click } 
   HTMLSyntax(RichEdit1, clBlue, clRed, clGreen); 
 end; { TForm1.Button1Click }

Generated 0:02:16 on Sep 25, 2018