39,59 €
Delphi is a strongly typed, event-driven programming language with a rich ecosystem of frameworks and support tools. It comes with an extensive set of web and database libraries for rapid application development on desktop, mobile, and internet-enabled devices. This book will help you keep up with the latest IDE features and provide a sound foundation of project management and recent language enhancements to take your productivity to the next level.
You’ll discover how simple it is to support popular mobile device features such as sensors, cameras, and GPS. The book will help you feel comfortable working with FireMonkey and styles and incorporating 3D user interfaces in new ways. As you advance, you’ll be able to build cross-platform solutions that not only look native but also take advantage of a wide array of device capabilities. You’ll also learn how to use embedded databases, such as SQLite and InterBase ToGo, synchronizing them with your own custom backend servers or modules using the powerful RAD Server engine. The book concludes by sharing tips for testing and deploying your end-to-end application suite for a smooth user experience.
By the end of this book, you’ll be able to deliver modern enterprise applications using Delphi confidently.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 605
Veröffentlichungsjahr: 2021
Expand your Delphi skills to build a new generation of Windows, web, mobile, and IoT applications
David Cornelius
BIRMINGHAM—MUMBAI
Copyright © 2021 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Group Product Manager: Richa Tripathi
Publishing Product Manager: Alok Dhuri
Senior Editor: Rohit Singh
Content Development Editor: Kinnari Chohan
Technical Editor: Maran Fernandes
Copy Editor: Safis Editing
Project Coordinator: Deeksha Thakkar
Proofreader: Safis Editing
Indexer: Pratik Shirodkar
Production Designer: Joshua Misquitta
First published: September 2021
Production reference: 2151121
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-80020-382-2
www.packt.com
To my wife, Terresa, for her incredible patience and support during the writing of this book.
– David
David Cornelius is a software engineer in the Pacific Northwest of the US with over 30 years of experience writing applications for education, research, finance, inventory management, and retail. He has been the coordinator for the Oregon Delphi User Group for two decades and keeps active in various online forums. David is the founder and principal developer at Cornelius Concepts, LLC. and an Embarcadero MVP.
In his spare time, he likes to ride his motorcycle, escape the city with his RV, play the tuba or bass guitar, and play strategy board games with friends.
I would like to thank, first of all, my technical reviewers, Jonathan Eaton and Marco Breveglieri, and the editors at Packt Publishing who helped me see things from the perspective of the reader and caught project errors and omissions I submitted to GitHub (which I tried to pass off as challenges to the readers). They certainly saved me much embarrassment and helped ensure the book you have makes sense—and the sample programs actually compile!
I would also like to thank attendees of the Oregon Delphi User Group, a small but loyal band of awesome Delphi programmer friends, who have been cheerleaders for my writing efforts.
Finally, I very much appreciate the love and support that friends and family have given me even when they didn't fully understand what I was writing about. They helped more than they know.
Jonathan Eaton is a Texan who loves living in Oregon. He has been an avid professional user of Delphi ever since he used it for a Y2K project way back in 1999. Since then, he has used Delphi to design, develop, and update software applications for a variety of industries, including chemical mixing, mineral asset accounting, insurance form processing, commercial real estate appraisal, and the development of software components of medical devices. In 2013, Jonathan became an ASQ Certified Software Quality Engineer and has been a strong proponent for the application of the principles of quality management to software development ever since. He's also written and self-published three novels and several collections of short stories.
Marco Breveglieri is a software and web developer. He started programming when he was 14 and got his first home personal computer, a beloved Commodore 16. Going forward—and getting serious—years later he attended secondary school focusing on computer science, continuing that learning path and taking his first steps from BASIC to Pascal and C++. Today, Marco continues to work for his own IT company using Delphi to create any kind of application. He also uses Visual Studio to build websites, using the Microsoft web stack based on the .NET Framework. He often takes part in technical conferences and holds training courses about programming with all these tools, especially Delphi, C#, and the web standards HTML5, JavaScript, and CSS3.
A favorite author once penned, "Resistance to change leads to catastrophic change." I fully believe that and, in this field, learning and growing and yes, changing, is good—anything else is career death.
Delphi has been around for over 25 years, but it is anything but old. The language, libraries, and toolset are not stagnant and applications built with this suite of compilers rival those of competing products. Continual updates, frequent webinars, lively forum debates, a recent explosion of books, and a myriad of blog sites are evidence of a rich and active developer community.
This book takes you on a journey. This journey will build upon and extend the hours you've invested in creating beautiful desktop applications and teach you how to craft new, globally usable mobile apps and backend servers that power today's interconnected platforms. Technology has exploded with ways to communicate and share data between apps, web services, and devices of all kinds. People demand choices—choice of operating system, choice of style, choice of screen size, and choice of location.
Companies everywhere have risen to the challenge of providing options to satisfy these customer demands with unique tools that market to a new generation of developers. Are you feeling left behind, wondering how to move forward?
Reading this book will expand your Delphi programming skill set in a step-by-step manner, explaining what you need to know to handle the variety of challenges addressing new platforms will bring. And we'll have fun along the way. We'll build database applications that have sample data built in to make prototyping easier than ever. We'll build a 3D game you can play on your smartphone. We'll create a full-featured mobile app you can extend for your own creative uses. We'll also build powerful backend REST servers with virtually no code.
Take this journey with me to multiple platforms—fearlessly!
This book is written mostly for the Delphi programmer who is confident on the Windows platform with the VCL but unsure what mobile development will mean for them. If you're relatively new to Delphi programming, this book will be a quick jump start into the rich and wonderful integrated development environment we all love—you'll figure it out quickly!
The experienced cross-platform developer will likely seek me out on a forum and point out obvious things I missed. I'm sorry in advance—this was a journey for me as well!
Chapter 1, Recent IDE Enhancements, will help you get up to speed quickly, starting with an understanding of the many parts of the IDE, and then explaining "What's new?" in each of the last five major versions.
Chapter 2, Delphi Project Management, simplifies the bewildering choice of where to start! With so many platforms and project templates, deciding how to create a new app can itself be overwhelming. We'll explore build configurations, shortcuts to managing options and project groups, and show you how to use the command-line compiler for automation.
Chapter 3, A Modern-Day Language, clears up any misunderstanding that Delphi is not capable of building cross-platform apps. We'll showcase enhancements made through the years to the Pascal-based syntax that facilitated the flexible and powerful language it is today.
Chapter 4, Multiple Platforms, One Code Base, is where we'll really start diving into cross-platform topics. The first step in that direction is learning about FireMonkey—we will take you carefully through several differences compared with the VCL in some simple apps so you can get comfortable quickly. Then we introduce various form factors of mobile devices and show how to manage them in Delphi. Finally, we cover several conditional compilation constants you'll need to know in order to separate code for specific platforms.
Chapter 5, Libraries, Packages, and Components, is an important chapter as writing a simple DLL isn't so simple when you have to deploy to platforms other than Windows—we cover several gotchas that you need to know. After we build both a dynamic library and a package (and why you might need one over the other), we turn a package into a cross-platform component.
Chapter 6, All About LiveBindings, explains how this expression-based data connector technology is more than just a replacement for data-aware controls in Delphi. The LiveBindings Designer is explained with tips on organizing with layers. We demonstrate how the LiveBindings Wizard can create components for you already hooked up to your data. Finally, we use custom formatting and parsing—and end with code that creates custom LiveBinding methods installed in Delphi.
Chapter 7, FireMonkey Styles, uncovers the nuances of styling a FireMonkey application and how to use and customize FireMonkey styles effectively for a distinctive appearance. We will build a simple app with a variety of controls and four different styles that you can run on each of your devices to see the differences.
Chapter 8, Exploring the World of 3D, is a fun chapter that demonstrates how you can use your favorite programming tool to utilize popular GPU engine libraries with ease. We build a simple app with a variety of 3D shapes, textures, lighting, and cameras for a broad overview of the capabilities. We end the chapter by using some of these techniques and more to build an escape game you can play on your phone!
Chapter 9, Mobile Data Storage, shows how well FireDAC works with different database products on multiple platforms, comparing one application that uses InterBase ToGo and a similar one using SQLite. We'll answer questions on licensing, explore free management tools, and offer tips for working with touchscreen devices.
Chapter 10, Cameras, the GPS, and More, demonstrates the power of the FireMonkey library to encapsulate various platform capabilities in simple components that you can readily use in your apps. We'll also build a database-enabled mobile app that utilizes techniques learned in previous chapters but that will grow in functionality for the rest of this book.
Chapter 11, Extending Delphi with Bluetooth, IoT, and Raspberry Pi!, explores various types of Bluetooth technologies, including BLE, which is what beacons are based on. We dive further and show how BLE is the basis for all IoT components and explain how to use ThingConnect components you can get from GetIt. We conclude this chapter on small devices by demonstrating how to deploy a Delphi app to a Raspberry Pi.
Chapter 12, Console-Based Server Apps and Services, switches gears from small, mobile devices to server technologies. We create both Windows services and Linux daemons and also show how to take an open source project and modify it for our own needs when we need to implement custom logging.
Chapter 13, Web Modules for IIS and Apache, continues the server discussion by concentrating on web server modules for the two most popular web servers – IIS on Windows, and Apache for both Windows and Linux. We bring over the data module tested in our console-based server and use WebBroker to build a simple but nice-looking web page that displays data in a grid with very little code.
Chapter 14, Using the RAD Server, is a big chapter covering a big product available for Delphi Enterprise and up that serves as another type of platform, one that provides a REST server in a box where you only add your custom business methods. We teach what RAD Server brings to the table, why it can pay for itself, and how to write modules for it. Then we go one step further and modify our sample mobile app to use it.
Chapter 15, Deploying an Application Suite, culminates the application development process by covering important aspects of deploying to production with discussions about externally defined configuration files, various security concerns, application icons and identity, testing, the installation of server backends, and mobile app submission.
You will need Embarcadero Delphi 10.4 Sydney Professional or Enterprise on Windows 64-bit with access to one or more mobile platforms on which you can deploy and test the sample applications:
You will need the Enterprise edition of Delphi to build Linux apps or RAD Server modules. Also, while the Community Edition may be used for many of the examples, you may be restricted from downloading from the GetIt portal.
If you are using the digital version of this book, we advise you to type the code yourself or access the code from the book's GitHub repository (a link is available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.
You can download the example code files for this book from GitHub at https://github.com/PacktPublishing/Fearless-Cross-Platform-Development-with-Delphi. If there's an update to the code, it will be updated in the GitHub repository.
We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
We also provide a PDF file that has color images of the screenshots and diagrams used in this book. You can download it here: https://static.packt-cdn.com/downloads/9781800203822_ColorImages.pdf.
There are a number of text conventions used throughout this book.
Code in text: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Back in the form unit, uCardPanel.pas, it starts with the unit keyword and has both an interface and implementation section, each with a uses clause."
A block of code is set as follows:
procedure TfrmPeopleList.lbPeopleClick(Sender: TObject);
var
APerson: TPerson;
begin
if lbPeople.ItemIndex > -1 then begin
APerson := lbPeople.Items.Objects[lbPeople.ItemIndex]
as TPerson;
lblPersonName.Caption := APerson.FirstName + ' ' +
APerson.LastName;
lblPersonDOB.Caption := FormatDateTime('yyyy-mm-dd',
APerson.DateOfBirth);
end;
end;
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
constructor TfrmPeopleList.TPerson.Create(
NewFN, NewLN string; NewDOB: TDate);
begin
FFirstName := NewFN;
FLastName := NewLN;
FDateOfBirth := NewDOB;
end;
Bold: Indicates a new term, an important word, or words that you see on screen. For instance, words in menus or dialog boxes appear in bold. Here is an example:
"Under the Develop section of Delphi's default Welcome page, click the Open a sample project… link and drill down through the Object Pascal, VCL, and CardPanel folders, and then open the CardPanel project."
Tips or important notes
Appear like this.
Feedback from our readers is always welcome.
General feedback: If you have questions about any aspect of this book, email us at [email protected] and mention the book title in the subject of your message.
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata and fill in the form.
Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Once you've read Fearless Cross-Platform Development with Delphi, we'd love to hear your thoughts! Please click here to go straight to the Amazon review page for this book and share your feedback.
Your review is important to us and the tech community and will help us make sure we're delivering excellent quality content.
This section introduces Delphi as a powerful programming tool for fully embracing cross-platform development, explains why it's so much more than just a legacy Windows compiler, and helps get you up to speed with the latest IDE and language enhancements that accelerate development and decrease the time to market. This foundational knowledge sets the stage for the rest of the book, helping you to navigate the IDE, write efficient code, drop to the command line if you need, and become familiar with new programming constructs.
This section comprises the following chapters:
Chapter 1, Recent IDE EnhancementsChapter 2, Delphi Project ManagementChapter 3, A Modern-Day LanguageThere are many types of projects that Delphi can build: standalone desktop applications for Windows and Mac, mobile apps for Android and iOS smartphones and tablets, Linux daemons, Android packages and services, and even plugin packages to add functionality within Delphi itself; all types are possible. If all you've ever built are Windows Visual Component Library (VCL) applications, it may be daunting to consider other platforms. But Delphi 10.4 is a very capable toolset, having simplified the steps you need to take, which allows you to concentrate on what you do best—building great software! Most of the heavy lifting is done for you when supporting other platforms, so it's time to extend your skills to these devices.
To help you down the right path, you need to know about the project types, target platforms, and starting templates available and how to best utilize them. After reading this chapter, you will have no difficulty knowing where to start. Additionally, you'll learn when to use debug and release configurations to separate testing and deployment scenarios, how to integrate version control right within the IDE, and how to set up automation both inside the IDE with build events and outside with command-line tools. These topics will be covered in the following sections:
Exploring project types and target platformsUsing build configurations effectivelyWorking with related projectsManaging source modifications Using the command-line tools for build automationThis chapter simply requires using Delphi on a Windows 10 machine to explore different project types. There will be screenshots of short example applications running on different platforms but they will be brief and only exist to showcase what lies ahead. The code for a couple of small Windows projects, including a batch script for compiling several projects is on GitHub at: https://github.com/PacktPublishing/Fearless-Cross-Platform-Development-with-Delphi/tree/master/Chapter02
Exploring project types and target platforms
As we look at each project type in this section, there will be different target platforms available. For example, a Windows VCL application can only be built on Windows platforms (either 32-bit or 64-bit), but a multi-device application can have several different target platforms. This is shown in the Projects window as the second sub-item under the project name and can be expanded to show the currently supported platforms added for that project. To add support for other platforms, right-click on Target Platforms and select from the list available. For example, a new multi-device application comes supporting six targets by default, but you can also add iOS Simulator:
Figure 2.1 – Adding and selecting a target platform
As we talk about the different project types and what platforms are available for each, this is how you add support for platforms that are not initially added to your project. You can only compile for one target at a time; the currently selected target is shown in parentheses next to the Target Platforms header.
If you don't need a rich interface with complex graphical controls, you may need nothing more than a console application. This is the simplest project type and is often used for small utility programs, automated unit testing, and prototyping Windows services or IIS modules. It can write output to the console using Writeln and pause for input using Readln but does not include any graphical libraries. Console applications can be built on Windows, Linux (Enterprise or Architect edition), or Mac OS X—and even though you can add platforms for Android and iOS, there seems to be no documentation or support on how to run console apps on mobile platforms.
To create a console application, select File | New | Other from the menu and select Console Application. A single source file is created with the program keyword and a compiler directive of {$APPTYPE CONSOLE}. All the sources can be contained in this one file and the program flow is simple and contained within the begin-end block created for you. The compiled application on Windows will be .EXE.
Running this will bring up a console window, typically with a black background and white text, and any output you've added to the application is displayed in a scrolling window within this box. You can add code to read text entered from the keyboard using Readln—which, if you're running it from the IDE, you will need to do in order to keep it from finishing and closing so quickly that you miss seeing any output.
Here's what a simple console application looks like when running:
Figure 2.2 – Windows 64-bit console application
We will use console apps to demonstrate language concepts and to test Windows services in later chapters.
A Windows VCL application has been the most common project type since the first version of Delphi. In fact, the main reason Delphi was created was to make it easier to develop Windows applications by replacing the Object Windows Library (OWL) framework used in Borland Pascal as wrappers around the Windows API. The VCL hides a lot of complexity and yet provides an extensible hierarchy of object-oriented components to ease the development of any type of Windows application.
To start one of these project types, select File | New | Other and select Windows VCL Application. A project source file is created with the program keyword and a "form" unit is added, which comprises two physical files: a .DFM, or Delphi, form, and .PAS, the source for this form with the unit keyword at the top of the file. The extension of the compiled application will be .EXE, for "executable."
Without doing anything else, simply building and running this project immediately launches a Windows application with one form that can be moved around the screen and closed like any other Windows application.
