Starting with a new programming language or framework typically involves creating a program that displays the famous "Hello World" message, and we will follow this convention. Our app will have just one button. When you press the button, aDelphi Hello World! message will be displayed. Later in this chapter, we are going to put this app on an iPhone and on an Android device.
Click on theMulti-Device Application - Delphi option in theFile |New menu, as shown in the following screenshot:
New "Multi-Device Application - Delphi" menu option
This will display the wizard with different multi-device project templates, as shown in the following screenshot:
Multi-device application project templates dialog
Double-click on theBlank Application project template.
The first thing after creating a new project is to save it all. The Delphi project is made of multiple files that are managed by the IDE. The IDE will also create subfolders in theproject directory for managing different artifacts such as compiled executable files, editor history, and recovery files, so it is always a good idea to save a new project into an empty directory.
Click on theSave All button in the Delphi toolbar just below the main menu to save all files in the project, as shown in the following screenshot:
The Save All speed-button
Clicking from time to time on theSave All button is something that you do almost automatically.
First, we will be asked to save the main form of our application. EnteruFormHelloWorld in the file save dialog and click onSave. Then, the second file save dialog is displayed. Here we need to give our project a name. EnterDelphiHelloWorld and click onSave.
Delphi organizes all files necessary to build an app into aProject. The Delphi IDE contains a number of windows that help us work on our projects. There are different project types. It is possible to create a project group inside the IDE, but at any given moment of time there can be only one project active. You can see the name of the active Delphi project in the very top left corner of the Delphi window. Its name is also displayed in bold font in theProject Manager. Delphi IDE is context sensitive, so the contents of different windows and dialogs can vary. An example of a context sensitive dialog isProject Options. Select theOptions item from the bottom of theProject menu. All options related to the current project opened in the IDE will be displayed, as shown in the following screenshot:
Project Options Dialog
Let's add a button to the form. The fastest way to work in the IDE is to use theIDE Insight. There are hundreds of different options, components, and windows in the IDE. If you know what you are looking for, you can just pressF6 orCtrl and. keys at the same time. The IDE Insight combobox will receive focus. You can also just click on the IDE Insight combobox in the top corner of the IDE window. We want to add a button to the form. Just start typing what you are looking for and notice how, with every keystroke, the list of available items in the IDE Insight combobox is changing. Typeb,u, andt and, after three keystrokes, theTButton component is the first on the list. Refer to the following screenshot:
Incremental filtering in the IDE Insight combobox
Just pressEnter and a button is added to the form. It is probably one of the single most useful productivity features of the IDE. Six keystrokes are needed to get the component added to the form. Alternatively, we could locate theTButton component in theTool Palette window and double-click it to add it to the form. Over time, you will find that you pressCtrl + almost subconsciously, very much like clicking onSave All. In fact, you can click onSave All right now. :)
We have a button in the middle of an empty form. Let's start working on how our app will look--that's form design. What our app is going to do--that's coding. Click on the button and move it more toward the window's top left corner. In the left bottom part of the IDE there is theObject Inspector window. Here we can modify properties and events of components. We need to change the text that is displayed on the button. Make sure thatTButton1 is selected in theObject Inspector and find itsText property. Replace the defaultButton1 withHello World. Now, find theName property of the button and change it tobtnHelloWorld. Notice that the new name of the component has been immediately reflected in theStructure view above theObject Inspector. Now select the form. You can click somewhere on the form in theForm Designer to select it or just click on the form's node in theStructure view aboveObject Inspector. That's how the IDE works. Different windows are synchronized with each other, so selecting a component, changes the current component selection in other views as well. Find theName property of the main form and change the defaultForm1 toFormHelloWorld. Now, find theCaption property of the form and change it toDelphi Hello World. Save all.
It is always good practice to give components and their properties meaningful names. There are different naming conventions. One of the most popular ones is to give components such name, that, just from the name itself, we could understand what the type of this particular component is. You also want to keep names short. If names are too long, you need to type more. Long names in the source code also tend to be less readable. It is up to you to decide what naming convention you use, but once you have decided, it is important to stick to it.
Now we need to define what will happen when the user of our app clicks on the button. For this, we need to attach an event handler to theOnClick event, of thebtnHelloWorld button. We could switch to theEvents tab in theObject Inspector, locate theOnClick event and double-click on the space next to it to generate an empty event handler. Let's do it faster. Just double-click on the button on the form. The IDE will switch from theForm Designer toCode Editor, and the cursor will be placed just at the beginning of an empty line betweenbegin andend where we need to enter some code in theObject Pascal language. This code will be executed by our app in response to the click event. It could be misleading that this event is calledOnClick, as you can use the mouse on desktop computers only. Fear not! If we compile our app to mobile targets, this event will be triggered in response to touching the button on the mobile device screen. In the code, we will invoke the built-inShowMessage function and pass to itDelphi Hello World! as the string that we want to display in the message.
Click onSave All again. Now let's run the application. Refer to the following screenshot:
Run the current project without debugging
Click on theRun green arrow icon under the main menu to build and run the project. You will get the following output:
Delphi "Hello World" multi-device app running on Windows
We are on Windows and by default, we will compile our multi-device app as a Windows 32-bit executable. This is very useful during the developement of mobile apps with Delphi. During the development it is quicker to run our multi-device app on Windows to see that it does not contain any mistakes. If it compiles fine on Windows, then there is a big chance that it will also compileOK with mobile compilers. Building and deploying to a mobile device typically takes longer.
Every Delphi form is made of two source code files. You can switch between these files by clicking on the third button from the left in the toolbar with the Toggle Form/Unit (F12) hint or at the bottom of the screen using theCode andDesign tabs. When we entereduFormHelloWorld as the name of the main form of our app, the IDE created two files:uFormHelloWorld.pas anduFormHelloWorld.fmx. The first file we can see in theCode tab is the source code, and the following is the screenshot of the toolbar with the toggle button to switch between the files:
Toggle Form/Unit speed button
The Code Editor is where we edit Delphi source files with thepas filename extension. We can see directly the contents of the file in the editor, as shown in the following screenshot:
Delphi Code Editor
The contents of thefmx file are managed by theForm Designer and we do not edit it directly. Every time we change something in theObject Inspector or in theForm Designer, these changes are stored in thefmx file. TheForm Designer gives us awhat you see is what you get user experience, so we can see how our app will look even before we run it. Refer to the following screenshot:
Delphi Form Designer - View as Form
You can preview the text of the form file by right-clicking somewhere on the form and selecting theView As Text option from the context menu.
To return to the form view, just right-click on the editor again and selectView As Form from the context menu, as shown in the following screenshot:
Delphi Form Designer - View as Text
The most important window in the IDE is theProject Manager. It provides a graphical interface to work with all files that make up our projects and lets us switch between different compilers and build configurations. You can build your applications in eitherdebug orrelease mode. The debug mode is used during the development of an app. The resulting binary file will have additional binary information embedded that is used by a debugger. When our app is ready to be built for distributing to an app store, then we can switch to release mode.
This will generate an app in the form that is suitable for distribution. Take a look at the following screenshot:
Delphi Project Manager
Let's have a look at different files that make up a Delphi project. If you want to quickly find the folder where the current project is located, you can do it easily. Click on the name of the project in theProject Manager. In theObject Inspector, you should now see,Full Name andFull Path properties. Copy the content of theFull Path property to the clipboard, paste it into Windows Explorer, and then hitEnter, as shown in the following screenshot:
Current project path and file name in Object Inspector
You should now see all the files and folders that make up ourDelphiHelloWorld project:
Files that make up a Delphi project in Windows 10 File Explorer
Alternatively, you could also right-click on the project name in theProject Manager and select theShow in Explorer option.
Among others, you will see here two files nameduFormHelloWorld and two project files with the same name as our projectDelphiHelloWorld, but with different extensions. The first one has the extensiondpr, which stands forDelphi Project and contains the main program file of our application. We can preview this file inside the IDE by going to theProject |View Source menu, as shown in the following screenshot:
Delphi project source code
It is the application main program file which is managed by the IDE and most of the time, we need to change anything there. The second project file has adproj extension. If we open it in Notepad, we will see that it contains XML code with build instruction, for the MSBuild engine that is used by Delphi to manage all files and resources that are needed to build our project for different targets and configurations, as shown in the following screenshot:
Contents of the dproj project file
There is also awin32 subfolder here. It contains a subfolder calleddebug and two files:DelphiHelloWorld.exe anduFormHelloWorld.dcu. The first file is a regular Windows 32-bit executable program that we have just built. It does very little. It only has a button and displays a message. The second file is the product of compiling a form file and is only useful for the IDE during the build process. You can safely delete thewin32 folder and its contents. The next time you run the application, all folders and files will be recreated. That's where the difference betweenbuilding andcompiling the project comes. The very first time, both commands do exactly the same--they generate all binary files in the output folder. In fact,dcu files are generated first and theexe file is generated next. If we selectBuild every time, all binary files are recreated. If we selectCompile, only thosedcu files are recreated that have changes. This really speeds up running a project inside of the IDE.