Delphi Programming/First application

From Wikibooks, open books for an open world
Jump to: navigation, search

Consoles and GUI[edit]

A dialog coded in Delphi. It's a GUI.

At first we want to begin with a console application, but what's the difference between consoles and GUIs? GUI is the short form of Graphical User Interface which means that the user has one or more windows with so called components — e. g. edit fields, buttons, check boxes, scroll bars, a toolbar, a menu, ... — where he can edit something or do something with them to call an event, linked with a procedure (in other programming languages called functions — but there's a difference between functions and procedures in Delphi). The procedure is a sub algorithm with some commands (also called statements) that are only executed by calling that event.

The program howoldru coded in Pascal. It runs in console.

In Windows, a console is also a little window, but it isn't called 'window' officially! By default the background of this 'window' is black and the font color is white. But you also can change those colors. You can put out or in a text there, but you can't set real components on it. The only thing you can do is simulating this. There are normally no events; your code will be executed off the reel. If you write nothing, the application will be terminated. If you write nothing in a GUI application, nothing will happen — the application won't be terminated!

Most today's applications are GUI application, for example Word, Excel or even Delphi itself. If you want to see a console application, all you have to do is pressing the start button, click Run and enter "cmd". Press 'Return' and the command prompt will open soon.

How can we create a GUI application? There are two ways. The first way — which is not very comfortable — is taking a console application and creating components and windows by using complicated commands defined by the Windows API (Application Public Interface), a library of many functions and procedures about multimedia, graphics and windows.

The second way which is more comfortable is using a special Integrated Development Environment (IDE) where we can design the components at design time (when we develop the application) and the IDE (Delphi) generates the code which creates those windows and components. Normally we — as a Delphi programmer — use this way!

The first application: a console[edit]

Start the IDE; Enter "file" / "new" and choose "console". You will see the following:

  1.  program Project1;
    
  2.  
    
  3.  {$APPTYPE CONSOLE}
    
  4.  
    
  5.  uses
    
  6.    SysUtils;
    
  7.  
    
  8.  begin
    
  9.    { TODO -oUser -cConsole Main : Enter your code here }
    
  10.  end.
    

You can delete line 9. Between begin and end. you can type your code. More about that you will find in the chapter Statements. The first line means that the project is registered and saved under 'Project1', so the unit is saved under 'Project1.pas' (pas = Pascal). '{$APPTYPE CONSOLE}' means that it is a console application. We use the library 'SysUtils' → we have to enter it in the uses clause. Later, when you have written some code, press F9 to compile and run. You will see your program under 'Project1.exe'. This is the file you have to copy to give your application to other people.