You didn't say which language, so here's Pascal/Delphi which should be very simple to follow and adapt to ...
answers.yahoo.com