Fast Text Highlighting

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]

 

Search

Search WWW

Search DelphiForFun.org

As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.

 

Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. 

     

We receive a few cents from each purchase.  Thanks

 


Support DFF - Donate

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Mensa Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

Mensa 365 Puzzlers  Calendar 2017

Mensa 365 Puzzlers Calendar 2018

(Hint: If you can wait, current year calendars are usually on sale in January.)

Contact

Feedback:  Send an e-mail with your comments about this program (or anything else).

Search DelphiForFun.org only

 

 

Problem Description

Here's a program to rapidly apply highlighting to selected  words in a TRichedit (Rich edit) control..

Background & Techniques

Applying syntax formatting to a sizable Richedit file is quite slow.  The technique used here is to intercept the WindowProc procedure used by TRichedit and let it paint the window which we then scan for words to by highlighted and rewrite them in the specified color.   Although my demo only highlights single words, more sophisticated highlighting, as for example performed by the Delphi IDE for strings, comments, numbers, etc. is possible.  The Basic code to do this was sent to me by Emile Tredoux and when someone else had the problem recently, I finally got around to converting the guts of the process to Delphi. 

I believe that there are enough comments in the code to make it usable to others.  The main constraint of the technique is that the highlighted text must fit into the space used for the text being replaced since TRichedit has already painted the window and we are just repainting selected portions.  The safest way to ensure this, used here, is to  use the TRichedit font for our Textout operations.  If the font has has fixed pitch as used by Delphi IDE, then highlighted  text font style could be set Bold or Italic as well.   

The code uses a number of Windows API calls and took a bit of fiddling to get the parameters defined correctly but it seems to be working fine for now.

Running/Exploring the Program 

bullet Download source
bullet Download  executable

Suggestions for Further Explorations

Lot's of room for variations of this technique. For example::

bullet Detecting and highlighting phrases.
bullet Highlighting color based on the word or phrase
bullet  Syntax highlighting as in the Delphi IDE by detecting comments, string constants, numbers, reserved words, etc.

 

 

Original Date: mmm dd, yyyy 

Modified: July 29, 2017

 

  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright 2000-2017, Gary Darby    All rights reserved.