Changing Caret Color
In most cases, the system caret is suffice for all the needs of an application.  However sometimes, for example, in a text editor, rather than displaying some status bar text indicating whether the text editor is in overstrike or insert mode, the most appropriate way (at least in my opinion) is to change the shape or the color of the caret.  It is almost next to impossible to do so in the Windows API as no functionality is provided.  A search through the internet proved fruitless.  In the end it is much easier to implement my own.

Implementation
Except for games, I find shapes other than the vertical or the underline bar used for text editing to be very distracting.  Hence the implementation of my caret is confined to rectangular shapes only.  A caret is nothing more than a colored shape blinking at a set rate.  To simulate the blinking, a timer is attached so that when its period is up, the shape can be redrawn accordingly.  Thus we can defined the following....

   type Carets is record
              Timer : OS.Timers.Timers;
               -- other relevant information goes here.
           end record;

There are 2 ways of handling the timer when its period is up.  One way is to handle the timer messages in the window that owns the caret.  This method is discarded since it burdens the user with unnecessay details.  The package should only allow the user to declare a caret object, set its attributes, place it in a desired place and let everthing else do its job automatically.  For this reason, a call back function solution is preferred.

   procedure TimerCallBack(hWnd                       : OS.Addresses;
                                              uMsg                        : OS.Unsigned;
                                              TimerID                   : OS.IDs;
                                              CurrentSystemTime : OS.Unsigned);

   pragma Convention(StdCall, TimerCallBack);

Carets.Set must be called first before a caret can be used as somewhere inside the body is a call to initialize the timer ie

   OS.Timers.Fire(Caret.Timer, TimerCallBack'Access);

The body of TimerCallBack should be responsible for painting the caret rectangle in the desired color.  Erasure is done by invalidating the painted area on the next cycle of the timer.  See the package Carets for details.

Using the package Carets
I firmly believe if somthing can be used easily, it will be used often. A piece of software is no exception.  To incorporate a caret into your application, just declare an object of the caret type, set its attributes and place it where where you want it to be.  All the blinking will be done automatically.  Assuming Caret has been declared somewhere in the application we can do something like this:

     Carets.Set(Owner, Caret, Size => (0, h), Color => OS.Colors.Light_Red);
      Carets.MoveTo(Caret, To_Location);
     --
     -- Owner is the window owning Caret
     -- caret size of default width with a height of h
     -- want a red blinking caret at To_Location

For a complete example see package ChatBoxes.

BACK