Delphi in all its glory
Writed by : Gabriel Moraru
Published date : 29/03/2023
ISBN-13 : 9798387412141
Language : English
Web site : https://gabrielmoraru.com/delphi-in-all-its-glory/
For whom is this book for?
This book is for everyone who wants to learn how to build fast, good-looking, stable applications. It doesn't matter if you never wrote a line of code, we will start from ground zero, but we will not stop until we reach the most advanced topics.
What is Delphi?
Delphi is a modern multi-purpose programming language. With Delphi, we can create cool looking graphic applications for Windows, Mac, Linux and Android. Take a look at "Let there be styles" chapter to see Delphi programs in all their beauty.
Delphi is fast, Delphi is easy to learn (seriously), Delphi is free (for personal use with its Community Edition).
1. Overview . . . . . 2
- For whom is this book for? . . . . . 2
- What is Delphi? . . . . . 2
- What will you learn from this book? . . . . . 2
- Book layout . . . . . 3
- Who am I? . . . . . 3
- How is this book written? . . . . . 4
- This book is eco-friendly . . . . . 4
- Pre-requisites for this book . . . . . 4
- Let’s start . . . . . 4
2. Table of content . . . . . 5
3. What is Delphi? . . . . . 24
- Delphi is RAD . . . . . 24
- Delphi is general-purpose . . . . . 24
- Delphi has deep roots . . . . . 24
- A bit of history . . . . . 25
- Delphi = GUI . . . . . 26
- What is the VCL? . . . . . 26
- What is RAD Studio? . . . . . 26
- An IDE with multiple personalities. How does it work? . . . . . 27
- A complete IDE . . . . . 28
- Mature and polished . . . . . 28
- But, it must be expensive... right? . . . . . 29
- What can we build right out of the box, with Delphi? . . . . . 30
- Advantages of Delphi applications . . . . . 32
- Where can we use Delphi? . . . . . 34
- Where can’t we use Delphi? . . . . . 35
- Delphi’s productivity . . . . . 35
- Famous programs built with Delphi . . . . . 36
- Summary . . . . . 36
4. Delphi today . . . . . 37
- Popularity . . . . . 37
- About Evil, Darth Vader and... Bill Gates . . . . . 38
- Availability of libraries . . . . . 39
- A brighter future . . . . . 39
- Tiobe Index . . . . . 40
- Summary . . . . . 40
5. An overview of the Delphi language . . . . . 42
- Clarity . . . . . 42
- A strongly typed language . . . . . 42
- Well designed . . . . . 42
- One string to rule them all . . . . . 43
- Not open-source . . . . . 43
- Fastest compiler . . . . . 45
- Better error messages . . . . . 45
- The compiler is here to help us . . . . . 46
- No more pointers . . . . . 46
- ASM in the mix . . . . . 46
- Damn fast . . . . . 46
- Compared to Julia . . . . . 47
- What kind of applications can we build with Delphi? . . . . . 47
- Why building apps is so easy with Delphi? . . . . . 48
- Delphi is WYSIWYG . . . . . 48
- Quick mockups . . . . . 48
- The VCL framework . . . . . 49
- Low code / no code . . . . . 49
- Portable code . . . . . 50
- Native look . . . . . 50
- Ready for the future . . . . . 50
- The never-ending story . . . . . 51
- Reminder . . . . . 51
6. Delphi’s brothers . . . . . 52
- Lazarus / FPC . . . . . 52
- The “Burning Monkey” . . . . . 54
- A bit of history . . . . . 54
- Firemoneky = VCL on steroids . . . . . 55
- Disadvantages of FMX . . . . . 55
- Converting a VCL project to FireMonkey . . . . . 56
- Delphi’s cousin . . . . . 57
- Summary . . . . . 57
7. Installing Delphi . . . . . 58
- ISO installer . . . . . 58
- The installation folder . . . . . 58
- Delphi sample projects . . . . . 62
- Delphi version info . . . . . 62
- Known bugs . . . . . 62
- Updates . . . . . 62
- We are done . . . . . 63
8. Let’s get started . . . . . 64
- Anatomy of a Delphi Project . . . . . 64
- The Project Manager . . . . . 64
- Project Options . . . . . 66
- The DProj: DPR’s little brother . . . . . 71
- Anatomy of a Delphi unit . . . . . 72
9. IDE this... IDE that... . . . . . 76
- The main menu . . . . . 77
- File . . . . . 77
- Edit . . . . . 77
- Search . . . . . 77
- Refactor . . . . . 78
- Run . . . . . 78
- GExperts and CnPack . . . . . 78
- Project . . . . . 78
- The main components of the IDE . . . . . 80
- The Project Manager . . . . . 80
- The Component Palette . . . . . 80
- The Object Inspector . . . . . 80
- The Messages window . . . . . 80
- Form designer . . . . . 80
- Customizing the IDE . . . . . 81
- The Code Editor . . . . . 82
- Code insights [1] . . . . . 82
- Code insights [2] . . . . . 82
- Indent a block of code . . . . . 83
- Easily re-order lines code . . . . . 83
- Auto-fix typos . . . . . 83
- Code Editor useful keyboard shortcuts . . . . . 83
- The Form Designer . . . . . 84
- Common IDE shortcuts . . . . . 85
- Third party IDE plugins for Delphi . . . . . 86
- DDevExtensions . . . . . 86
- CnPack . . . . . 87
- GExperts . . . . . 91
- We can build our own plugins . . . . . 91
- Plugin management . . . . . 92
10. Our first Delphi application . . . . . 93
- Smallest Delphi application . . . . . 93
- Let’s start . . . . . 93
- Form = PAS + DFM . . . . . 98
11. A crash course in the Delphi language . . . . . 99
- If you already know C++ . . . . . 99
- A typical Delphi piece of code . . . . . 100
- Comments . . . . . 101
- Variables . . . . . 101
- What is a variable? . . . . . 101
- What are data types? . . . . . 101
- Declaring variables . . . . . 102
- General naming conventions in Delphi . . . . . 103
- Naming variables . . . . . 104
- Global versus local variables . . . . . 105
- Variable initialization . . . . . 106
- Assigning values to a variable . . . . . 107
- Constants . . . . . 108
- Arithmetic operators . . . . . 109
- How do computers store numbers in their memory? . . . . . 110
- Memory Cells . . . . . 110
- Data types . . . . . 113
- Primitive Data Types . . . . . 114
- Managed Data Types . . . . . 115
- Delphi is “strong typed” . . . . . 116
- Boolean data type . . . . . 117
- Integer data type . . . . . 117
- Strings . . . . . 119
- Advanced topics . . . . . 125
- Enumerations . . . . . 126
- Subranges . . . . . 127
- Real numbers . . . . . 127
- Arrays . . . . . 130
- The size of a variable . . . . . 137
- Defining our own data types . . . . . 139
- Routines . . . . . 140
- 1. Compartmentalizing your code . . . . . 140
- 2. Reusing your code . . . . . 141
- Procedures . . . . . 142
- Functions . . . . . 143
- Good practice . . . . . 149
- Statement separator . . . . . 149
- How does a computer program work? . . . . . 150
- Displaying a message . . . . . 150
- Records . . . . . 151
- Assigning records . . . . . 152
- Field initialization . . . . . 155
- Advanced topics . . . . . 155
- With . . . . . 156
- FOR loops . . . . . 157
- While . . . . . 159
- Repeat/Until . . . . . 160
- Nested routines . . . . . 160
- Function overloading . . . . . 161
- Parameters of a routine . . . . . 161
- Open array parameters . . . . . 163
- Not all parameters are created equal . . . . . 164
- By reference . . . . . 164
- By value . . . . . 164
- By constant value (fastest) . . . . . 165
- By constant reference . . . . . 165
- Out parameters . . . . . 166
- Default parameters . . . . . 166
- Playing more with parameters of a routine . . . . . 167
12. Classes and objects . . . . . 171
- Why objects? . . . . . 171
- An object . . . . . 171
- Objects vs classes . . . . . 172
- Naming conventions . . . . . 172
- Instantiation . . . . . 172
- Extensibility . . . . . 173
- Constructors . . . . . 173
- Let's build our own class: . . . . . 174
- Inherited . . . . . 174
- Destructors . . . . . 175
- Try/Finally . . . . . 175
- Super-duper important . . . . . 176
- Special cases . . . . . 176
- Fields . . . . . 178
- Properties . . . . . 178
- Read-only properties . . . . . 180
- Recap . . . . . 181
- Published properties . . . . . 181
- Default . . . . . 182
- Manual memory management for objects . . . . . 182
- Pointers . . . . . 187
- Typed vs untyped pointers . . . . . 188
- Dereferencing a pointer . . . . . 188
- Assigning pointers . . . . . 189
- References . . . . . 192
- Dangling references . . . . . 194
- Assigning an object to another object . . . . . 195
- Objects – A real world example . . . . . 198
- Inheritance, polymorphism, and typecasting . . . . . 199
- The inheritance chain . . . . . 199
- Typecasting . . . . . 201
- The hard typecast . . . . . 202
- The 'as' typecast . . . . . 203
- The 'is' operator . . . . . 204
- Invalid typecasts . . . . . 205
- More invalid typecasts . . . . . 205
- RTTI alternative . . . . . 206
- Methods . . . . . 207
- Method binding . . . . . 207
- Speed . . . . . 208
- Static methods . . . . . 208
- Overriding methods / Polymorphic behavior . . . . . 209
- Polymorphism . . . . . 211
- Hiding vs overriding . . . . . 212
- VMT . . . . . 214
- Overloading methods . . . . . 215
- Recap for polymorphism . . . . . 217
- Another experiment . . . . . 217
- Declaring multiple constructors with the same name . . . . . 218
- Keywords recap . . . . . 220
- Methods - Advanced topics . . . . . 221
- Class methods . . . . . 221
- Concrete example . . . . . 221
- Let's write a better example . . . . . 223
- Class fields . . . . . 224
- Putting class fields to a good use - Singleton class . . . . . 225
- One more example . . . . . 225
- Predefined class methods in TObject . . . . . 226
- Abstract methods . . . . . 227
- Abstract classes . . . . . 230
- Generics . . . . . 230
- Creating a generic class . . . . . 232
13. Forms . . . . . 234
- PAS + DFM = Form . . . . . 234
- Auto-created forms . . . . . 236
- The main form . . . . . 236
- Secondary forms . . . . . 237
- Stay on top . . . . . 240
- The “Application” object . . . . . 240
- Showing a new created form . . . . . 241
- The owner of a form . . . . . 241
- Application as owner . . . . . 241
- Form as owner . . . . . 242
- Nil as owner (no owner) . . . . . 242
- Form initialization . . . . . 242
- Closing a form . . . . . 246
- Several ways to close a form . . . . . 247
- Release versus Close . . . . . 248
- About GUI Applications . . . . . 249
- So, why do we need GUIs? . . . . . 249
- Save GUI state to file . . . . . 258
- User selectable styles . . . . . 258
- Summary . . . . . 259
14. Visual components . . . . . 260
- Parenting and Ownership . . . . . 261
- Parenting and modularization . . . . . 262
- Ownership vs Parenting . . . . . 264
- Properties of a component . . . . . 264
- Building our own visual component . . . . . 265
- Event handlers . . . . . 267
- The ubiquitous Sender parameter . . . . . 270
- Most used visual components . . . . . 271
- TLabel . . . . . 272
- TButton . . . . . 272
- TPanel . . . . . 272
- TRadioButton . . . . . 273
- TCheckBox . . . . . 273
- TImage . . . . . 273
- TEdit . . . . . 274
- TMemo . . . . . 274
- TRichEdit . . . . . 274
- TSpinEdit . . . . . 275
- Visual Components (advanced) . . . . . 275
- TTrayIcon . . . . . 275
- TFlowPanel . . . . . 278
- TGridPanel . . . . . 280
- Actions / TActionList . . . . . 282
- TActionManager . . . . . 289
15. The Delphi compiler . . . . . 297
- What is a compiler? What is an interpreter? . . . . . 297
- A reliable compiler . . . . . 297
- The toolchain . . . . . 298
- Executable size . . . . . 298
- Compiler speed . . . . . 299
- Improve compiler’s speed . . . . . 299
- From Pas to Exe . . . . . 301
- The compilation order . . . . . 302
- Running the program . . . . . 303
- DCUs . . . . . 303
- The linker . . . . . 303
- Compile vs Build . . . . . 304
- Debug vs Release DCUs . . . . . 304
- Release-mode . . . . . 304
- Debug-mode . . . . . 305
- The Debug information . . . . . 305
16. Libraries . . . . . 307
- Library architecture . . . . . 307
- It is all about aliens and pyramids . . . . . 309
- The files of a library . . . . . 310
- DPK . . . . . 310
- GroupProj . . . . . 310
- BPL . . . . . 311
- Using libraries in our projects . . . . . 311
- The RTL library . . . . . 312
- The VCL library . . . . . 312
- Library paths . . . . . 313
- Environment variables . . . . . 313
- The “Library” settings . . . . . 313
- The “Library path” field . . . . . 315
- The “Package/DCP/HPP output directory” fields . . . . . 318
- The “Browsing path” field . . . . . 318
- The “Debug DCU path” field . . . . . 320
- Debugger Source Path . . . . . 320
- Precompiled files . . . . . 320
- The “Use debug DCUs” field . . . . . 321
- Precompiling our own files . . . . . 322
- The “Debug source path” field . . . . . 323
- Project Paths . . . . . 325
- How to install a library? . . . . . 326
- Installing a library . . . . . 326
- Adding library’s folder to the “Search path” . . . . . 327
- Installers . . . . . 329
- Installing libraries via GetIt . . . . . 329
- How to uninstall a library from Delphi . . . . . 331
- Runtime/Designtime libraries . . . . . 332
- Run-time libraries . . . . . 332
- Design time libraries . . . . . 332
- Design time vs Run time . . . . . 333
- How to use a library in our projects? . . . . . 334
- About libraries and licenses . . . . . 335
- Discussions . . . . . 337
- Conclusions . . . . . 338
- More details . . . . . 338
- Dynamic Link Libraries (BPLs) in Delphi . . . . . 338
- Link with runtime packages . . . . . 339
- Separating Libraries from Projects . . . . . 341
- The libraries are “global” . . . . . 341
- Debugging 3rd party libraries . . . . . 342
- Using C/++ libraries in Delphi . . . . . 343
- Popular libraries . . . . . 345
- FastMM4 . . . . . 345
- Graphics32 . . . . . 346
- Delphi LightSaber . . . . . 346
- Jedi . . . . . 346
- LightSaber . . . . . 347
- Freeware . . . . . 347
- Sub-libraries . . . . . 348
- LightSaber Core . . . . . 349
- LightSaber Log . . . . . 351
- LightSaber Common . . . . . 352
- LightSaber Internet . . . . . 353
- Filename convention used in Delphi LightSaber . . . . . 354
- Proteus . . . . . 354
- What is Proteus? . . . . . 354
- What can Proteus do for us? . . . . . 355
- How to use it? . . . . . 355
- Proteus details . . . . . 357
- Setting up your app . . . . . 360
- Advanced topics . . . . . 362
- Certificate ID . . . . . 364
- Active certificate . . . . . 365
17. The debugger . . . . . 366
- What is a debugger? . . . . . 366
- Preparing our project for debugging . . . . . 366
- Stacks . . . . . 366
- Configurations . . . . . 368
- The “Compiling” page . . . . . 369
- Other settings . . . . . 371
- A picture is worth a thousand words . . . . . 371
- The “Linking” page . . . . . 373
- Debugger Source Path . . . . . 374
- Breakpoints . . . . . 375
- Breakpoint properties . . . . . 377
- Relocate the execution point manually . . . . . 378
- Finally, ready to debug . . . . . 379
- Starting the program under the debugger . . . . . 379
- Debugging from a crash point . . . . . 380
- Cannot debug? . . . . . 383
- Fun stuff to do during debugging . . . . . 384
- Hint for geeks . . . . . 386
- Debugging cheat sheet . . . . . 387
18. Remote debugging . . . . . 389
- How does it work? . . . . . 389
- Install PAserver (on the remote machine) . . . . . 390
- Run PAServer (on the remote machine) . . . . . 390
- Configure the environment (locally) . . . . . 391
- Assign a Connection Profile to the Delphi project . . . . . 394
- Choose which files to deploy . . . . . 396
- Deploying the files manually . . . . . 396
- Start debugging . . . . . 396
- Stability . . . . . 397
19. Automatic bug reporting . . . . . 399
- MadShi MadExcept . . . . . 400
- EurekaLog . . . . . 403
20. Advanced debugging techniques . . . . . 405
- How to debug your components/the IDE? . . . . . 405
- Method 1: Attach to process . . . . . 405
- Method 2: Debug with “Run -> Load process”. 406
- Method 3: External tools . . . . . 410
- Method 4: Start Delphi in “safe” mode . . . . . 411
- Stupid Windows 10 . . . . . 411
21. Speed up your program . . . . . 412
- Profiling your program . . . . . 413
- Home-brewed method (as cheap as the home-made beer) . . . . . 413
- Third party profilers (aka non-free) . . . . . 415
- Improving the speed . . . . . 416
- Record field alignment . . . . . 416
- CPU Optimization . . . . . 419
- Compared to Julia . . . . . 420
22. Compiler! Please save my ass! . . . . . 426
- Overflow checking . . . . . 427
- IO checking . . . . . 428
- Range Checking . . . . . 428
- Enabling Runtime Error Checking . . . . . 428
- Assertions . . . . . 429
- Nothing comes for free . . . . . 431
23. Memory management . . . . . 432
- FastMM . . . . . 432
- 1. Dramatic speed increase for memory-bound operations . . . . . 432
- 2. Prevents memory fragmentation . . . . . 432
- 3. Better memory sharing . . . . . 433
- 4. Safer code . . . . . 433
- 5. No more access violations . . . . . 436
- Switching FastMM to Full Debug Mode . . . . . 437
- How to read a FastMM leak report? . . . . . 437
- The summary . . . . . 439
- Leaked blocks . . . . . 439
- Advanced topics . . . . . 442
- You create it, you kill it . . . . . 442
- Memory ownership . . . . . 442
- Dangling references . . . . . 443
- Alice loses her preferred book . . . . . 443
- Getting our dirty paws on a dangling reference . . . . . 444
- The root of all evil . . . . . 446
- There are even worst scenarios . . . . . 447
- Do some noise . . . . . 447
- Bruce Willis (and FastMM) saves the day... again! . . . . . 449
- Garbage collectors . . . . . 450
- About ownership . . . . . 450
- Passing the ownership around . . . . . 451
- Summary . . . . . 453
24. Exceptions . . . . . 454
- Exception Handling . . . . . 456
- Let’s do the “What this code does” game . . . . . 459
- Amateur mistake . . . . . 461
- Keep it real . . . . . 463
- Spit, don't swallow . . . . . 463
- Good boy, Debugger! . . . . . 464
- Ignoring exceptions during debugging . . . . . 465
- Try/Finally vs Try/Except . . . . . 467
- Raising exceptions . . . . . 469
25. Windows Messages in Delphi . . . . . 471
- The Windows Messaging System . . . . . 471
- Message Handlers in Delphi . . . . . 471
- Practical Examples . . . . . 472
26. Programmer’s toolbox . . . . . 474
- Delphi registry secret . . . . . 474
- How to start a “mint” Delphi instance? . . . . . 475
- Total Commander . . . . . 476
- SoftPerfect RAMDisk . . . . . 478
- Version control . . . . . 479
- Do we need a repository? . . . . . 480
- Creating a new Tortoise repository . . . . . 482
- Revisions . . . . . 484
- Resolving Conflicts . . . . . 485
- Commit Progress . . . . . 486
- Icon overlays . . . . . 486
- Shelving . . . . . 486
- Normalizers . . . . . 487
27. Code safety . . . . . 490
- Things that are ok, but dangerous . . . . . 490
- Delphi language . . . . . 491
- “Simply don’t do it” . . . . . 494
- Letting the compiler to help us . . . . . 494
- Extra techniques . . . . . 495
- Program architecture . . . . . 495
- Some details . . . . . 496
- On FreeAndNil . . . . . 497
- A word of warning on Application.ProcessMessages . . . . . 498
28. Advance topics . . . . . 500
- Running a single instance of our program . . . . . 500
- How does it work? . . . . . 500
- Let’s see the code . . . . . 501
- Anonymous methods . . . . . 502
- Capture of Variables . . . . . 503
- Usage . . . . . 503
- Flexible Usage . . . . . 504
- Inline Definitions . . . . . 504
- Analyzing the code above . . . . . 504
. . . . . 29. Migrating 32-bit Code to 64-bit: Best Practices and Considerations . . . . . 506
- Before you start . . . . . 506
- Upgrading Third-Party Libraries . . . . . 506
- Adding 64-bit Support to the Project . . . . . 506
- Fixing the paths . . . . . 506
- Inspecting the code . . . . . 507
- Choosing the correct data types . . . . . 507
- Typecast from pointer to integer . . . . . 508
- Typecast between Record and TObject . . . . . 509
- WinAPI issues . . . . . 509
- SendMessage . . . . . 510
- Vcl.Controls.TControl.Perform . . . . . 510
- SetWindowLong/ GetWindowLong . . . . . 511
- SetWindowLong: . . . . . 511
- Avoid using LongInt . . . . . 512
- Extended type . . . . . 512
- Inline ASM code . . . . . 513
- Functions that return Cardinal instead of Pointer . . . . . 513
- Summary . . . . . 513
30. Deploying our application on other platforms . . . . . 515
- Android . . . . . 515
- Apple . . . . . 515
- Linux . . . . . 516
- Summary . . . . . 516
31. Being a programmer . . . . . 517
32. All good things must come to an end . . . . . 522
- Where do you go from here? . . . . . 522
- Your next big project could be a game . . . . . 522
- About Stack Overflow . . . . . 523
- Fighting bullies on Stack Overflow . . . . . 523
- Ask the artificial intelligence . . . . . 524
- Video tutorials . . . . . 527
- Books . . . . . 527
- My books . . . . . 527
- Already published books: . . . . . 528
- What I have learned in the last 25 years of software development . . . . . 528
33. Appendix . . . . . 532
- Files generated by Delphi . . . . . 532
- Example of ASM code in Pas file: . . . . . 533
- Delphi releases . . . . . 534
- What's new since Delphi 10.0? . . . . . 535
34. Notes and credits . . . . . 539
- Contact me! . . . . . 539
- Disclaimer . . . . . 539
- Source code . . . . . 539
- Be environment responsible . . . . . 539
- Credits . . . . . 539