Tuesday, 9 April 2013

How to correctly free a thread

There are 2 ways to free a thread, below are both, but my favourite way is to explicitly free the thread as I may want to reference something after the thread is terminated, but before it is freed.

Explicitly free the thread 
1) Set the 'FreeOnTermiate' property to false, this can be done when your thread class is created. 
2) In the execute procedure check for the terminate property e.g.

while not Terminated do
     // execute code

3) When you want to free the thread do something like:


Easier way to free the thread
1) Set the 'FreeOnTermiate' property to true.
2) When you want to free the thread just call:


Wednesday, 3 April 2013

How to use an animated GIF in Delphi

In the latest versions of Delphi you can easily use animated GIFs, here are the steps to add one to a form.

  1. Add a standard TImage component.
  2. In the picture property select the GIF file using the ellipsis.
  3. The TGifImage will require GIFImg adding to the uses clause.
  4. In code to set the animation going do the following:
(Image1.Picture.Graphic as TGIFImage).Animate := True;

Tuesday, 2 April 2013

Retrieving a system color

I had a problem recently and needed to set a font colour depending what was set as a system colour, in Delphi this ended up being very easy. To get say the highlight colour just do the following:


This returns the colour as a TColor, I then used this value in a previous function to determine whether is was light or dark and then set the font colour accordingly. This will require 'Windows' and 'Graphics' in the uses clause. The 'color_highlight' is a constant, go to where it is declared in the windows unit to see the other options.