IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
Delphi
Search Delphi Group:

Advanced
-Collapse +Expand Delphi To/From
To/FromCODEGuides
-Collapse +Expand Delphi Store
PRESTWOODSTORE

Prestwood eMagazine

May Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingDelphi for W...Language Basics   Print This     
  From the November 2009 Issue of Prestwood eMag
 
Delphi Language Basics:
A 10 Minute Delphi for Win32 Quick Start
 
Posted 11 years ago on 10/6/2008 and updated 12/28/2008
Take Away: Create a classic "Hello, World" Windows native code application using Delphi. This tutorial is based on Borland Developer Suite 2006 but you can use any version of Delphi you wish.

KB101214



Got 10 minutes? Want to get started with Delphi for Win32?

This article is part of our series of 10 Minute Quick Starts. Each quick start is step by step, assumes you know very little about the subject, and takes about 10 minutes. You can use them to scratch the service of areas you want to learn and as a quick review when returning to something after a long absence.

Delphi for Win32

In this tutorial, you will create a classic "Hello, World" windows application. Unlike a DotNet application, a Delphi Win32 application is a true native code application. Delphi native code applications allow you to build the fastest Windows applications available today. With the upcoming Delphi for Win64, you'll be able to upgrade your application to take advantage of 64-bit processors. The goal of this tutorial is to get your feet wet with Object Pascal syntax and introduce the Delphi development environment.

Setup

To get started, you have to have Delphi installed. This article is based on Borland Developer Suite 2006 but you can use any version of Delphi you wish (there may be slight variations in menu options but we try to keep our 10 Minute Quick Starts as generic as possible).

If you don't have Delphi yet, you can download the trial edition for free:

And here, we, go...

Part One: Do something!

  1. Start a new project. Open Delphi and click File | New | VCL Forms Application. The 3 primary files created are the project file (.DPR extension), the unit code file (.PAS extension), and a binary form design file (.DFM extension).
     
    Note - VCL Forms. Good development environments offer you pre-written routines (a library of code). In Delphi, the library is called the Visual Component Library (VCL) and it's quite famous for making complex tasks easier than other code libraries.
     
  2. Save the project into a new folder called "Hello". Now let's save the project into a folder that will contain all of the source files for this project. Click File | Save All.

    Save the unit.
    Create a folder named "Hello" and save this unit as HelloFormUnit.pas. This will create both the unit source code (HelloFormUnit.PAS) and the binary form design file (HelloFormUnit.DFM). The unit file in this case is a the source code for our first form in our Windows application. 

     
    Save the project file as "Hello". The name of the .DPR file will be the name of the EXE. So, in this case, we want our executable file to be named Hello.exe so when we save the project

     
    Here are the initial files created for a VCL Forms application by Delphi 2006:

     
    After you compile, the file list grows a bit. In addition to the .EXE (same name as .DPR), a .RES resources file is created for the .EXE and a compiled version of the form is created (.DCU file). Although all these files are created, you only need to distribute the .EXE file.

      
  3. Place 3 buttons on the form. From the Tool Palette, place 3 TButton controls on the form. You can double click the TButton control on the Tool Palette or click it and click a location on the form.

    Note - All the classes in the VCL start with "T" for Type. The base class in the VCL is TObject and all classes descend from it. TObject contains very little code but does have fundamental code used by all classes including the code that is required to create, maintain, and destroy an object instantiated from a class. Since TObject is an abstract class, it is rarely created directly.
     
    Delphi TForm with 3 TButtons
     
  4. Add code to button 1. Double click Button 1 and add the following code:

    ShowMessage('Hello, World!');

    Note - Literals in Object Pascal are single quoted (apostrophe). Also, each line must end with a semi-colon except the last line within a code block. Although the semi-colon is optional on the last line of your code within a code block, it's a good habit to always include it anyway.
  5.  
  6. Switching from Code To Form and back. The form has the same name as your unit.pas file but with a DFM extenstion. To view the form, expand the unit in the Project Manager and double click the DFM file.
     

     
  7. Compile your code. You are now ready to compile your code into a Windows application. Click Project| Build Project1 where Project1 is the name of your application. If you have any syntax errors or compiler warnings, this is when Delphi will let you know and you can fix them.
     
  8. Run-it! Now let's test your very first native code Windows program. You can run it within Delphi (called debugging) or you can run the EXE outside of Delphi. To run it, either click Run | Run, the green trianlge toolbar run icon, or press F9. Click Button 1 to show the Hello, World dialog. Click OK then close the form to abort the debugging session.
     

     
     
    You can also run the application outside of Delphi. The compiled EXE is named the same name as the DPR file except with an EXE extension. So, if you saved this project as Project1.dpr then the name of the EXE is Project1.EXE. You can even distribute your EXE to any Windows computer.
     
  9. Add code to Button 2. The ShowMessage function used above wraps up the Win32 MessageBox function in a simple to use command. Add the following code to Button 2's click event:

     MessageBox(0, 'Hello, World!',  'Press OK', MB_OK);

  10. Add code to Button 3. So far we've been using literals for the text in our dialog box. This time let's declare two variables, concatenate them together and use the variable with the MessageBox member. For good measure, we'll make use of the "//" single line comment and the "{}" multi-line comment.
     
    procedure TForm1.Button3Click(Sender: TObject);
    var
      RetVal      : Integer;
      TitleStr    : PAnsiChar;
      MessageStr  : PAnsiChar;
    begin
      //Assign variables.
      TitleStr := 'Press OK to continue.';
      MessageStr := 'Proceed with action?';



      RetVal := MessageBox(0, MessageStr,  TitleStr, MB_OKCANCEL);


      {
      Check return value
      then display appropriate display box."
      }
      If RetVal = MB_OKCANCEL Then
        ShowMessage('Proceeding...')
      Else
        ShowMessage('Cancelled');
    end;

  11. Test-It!

Part Two: The Help System

The Visual Component Library (VCL). Good development environments provide a library of prewritten code for your use. For example, Borland Delphi provided it's Visual Component Library (VCL). The better you know the VCL, the easier coding in Delphi will be. Since the VCL is a class library that fully exploits and abides by the tenets of object oriented coding, the more you study general Object Orientation (OO) the better.

The Help System. Each development environment has it's way of documenting it's own code library, event model, and tool usage. Each time you use something new in Delphi, breifly read about it in the online help system. The ShowMessage routine is part of the VCL and the MessageBox function is part of the Win32 API. The Delphi help system documents both the VCL and the Win32 API and you'll have to get used to reading the wealth of information it contains.
 

Click on the MessageBox command we typed above to put the cursor within it. Then press F1 to launch the help system. Notice under reference the name space that contains MessageBox is listed including the namespace path.
 
The following screen clip is Delphi 2006:
 

 

What now?

Now that you've gotten your feet wet with Delphi, you can start learning how to do specific coding tasks, learn the intricies of the language, and the ins and outs of the development environment. A good next step is to browse the varioius menu options and see what they do. Select various menu options, when in the feature, press F1 for a quick overview.

If you want a bit more syntax, click on the related post below.

That's-It! I hope it was helpfull.

More Info

Article:  A 10 Minute Your First Delphi Class Quick Start

Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1191A1
Enter key:
Article Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #101214 Counter
11258
Since 10/6/2008
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2019 Prestwood IT Solutions.   [Security & Privacy]