Professional Documents
Culture Documents
It's time to create a simple example in Delphi now. When you start Delphi, a default
project is created with one form. This default project automatically creates a blank
form, with its associated unit file and a project file, among others.
To get started, from the beginning, close anything that's open by choosing File |
Close All from the main menu.
Before you create your first Delphi project, you need to know what you want to
develop; a DLL, MDI application, SDI application a CLX application (for Linux) or
something else. To start without all the bells and whistles we'll create a standard
SDI Windows application. Simply point your mouse to File | New and select
Application. This creates a new project group with a single application in it.
The new project contains an empty form, a unit (associated with its form), and a
project file. As you develop and build your application, new files will be created and
added to the project. The project files are listed in the Project Manager window,
display it by selecting View | Project Manager from the main Delphi menu. With the
Project Manager, you can easily visualize how all your project files are related. If you
share files among different projects, using the Project Manager is recommended
because you can quickly and easily see the location of each file in the project.
Since this course is about Delphi for the Windows platform, we will be exploring
Delphi programming from that point of view. However, if you have Kylix and want to
join this course you are of course encouraged to do so. Even though, my intention at
this stage of this Course, is not to explain differences between CLX (Linux) and VCL
(Windows) development you should know that there are no reasons why you should
not join the course and just have in mind that when we talk about, let's say,
form1.DFM you think form1.XFM.
Hello Delphi
Now that we've created a project, we can begin work on our first application. This
first application will be pretty simple - we'll change the caption of the (main) form
once the application is executed. The change will be initiated from code - no user
interaction will be necessary.
To add the code that changes the caption of the form we need to *call* the Code
Editor window. If you have the Project Manager displayed on the screen, double click
the Form1. This will bring up the Form designer window to the front. Another way to
bring the Form1 to the front of the screen is to select Form1 from the Window menu.
Once Form1 is on top and active, double click it. This action has the following result:
the Code editor is positioned on the top of the screen and Delphi creates the skeleton
code for the new event handler.
Note: another way of achieving the same result is to activate Form1 in the Object
Inspector, select the Events tab and double click in the OnCreate column value.
As stated in the second chapter of this course, each form has a collection of events –
such as a mouse click, keypress, or component activation – for which you can specify
some additional behavior. In this case the event is called OnCreate. This event occurs
when the form is created.
The skeleton code looks like:
For the moment do not get bothered with the meaning of the text you see.
If everything goes well (it should) your application is executed and you see a blank
form on the screen. Note several things. First, the form is blank - there are no dots
that make up the grid you see when designing the form look. Second, there is a new
button on the Windows Task Bar - when you point to it you'll see that it has the
Project1 caption. Third, the caption of Delphi IDE is changed from "Delphi 6 - Project
1" to "Delphi 6 - Project 1 [Running]". And fourth, most important for us, the caption
of the form is Hello Delphi ! + *date and time of the execution*.
There is not much you can do with this window, you can move it resize it and finally
close it. Every time you (compile and) run this project a form caption will say Hello
Delphi with the date and time of the execution.
Ok, I know this is not a lot, but be patient - this is your first project - it is not
supposed to do something meaningful. If you want a little more, here goes another
simpe example.
Note 1: When you have saved the unit file, the corresponding form was saved as
MainUnit.dfm
Note 2: In the Code Editor window, Unit1 is now referred to as MainUnit.
Note 3: Since you have saved the project with the *new* name, if you run your
application now, the button on the Task Bar will say "HelloProject". Of course the
name of the application and the name of the project do not need to be the same,
later we will see how to change the name of a Delphi application.
Note, if you open up the HelloExample folder in the Windows Explorer, you should
find several files inside it. These are MainUnit.pas, MainUnit.dfm and several others.
The most important file inside this folder is the HelloProject.exe. This is your
applications executable file, if you double click it you'll execute it. If you want to
"install" your application on another machine this is the only file you need to copy.
Beside printed materials, Delphi holds a great deal of information in the Help system.
Even though you'll need to learn how to use it, it is really worth it - there are many
code examples to help you understand all the nuts and bolts of Object Pascal
programming. What's more, context-sensitive Help is available from nearly every
portion of the Code editor. To get context-sensitive Help from the Code editor window
simply place the cursor on the property, event, method, procedure or type for which
you want Help, then press F1.
Try it. Position the mouse cursor inside the word "Caption" in the Code Editor (the
word Caption you typed in the only example so far) and hit the F1 key.
Once you press the F1 key, a pop up window will ask you to specify more exactly
what you want to know. Here comes the hard part: how in the world you know what
topic to pick. The "problem" lies in the fact that, in Delphi, many components have
properties of the same name (and behavior). To get the help on Form Caption
property you need to pick TControl.Caption. Why TControl, when you are working
with Form not something called TControl? Well, for the moment this is hard to
explain, let's just say that Form derives from something called Control and that
Control has a Caption property. What you will find out is that in general, Caption is
used for text that appears as a window title.
But how will you know what to pick? There is a solution. Point to Object Inspector,
Properties page. Select the property you want to find out about and than press F1.
0. Learn about the Name property of the Form object. Note that the Name property
should tell you what the form does.
1. Explore the Object Inspector and try to figure what properties relate to the Form
positioning on the screen (Left, Top, Width, Height, ...) and what happens when you
alter them at design time.
2. Try to change the color of the Form from the Object Inspector (Color property)
3. Learn about the BorderIcons and BorderStyle properties and how they relate to
visual representation of the Form at run time.
4. Find what exactly DateTimeToStr is used for.
5. Be sure not to miss the next chapter!