1. Computing
NO GUI Delphi applications
Page 1: Setting up a console mode application with Delphi.
More of this Feature
Page 2: A real example
Join the Discussion
"Post your views, comments, questions and doubts to this article."
Discuss!
Related Resources
Wizards
DPR: Project file
Running with parameters
Working with File I/O
Working with resources
Text file routines
Elsewhere on the Web

Console applications are pure 32-bit Windows programs that run without a graphical interface. When a console application is started, Windows creates a text-mode console window through which the user can interact with the application. These applications typically don't require much user input. All the information a console application needs can be provided through command line parameters.
For students, console applications will simplify learning Pascal and Delphi - after all, all the Pascal introductory examples are just console applications.

New ... Console application
Here's how to quickly build console applications that run without a graphical interface.

Console app wizardIf you have a Delphi version newer than 4, than all you have to do is to use the Console Application Wizard. Delphi 5 introduced the console application wizard. You can reach it by pointing to File|New, this opens up a New Items dialog - in the New page select the Console Application. Note that in Delphi 6 the icon that represents a console application looks different. Double click the icon and the wizard will setup a Delphi project ready to be compiled as a console application.

While you could create console mode applications in all 32-bit versions of Delphi, it's not an obvious process. Let's see what you need to do in Delphi versions <=4 to create an "empty" console project. When you start Delphi, a new project with one empty form is created by default. You have to remove this form (a GUI element) and tell Delphi that you want a console mode app. This is what you should do:

0. Select "File | New Application"
1. Select "Project | Remove From Project..."
2. Select Unit1 (Form1) and click OK. Delphi will remove the selected unit from the uses clause of the current project.
3. Select "Project | View Source"
4. Edit your project source file:
• Delete all the code inside "begin" and "end".
• After the uses keyword, replace the "Forms" unit with "SysUtils".
• Place {$APPTYPE CONSOLE} right under the "program" statement.

You are now left with a very small program which looks much like a Turbo Pascal program which, if you compile it will produce a very small EXE. Note that a Delphi console program is not a DOS program because it is able to call Windows API functions and also use its own resources. No matter how you have created a skeleton for a console application your editor should look like:

Console app blank project

This is nothing more than a "standard" Delphi project file, the one with the .dpr extension.

. The program keyword identifies this unit as a program's main source unit. When we run a project file from the IDE, Delphi uses the name of the Project file for the name of the EXE file that it creates - Delphi gives the project a default name until you save the project with a more meaningful name.
. The $APPTYPE directive controls whether to generate a Win32 console or graphical UI application. The {$APPTYPE CONSOLE} directive (equivalent to the /CC command-line option), tells the compiler to generate a console application.
. The uses keyword, as usual, lists all the units this unit uses (units that are part of a project). As you can see, the SysUtils unit is included by default. Another unit is included too, the System unit, though this is hidden from us.
. In between the begin ... end pair you add your code.

Ok. Now when you know how a console application looks like it's time for a real example. Let's create a simple game!

Next page > A console mode Delphi example; Cosole applications: Tips & Tricks > Page 1, 2

©2013 About.com. All rights reserved.