Friday, November 20, 2009

Delphi project explained

This is another post for Delphi beginners.
In this post I will try to explain what a Delphi project contains and how a GUI application is created.
Start Delphi > File -> New -> Application|VCL Forms Application press SHIFT+CTRL+S(this shortcut saves all files opened in Delphi IDE) save everything to a folder without naming them.
At this point you should have 4 or 5 files(I'm not sure from what version of Delphi they introduced ProjectName.dproj(xml) -- you can delete the .dproj file it will be automatically recreated) file, and here are the names of files
- Project1.dpr -- this is the program with which you should be familiar i.e.
program delphiOnSteroids;
     WriteLn('Hi dudes!');
- Project1.res -- this file keeps the application's icon, it can be deleted and will be recreated after IDE shows you and error file which says that the resource file cannot be found and it will be recreated
- Unit1.dfm -- files with *.dfm extension contains information about objects on the form and form information, i.e. control width, height, color, font, etc.
- Unit1.pas -- this file contains code that defines and implements a new class of form with ease.

Did you know that when you create a new form and add a few controls on the form, modify them, add events, etc. you basically create a new class without even noticing?
Here is unit1.pas after creating a new project and saving it without modifying anything but adding two variables in order to explain the private and public keywords
unit Unit1;


  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  TForm1 = class(TForm)
         ImAPrivateString: String;
// ImAPrivateString can be viewed only by descendants
// of this class or by class it self
    { Private declarations }
        ImAPublicInteger: Integer;
//ImAPublicInteger can be accessed publicly by
// typing the name of the identifier which is of type this class.identifier
// for instance Form1.ImAPublicInteger := 2009;
    { Public declarations }

  Form1: TForm1; // this is a unit variable which can be accessed from
// other units by adding this unit to it's uses clause
// i.e. uses unit1;
// this variable means nothing without this line of code in the *.dpr file
// Application.CreateForm(TForm1, Form1);
// the above procedure creates a new form of type TForm1 and assigns
// it to Form1 variable which is the name of the form in unit1


{$R *.dfm} // this is a compiler directive which tells the compiler
// something like, *whistle* hey compiler the file with same name as 
// mine but with *.dfm extension is my form's properties, you know
// what to do with them!

Let's analyze the *.dpr file
program Project1; // define the name of the program
// MUST be same as filename.dpr(without ".dpr) in our case Project1.dpr

  Forms, // need this in order to create forms
// as you can see here you can also declare a relative path
// for instance UnitX in '..\DistancedUnit.pas'
// you even have a comment to let you know that it's about Form1
  Unit1 in 'Unit1.pas' {Form1};

// the ProjectName.res file that I was talking about
// compiler adds this to the executable
{$R *.res}

// Delphi developers are saying this:
// This used to call InitProc, which was only used for COM and CORBA.
// Neither is used with the .NET version 
// about Application.Initialize;
// this is something new introduced since Delphi 2007 if I'm 
// not mistaken
  Application.MainFormOnTaskbar := True;
// creates and assigns a instance of type TForm to the unit
// variable Form1 in Unit1.pas
  Application.CreateForm(TForm1, Form1);
// this will start your application and shows Form1
// if you write somewhere Application.MainForm := SomeFormVariable
// then when that form closes your application terminates!
About the *.dfm files, it's actually plain text, images/resources of components are stored as hex strings, you can call a *.dfm a better storage than the *.ini file, when you call Application.CreateForm(bla bla) the application reads the *.dfm stored in the *.exe as resource and creates the controls as defined in the *.dfm with lightning speed using RTTI(Run Time Type Information).


Blogroll(General programming and Delphi feeds)