1. Technology

Add an Easter Egg to your Delphi application


In techmythology, Easter Eggs aren't brightly colored protochickens. Easter eggs have always been the opportunity for developers to demonstrate pride in the products that they have been developing for months or even years.

Typically, Easter eggs are used to display the credits for the development team (to let the users of their software find out who they are) or to display a humorous message. To see an easter egg, the user needs to know a special procedure or sequence of keystrokes; click in the proper place or edit the right file.

In other words they're implemented as undocumented bits of code that come hidden inside operating systems, applications, Web browsers, games, and other programs.

Easter Eggs in the Delphi IDE

Each version of the Delphi IDE holds a hidden message that highlights the development staff that produced that version of the compiler. In most cases, they are credit lists for the developers of the product, but sometimes they are not.

To find what kind of eggs can be hunted down in Delphi, check out Borland Easter Eggs.

Implementing an Easter Egg

One of the practical uses of Easter eggs that I'm aware of is using Easter eggs as an entry point to special configuration options that the developer doesn't want users to mess around with. Of course this is not the kind of Easter egg we are interested in right now. Coding an Easter egg like that one would be no fun at all.

We'll make an Easter egg that displays a hidden (fun) message when the user presses a certain key combination. The same code can be used to, for example, show a simple scrolling text, start a full-blown multimedia presentation or even start a small game users can play.

To add an Easter egg to your code do the following:

  1. Select which form will be used to activate the Egg. If you have only one form in your project - the main form is the one you'll use. Let's say the name is Form1.
  2. Declare an integer variable (name: icnt) in the private part of the form declaration.
       icnt : integer;
  3. Add two form-level constants (in the interface section) :
       sEgg = 'ADPRULEZ';
       iEggLen = Length(sEgg) ;
  4. Add the next code to the OnCreate event handler for the form:
    procedure TForm1.FormCreate(Sender: TObject) ;
      KeyPreview := True;
  5. Our Easter Egg is activated when the user types ADPRULEZ whilst holding the CTRL key. Let the OnKeyDown for the Form look like:
    procedure TForm1.FormKeyDown(
      Sender: TObject;
      var Key: Word;
      Shift: TShiftState) ;
      if ssCtrl in Shift then
       if Key = Ord(sEGG[icnt]) then
        if icnt = iEggLen then
          ShowMessage('About Delphi Programming Easter Egg!') ;
          icnt := 1;
         icnt := icnt + 1;

        if Key = Ord(sEGG[1]) then icnt := 2 else icnt := 1;
How does it work?
The hidden word that activates the Egg is held in the sEgg constant and is ADPRULEZ. The icnt integer variable is used to count keystrokes when "hunting" for egg. In the OnCreate event handler the KeyPreview property for the form is set to True. This assures that the Form receives keyboard events before the active control. The main code is in the OnKeyPress even handler. We first check whether the Ctrl key is down. If so, we check if the last keystroke in the sequence was the proper key. If the 'hidden' combination is typed the message box simply displays: 'About Delphi Programming Easter Egg!'

Note: those cheat codes like IDKFA you use when playing DOOM clones are just simple Easter eggs.

The Secret Spot Egg

If you don't want to mess around with typing secret codes, you might think of placing some hidden hot-spot area somewhere on a form. Suppose you have a Splash Screen and a small secret place on a form that calls some code when clicked. The code would go in the Form's OnMouseDown event handler. Note that there should be no visible components placed over the secret spot!

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer) ;
var SecretSpot : TPoint;
   SecretSpot.x := 1; {little under and little to the right}
   SecretSpot.y := 1; {from the top left corner of a form}

   if (X=SecretSpot.x) and (Y=SecretSpot.y) then ShowMessage('Secret place!')
Easter eggs are just fluff, but people love them. Add some Easter eggs to your Delphi application and let the users hunt them...

©2014 About.com. All rights reserved.