100% found this document useful (7 votes)
27 views52 pages

Delphi Cookbook 50 hands on recipes to master the power of Delphi for cross platform and mobile development on Windows Mac OS X Android and iOS 1st Edition Daniele Teti download

The Delphi Cookbook offers 50 hands-on recipes for mastering Delphi in cross-platform and mobile development across Windows, Mac OS X, Android, and iOS. Authored by Daniele Teti, it covers essential topics such as Delphi basics, language features, FireMonkey for UI design, multithreading, and server-side applications. The book is published by Packt Publishing and aims to enhance the skills of developers in utilizing Delphi effectively.

Uploaded by

basamotallys
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (7 votes)
27 views52 pages

Delphi Cookbook 50 hands on recipes to master the power of Delphi for cross platform and mobile development on Windows Mac OS X Android and iOS 1st Edition Daniele Teti download

The Delphi Cookbook offers 50 hands-on recipes for mastering Delphi in cross-platform and mobile development across Windows, Mac OS X, Android, and iOS. Authored by Daniele Teti, it covers essential topics such as Delphi basics, language features, FireMonkey for UI design, multithreading, and server-side applications. The book is published by Packt Publishing and aims to enhance the skills of developers in utilizing Delphi effectively.

Uploaded by

basamotallys
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

Delphi Cookbook 50 hands on recipes to master

the power of Delphi for cross platform and


mobile development on Windows Mac OS X Android
and iOS 1st Edition Daniele Teti pdf download
https://ebookname.com/product/delphi-cookbook-50-hands-on-
recipes-to-master-the-power-of-delphi-for-cross-platform-and-
mobile-development-on-windows-mac-os-x-android-and-ios-1st-
edition-daniele-teti/

Get Instant Ebook Downloads – Browse at https://ebookname.com


Instant digital products (PDF, ePub, MOBI) available
Download now and explore formats that suit you...

Mac OS X and iOS Internals To the Apple s Core 1st


Edition Jonathan Levin

https://ebookname.com/product/mac-os-x-and-ios-internals-to-the-
apple-s-core-1st-edition-jonathan-levin/

iOS and OS X Network Programming Cookbook 1st Edition


Jon Hoffman

https://ebookname.com/product/ios-and-os-x-network-programming-
cookbook-1st-edition-jon-hoffman/

Mac OS X Power Tools 1st Edition Dan Frakes

https://ebookname.com/product/mac-os-x-power-tools-1st-edition-
dan-frakes/

The 100 Most Influential World Leaders of all Time 1st


Edition Mckenna A

https://ebookname.com/product/the-100-most-influential-world-
leaders-of-all-time-1st-edition-mckenna-a/
Scientific Opportunities with a Rare Isotope Facility
in the United States National Research Council

https://ebookname.com/product/scientific-opportunities-with-a-
rare-isotope-facility-in-the-united-states-national-research-
council/

Riding the Wind with Liezi New Perspectives on the


Daoist Classic Ronnie Littlejohn

https://ebookname.com/product/riding-the-wind-with-liezi-new-
perspectives-on-the-daoist-classic-ronnie-littlejohn/

Corpora and Discourse The Challenges of Different


Settings Studies in Corpus Linguistics Volume 31 31st
Edition Annelie Ädel (Editor)

https://ebookname.com/product/corpora-and-discourse-the-
challenges-of-different-settings-studies-in-corpus-linguistics-
volume-31-31st-edition-annelie-adel-editor/

The Experimental Darkroom 1st Edition Christina


Anderson

https://ebookname.com/product/the-experimental-darkroom-1st-
edition-christina-anderson/

The Little League Hitter s Journal 1st Edition Mark


Gola

https://ebookname.com/product/the-little-league-hitter-s-
journal-1st-edition-mark-gola/
Make The Annotated Build It Yourself Science Laboratory
Build Over 200 Pieces of Science Equipment 1st Edition
Barrett Raymond

https://ebookname.com/product/make-the-annotated-build-it-
yourself-science-laboratory-build-over-200-pieces-of-science-
equipment-1st-edition-barrett-raymond/
www.it-ebooks.info
www.it-ebooks.info
Delphi Cookbook

50 hands-on recipes to master the power of Delphi for


cross-platform and mobile development on Windows,
Mac OS X, Android, and iOS

Daniele Teti

[PACKT]
PUBLISHING

B I R M I N G HAM - M U M BAI

www.it-ebooks.info
Del p h i Cookbook

Copyright© 2014 Packt Publishing

All rights reserved . No pa rt of this book may be reprod uced , stored i n a retrieva l syste m ,
or tra nsm itted i n a ny form or by any mea ns, without t h e prior written perm ission of the
pu blisher, except i n the case of brief q u otations em bedded i n critica l a rticles or reviews.

Every effort has been made in the prepa ration of this book to ensure the accu racy of the
i nformation presented . However, the i nformation conta i ned in this book is sold without
wa rra nty, either express or i m p l ied . Neither the author, nor Packt Publishi ng, and its
dealers and d istributors wi l l be held l iable fo r a ny d a mages ca used or a l l eged to be
ca used d i rectly or i n d i rectly by this book.

Packt Publ ish i ng has end eavored to provide trademark i nfo rmation about all of the
com pa n ies and prod ucts mentioned in this book by the appropri ate use of ca pita ls.
However, Packt Publishing can n ot gua ra ntee the accu racy of this i nformati o n .

Fi rst p u b l ished : Septe m ber 2014

Prod uction reference: 1190914

Publ ished by Packt Publ ish ing Ltd .


Livery Place
35 Livery Street
B i r m i ngham B3 2PB, U K.

ISBN 978-1-78355-958-9

www . packtpub . com

Cover i mage by J u n aid Shah (j una i dshahl l l@gma i l . com)

www.it-ebooks.info
Credits

Author Copy Editors

Daniele Teti Roshni Banerjee

Adithi Shetty
Reviewers

Eric Van Feggelen Project Coordinator

Sherwin John Leena Purkait

Olivier Pons
Proofreaders
Jorge H. Rodriguez
Bridget Braund

Commissioning Editor Paul Hindle

Sam Birch Amy Johnson

Acquisition Editor Indexers

Richard Harvey Monica Ajmera Mehta

Tejal Soni
Content Development Editors

Balaji Naidu Graphics

Pooja Nair Valentina D'silva

Abhinash Sahu
Technical Editors

Mrunal Chavan Production Coordinators

Dennis John Aparna Bhagat

Edwin Moses Manu Joseph

Cover Work

Aparna Bhagat

www.it-ebooks.info
About the Author

Daniele Teti is a software a rch itect, tra i ner, and consu lta nt with over 18 yea rs of
professional experience. He writes code in a n u m ber of languages but h is preferred
la nguage to com p i l e native software is Object Pasca l .

Daniele i s a wel l-known Del phi a n d progra m m ing expert i n the developer com m u n ity. He's
the m a i n developer and d rives the development of some Del phi open sou rce projects
(Del phi MVCFramework; DO R M , "the Del phi ORM"; Del phi Red is Client; and so on). He wrote his
fi rst program when he was 11 years old, and si nce then, he happily conti n ues to write software
a l m ost every day. Apa rt from Del p h i , he's a huge fa n of design patterns, expert systems, RESTfu l
a rchitectu res, and And roid OS. When he is not busy writi ng software or progra m m ing (as h is
job or hobby), he l i kes to play the gu itar, write songs, and do vol u nta ry activities. Cu rrently, he
works as an R&D Di rector & Ed ucational at bit Ti me Software (www . bi t t ime . i t), an Italian
representative of Embarcadero Technologies (www embarcade ro . com). He recently beca me
.

the CEO of bit Time Professiona ls, which is a spi n-off com pa ny of bit Ti me Software; this
com pany specializes i n consultancy, tra i n i ng, and development.

Bei ng a softwa re a rch itect, consu lta nt, and teacher for m a ny Ita l i a n and E u ropean com p a n i es,
he travels very often a ro u n d the world . He is the Tec h n ica l Di rector of ITDevCon , the biggest
Europea n Del p h i conference (www . i tdevcon . i t ) . He's a lso a n i nternational spea ker at
m a ny tec h n ical conferences.

Daniele l ives in Rome, Ita ly (where each photogra pher becomes an a rtist) with h is beloved
wife, Debora, and their l ittle boy, Mattia.

www.it-ebooks.info
About the Reviewers

Eric van Feggelen is a passionate a n d experienced softwa re consu lta nt who delivers
h igh-q ual ity sol utions usi ng the latest tech nologies ava i l a ble. He has a bout 15 yea rs of
experience as a developer and has been i nterested in i nformation technology h is enti re l ife.
I n the past few yea rs, he wo rked for major corporations, such as M icrosoft a n d Ava nade
a n d conti n u es to serve the Microsoft Enterprise space as a private contractor for h is own
com pa ny. At the time of writi ng th is book, Eric has worked as a lead d eveloper for a Microsoft
Dyn a m i cs sta rt-up.

I n 2013, Eric reviewed Mastering Windows 8 C++ App Development, Packt Publishing.

Olivier Pons is a developer who's been bu i l d i ng websites si nce 1997. He is a teacher at


l n geS u p ( Ecole S u p e rie u re d ' l nge n ierie l nformatique http : / /www . i ngesup . com/ and
-

http : / /www . y - nov . com), at the U n ive rsity of Sciences (I UT) i n Aix-en-Provence, France.
At Ecole d ' l n ge n i e u rs d es M i nes d e Gardan ne, he teaches state-of-the-a rt web tec h n i q ues,
such as MVC fu ndamenta ls, Symfony, Word Press, P H P, HTM L, CSS, jQuery/jQuery Mobile,
Node.js, Angu l a rJS, Apache, Li n ux basics, a n d adva n ced Vi m tec h n i q ues. He has a l ready
worked as a tec h n ical reviewer for Ext JS 4 First Look, jQuery Hotshot, jQuery Mobile Web
Development Essentials, WordPress Complete, and jQuery 2.0 for Designers Beginner's
Guide Second Edition. All these books were publ ished by Packt Publishi ng. I n 2011, he
l eft h is fu l l-ti me job as a Del p h i and PH P d eveloper to concentrate on h is own compa ny,
H Q F Development (http : / /hqf . f r) . He cu rrently runs a n u m ber of websites i n c l u d i ng
http : / /www . bat t l e s oop . f r, http : / /www . krys t a l l opol i s . f r / (wh ich wi l l be
released soon), http : I /www . l i vrep i z z a s . f r, http : I /www . papdevi s . f r, and
http : / / o l ivi e rpons . f r (his own web d evelopment blog) . He a lso works as a consu lta nt,
teacher, and project m a nager, a n d so meti m es, helps big compan ies as a senior / h ighly
skil led d eveloper.

www.it-ebooks.info
Jorge H. Rodriguez has a backgro u n d i n softwa re development and more than 20 yea rs
of experience under his belt, m a ny of them working with Del p h i .

Always on t h e lookout for new and exciti ng technologies, Jorge l ives to code and spends m uch
of h is spare ti me rea d i ng techn ical books and playi ng o n l i n e chess. D u ring wi nters, he l i kes to
go snowboa rd i ng with his o n ly so n , Ca m i lo.

Jorge resides i n Va ncouver, Ca nada, awa iti ng his Colombian gi rlfriend, Shana. He can be
contacted at delphi . deve l ope r@shaw . ca.

www.it-ebooks.info
www.PacktPub.com

Support files, eBooks, discount offers, and more


You m ight wa nt to visit www . Packt Pub . com for support fi les and down loads related to
you r book.

Did you know that Packt offers e Book ve rsions of every book p u b l ished, with PDF and ePub
fi les ava i l a ble? You ca n u pgrade to the eBook version at www . Packt Pub . com and as a print
book customer, you a re entitled to a d isco u nt on the eBook copy. Get i n touch with us at
s e rv i ce®packtpub . com for more deta i ls.

At www . Packt Pub . com, you ca n a lso read a col l ection of free tec h n ical a rticles, sign up
for a ra nge of free newsletters and receive excl usive d iscou nts and offers on Packt books
and eBooks.

[dJ PACKTLi
http : / / Packt Lib . Packt Pub . com

Do yo u need i nsta nt so l utions to you r IT q u estions? Packtlib is Packt's o n l i n e d igita l book


l i bra ry. Here, you ca n access, read and sea rch across Packt's enti re l i bra ry of books.

Why subscribe?
"' Fu l ly sea rchable across every book publ ished by Packt
"' Copy a n d paste, pri nt and bookmark content
"' On demand and accessible via web browser

Free access for Packt account holders


If you have an acco u nt with Packt at www . Packt Pub . com, you can use this to access
Packtl i b today and view n i n e enti rely free books. S i m ply use you r logi n cred enti a ls for
i m med iate access.

www.it-ebooks.info
www.it-ebooks.info
Table of Contents
�fu� 1
Chapter 1: Delphi Basics 7
Introduction 7
Changing your application's look and feel with VCL styles and no code 8
Changing the style of your VCL application at runtime 10
Customizing TDBGrid 14
Using the owner's draw combos and listboxes 20
Creating a stack of embedded forms 23
Manipulating JSON 26
Manipulating and transforming XML documents 32
1/0 in the twenty-first century - knowing streams 38
Putting your VCL application in the tray 42
Creating a Windows service 48
Associating a file extension with your application on Windows 53

Chapter 2: Become a Delphi Language Ninja 59


Introduction 59
Fun with anonymous methods - using higher-order functions 59
Writing enumerable types 64
RTII to the rescue - configuring your class at runtime 68
Duck typing using RTII 72
Creating helpers for your classes 76
Checking strings with regular expressions 84

Chapter 3: Going Cross Platform with FireMonkey 91


Introduction 91
Giving a new appearance to the standard FireMonkey
controls using styles 92
Creating a styled TListBox 98

www.it-ebooks.info
Table of Contents -------

Impressing your clients with animations 102


Using master/details with LiveBindings 105
Showing complex vector shapes using paths 116
Using FireMonkey in a VCL application 122

Chapter 4: The Thousand Faces of Multithreading 129


Introduction 129
Synchronizing shared resources with TMonitor 129
Talking with the main thread using a thread-safe queue 137
Synchronizing multiple threads using TEvent 140
Displaying a measure on a 2D graph like an oscilloscope 143

Chapter 5: Putting Delphi on the Server 147


Introduction 147
Web client JavaScript application with WebBroker on the server 148
Converting a console service application to a Windows service 157
Serializing a dataset to JSON and back 160
Serializing objects to JSON and back using RTII 165
Sending a POST HTIP request encoding parameters 171
Implementing a RESTful interface using WebBroker 174
Controlling remote applications using UDP 190
Using App Tethering to create a companion app 197
Creating DataSnap Apache modules 203

Chapter 6: Riding the Mobile Revolution with FireMonkey 213


Introduction 213
Taking a photo, applying effects, and sharing it 214
Using listview to show and search local data 222
Do not block the main thread! 227
Using SQLite databases to handle a to-do list 234
Using a styled TListView to handle a long list of data 239
Taking a photo and location and sending it to a server continuously 247
Talking to the backend 257
Making a phone call from your app! 264
Tracking the application's life cycle 269

---[]]f-----�

www.it-ebooks.info
------ Table of Contents

Chapter 7: Using Specific Platform Features 275


Introduction 27 5
Using Android SOK Java classes 276
Using iOS Objective-C SOK classes 282
Displaying PDF files in your app 286
Sending Android intents 291
Letting your phone talk - using the Android TextToSpeech engine 300

Index 305

--------�O!D-

www.it-ebooks.info
www.it-ebooks.info
Preface
If you've been a softwa re developer for a long time, you certa i n ly know how usefu l a
conversation ca n be with a col l eague who a l ready d i d so m eth i ng s i m i l a r to what you a re
doi ng, and can d iscuss it as he/she may have faced the sa me proble m . It is not possible to
i n c l u d e a l l the possible situati ons that a developer may face i n a book, but most problems a re
si m i la r at least i n pri nciple. This is the reason this book is orga n ized as a cookbook; j ust l i ke
how a com bi nation of foods can be ada pted and mod ified to be appropriate for d iffe rent types
of meals. A progra m m i ng reci pe ca n provide the idea to solve m a ny d ifferent problems.

This book is a n adva nced-level gu ide that will help Del p h i developers become experts i n the i r
every day j o b . T h e every day job and t h e q u a l ity o f you r del iverables is what contri bute to the
q u a l ity of you r professional l ife. It does not m a ke sense to rei nvent the wheel repeated ly,
espec i a l ly when working with a well-esta bl ished tool such as Del p h i . The focus of this book is
to provide readers with com p rehensive a n d deta i led exa m ples on how effective ly the Del p h i
softwa re can b e d esigned and writte n . Al l t h e reci pes i n t h i s book a re a resu lt o f yea rs of
d evelopment, tra i n i ng, a n d consu lta ncy activities i n the most d ifferent fields of the IT i n d ustry,
from s m a l l systems with thousa nds of i nsta l l ations to la rge systems com m issioned by a ny big
com pa ny or govern ment. It is not a magic book that wi l l solve all you r development problems
(if you fi nd it, tel l m e please!), but it can be a va l i d sou rce of help to get a d ifferent point of
view on a specific problem, or a h i nt on how to solve problems.

Armed with the knowledge of advanced concepts, such as high-order fu nctions and
a nonym ous m ethods, generics and e n u merables, extended RTTI and d uck typ i ng,
Live B i n d i ngs, m u ltith rea d i ng, Fire M o n key, mobile developm ent, server-side d evelopment
and m a ny more, yo u w i l l be pleasantly su rprised as to how q u ickly and easily yo u ca n use
Del p h i to write h igh q u a l ity, clea n , readable, m a i nta i n a ble, and extensible code.

I have read too many boring progra m m i ng books, so I tried to m a i nta i n a relaxed a n d l ight
expositio n . A s m a l l appl icabi l ity scenario, which descri bes a situation where a parti c u l a r
technology, approach , or d esign pattern ca n b e used successfu l ly, i ntrod uces a l l t h e reci pes.
The reci pes a re not too complex beca use otherwise the book may consist of thousa nds of
pages; however, it is a lso not trivia l beca use the IT books landscape is a l ready fu l l of trivial
exa m p les with a few d i rect a p p l i cations. I tried to do a good trade-off and hope I succeed ed .

www.it-ebooks.info
Every time I sta rt to read a new book, I ask myself, "Wi l l the a uthor have someth ing i nteresti ng
to say?", " H ow m uch wi l l this book cha nge my poi nt of view on the topics it ta l ks about?",
or "Wi l l it be worth the time to read this book?". Now that I'm on the other side of the river, I
worked hard to put as m uch good q u a l ity content as possible i n this book, which I hope w i l l
match you r expectations.

On a fi nal note, writi ng h u n d reds of pages a bout adva nced progra m m i ng is not a n easy task.
However, I a m very pleased to have done it, and I hope yo u wi l l enjoy read i ng it as m u ch as I
enj oyed writi ng it.

What this book covers


Chapter 1, Delphi Basics, ta l ks about a set of genera l approaches that should not be ignored
by a ny Del p h i progra m mer. Some reci pes a re s i m ple, wh i l e some a re not, but a l l of them
should be deeply understood . By the end of this cha pter, yo u wi l l be a ble to use some of the
fu ndamenta l Del p h i tech n i q u es related to the RTL, VCL, and OS i ntegratio n .

Chapter 2 , Become a Delphi Language Ninja, focuses on t h e Obj ect Pasca l language. A
progra m m i ng language is the way yo u ta l k to the mach i ne, so you m ust be fl uent a n d should
know a l l the possibil ities offered . This chapter tal ks about h igher-order fu nctions, practical
uti l i zation of the extended RTII , regu lar expressions, and other th i ngs usefu l to a ugment the
power of you r code and lowe r the a m o u nt of time spent on debuggi ng.

Chapter 3, Going Cross Platform with FireMonkey, is ded icated to the Fire M o n key fra m ework
in genera l . What you wi l l learn from this chapter can be used in m a ny of the platforms
Fire M o n key su pports. Moreover, you w i l l learn about nontrivia l Live B i n d i ngs uti l i zations.

Chapter 4, The Thousand Faces of Multithreading, is one of the most com p l ex chapte rs. It
ta l ks a bout thread syn c h ro n i zation and the mechan isms used to obta i n this synchronization,
i n c l u d i ng TMon itor, thread-safe q u eues, a n d TEvent. By the end of this cha pter, yo u w i l l be
able to create a n d com m u n icate with backgro u n d th reads, leaving the m a i n th read free to
u pdate yo u r G U I (or com m u n icate with the OS) .

Chapter 5, Putting Delphi on the Server, focuses on how wel l Del p h i can behave when ru n n i ng
on a server. Some people th i n k that Del p h i is a client-on ly tool , but it is not true; the n u m ber
of Del p h i server-side systems ru n n i ng all over the world prove it! In this cha pter, we' l l show
how to create powerfu l servers that offer services over a netwo rk. Then, i n one of the reci pes,
we' l l a lso i m plement a JavaScript cl ient that bri ngs the database data to the user's browser.
The tec h n i q ues exp lai ned i n this chapter open a ra nge of possibi l ities, especia l ly in the mobile
and web a rea.

www.it-ebooks.info
Chapter 6, Riding the Mobile Revolution with FireMonkey, is dedicated to mobile development
with Del p h i and Fire M o n key. If you a re i nte rested in mobile developm ent, I th i n k that this wi l l
b e you r favorite chapter! Mobiles a re everywhere and this chapte r wi l l exp l a i n how to write
softwa re for you r And roid or iOS d evice, what a re the best practices to use, how to save you r
data on you r mobile device, how to retrieve a n d u pdate remote data, a n d how to i ntegrate with
a mobile operati ng system .

Chapter 7 , Using Specific Platform Features, shows you how to i ntegrate yo u r a p p with the
u n d erlyi ng mobile operati ng systems beyond what Fire M o n key offers. You wi l l learn how to
i m port Java a n d Objective-C l i bra ries i n you r app and use the S O K classes from you r Object
Pasca l cod e.

What you need for this book


This book ta l ks about Del p h i , so you need Del p h i . Not a l l reci pes a re ava i lable i n a l l the Del p h i
ed itions. Typical ly, t h e m o b i l e projects ca n b e com p i led o n ly i f you have Del p h i Enterp rise or
better (or Del p h i Professional p l us the mobile add-on, or RAD Stud i o professional, or better).
All the projects a re com p i led and tested on Del p h i XE6. Many of the reci pes can also be
com p i l ed on older versions.

If you wa nt to ru n the mobile app on a phone or ta blet, you can use the And roid e m u l ator or
iOS si m u l ator, but it is strongly reco m m ended that you use a n actu a l device to see how the
app rea l ly behaves. To deploy a n iOS app on you r device, yo u a lso need a n Apple com p uter
with Mac OS X. More i nformation is provided i n the related cha pters.

Who this book is for


This book a i ms to h e l p professional Del p h i developers i n their day-to-day jobs. This book wi l l
teach you a bout the newest Del p h i technologies and its hidden gems. I t i s not a book for a
newbie, but the practical approach wi l l help you reach a new leve l i n you r Del p h i ski l ls. An
experienced d eveloper wi l l benefit from th is book beca use nontrivial problems a re solved
using the best practices. Where more than one way is ava i lable or the topics are too vast to be
exp lai ned i n the ava i lable pages, references a re provided that a l low i nterested readers
to go deeper i n that field . It is a book that' l l hold on to you r desk for the next few yea rs.

Conventions
I n th is book, you wi l l fi nd a n u m ber of styles of text that d isti ngu ish between d ifferent kinds of
i nformatio n . Here a re some exa m ples of these styles, and an explanation of their mea n i ng.

Code words in text, database table names, folder names, fi lena mes, fi le extensions, path names,
d u m my U R Ls, user i n p ut, and Twitter handles are shown as follows: "The TStyl eManager .
S tyl eName s property conta i ns a l l na mes of the avai lable styles."

www.it-ebooks.info
A block of code is set as follows:

LogMe s sage ( ' Your me s sage goe s here for SUCCES S ' ,
EVENTLOG_SUCCES S , 0 , 1 ) ;

When we wish to d raw yo u r attention to a particular part of a code block, the releva nt l i nes or
items a re set i n bol d :

begin
App l i ca t i on . In i t i a l i z e ;
App l i ca t i on . MainFormOnTaskbar : = True ;
T S tyl eManager . TryS e t S tyl e ( ' Iceberg C l a s s i co ' ) ;
App l i ca t i on . Create Form ( TMainForm , Ma inForm) ;
App l i ca t i on . Run ;
end .

Any com mand-l i ne i n put or output is written as fol l ows:

C : \<ExeProjec tPath>\Windows S e rvi c e . exe /ins t a l l

C : \<ExeProjec tPath>\Windows S ervi c eOrGUI . exe /GUI

New terms and important words a re shown i n bol d . Words that you see on the screen , i n
m e n us or d ialog boxes for exa m ple, a p pear i n the text l i ke this: "Cl ick on Start, wa it for the
confi rmation, and the service should sta rt to write its logfi le."

[� Warnings or important notes appear in a box like this.


]
[ --R '-'()
Tips and tricks appear like this.
]
Reader feedback
Feed back from o u r readers is a lways welcome. Let us know what you th i n k about this
book-what you l i ked or may have d is l i ked . Reader feed back is i m porta nt for us to develop
titles that you rea l ly get the most out of.

To send us genera l feedback, s i m ply send an e-m a i l to feedback@packtpub . com,


a n d mention the book title via the subject of you r m essage.

If there is a topic that you have expertise i n and you a re i nterested i n either writi ng or
contri buti ng to a book, see o u r a uthor gu ide on www . packtpub . com/ authors.

www.it-ebooks.info
Customer support
Now that you a re the proud owner of a Packt book, we have a n u m ber of th i ngs to h e l p you
to get the most from yo u r p u rchase.

Downloading the example code


You ca n down load the exa m ple code fi les for a l l Packt books yo u have p u rchased from
you r acco u nt at http : / /www . packtpub . com. If you p u rchased this book elsewhere, you
can visit http : I /www . packtpub . com/ support and register to have the fi les e-ma i led
d i rectly to you .

Errata
Although we have taken every ca re to ensure the accu racy of o u r content, m ista kes do
happen . If you fi nd a m ista ke i n one of o u r books-maybe a m ista ke i n the text or the
cod e-we wo u l d be gratefu l if you wou l d report this to us. By doing so, you can save other
readers from frustration and help us i m p rove subseq uent versions of this book. If you fi nd
a ny errata, please report them by visiti ng http : I /www . packtpub . com/ submi t - e rrata,
selecti ng you r book, clicking on the errata submission form l i n k, a n d entering the deta i ls
of you r errata . Once you r errata a re ve rified, you r subm ission wi l l be accepted a n d the errata
w i l l be u p l oaded on o u r website, or added to a ny list of existing errata, under the Errata
section of that title. Any existi ng errata can be viewed by selecti ng you r title from
http : / /www . packtpub . com/ support.

Piracy
Pi racy of copyright material on the I nternet is an ongoing problem across a l l m ed i a . At Packt,
we ta ke the protection of o u r copyright a n d l i censes very seriously. If you come across a ny
i l l egal copies of o u r works, i n a ny form, on the I nte rnet, please provide us with the location
add ress or website name i m med iately so that we can p u rsue a remedy.

Please contact us at copyr ight@packtpub . com with a l i n k to the suspected


pi rated materia l .

We appreciate you r h e l p i n protecting o u r a uthors, and our abil ity to bring you va l uable content.

Questions
You ca n contact us at que s t i ons@packtpub . com if you are having a problem with a ny
aspect of the book, and we wi l l do o u r best to add ress it.

www.it-ebooks.info
www.it-ebooks.info
1
Delphi Basics

I n th is cha pter, we wi l l cover the fol lowi ng reci pes:

.,. Changi n g you r a p p l i cation's look and feel with VCL styles and no code
.,. Changi n g the style of you r VCL appl ication at ru nti m e
.,. Custom izing TDBGrid
.,. Using the owner's d raw com bos and l istboxes
.,. Creating a stack of em bedded forms
.,. M a n i p u lati ng JSON
.,. M a n i p u lati ng a n d tra nsform ing XM L docu ments
.,. 1/0 i n the twenty-first centu ry - knowing streams
.,. Putti ng you r VCL appl ication i n the tray
.,. Creating a Windows service
.,. Associati ng a fi le extension with you r a p p l ication on Windows

I ntrod uct i o n
This chapte r expl a i ns some of the day-to-day needs of a Del p h i progra m mer. These a re
ready-to-use reci pes that w i l l be usefu l every day and have been selected a head of a lot of
others beca use a lthough they may be obvious for some expe rienced users, they a re sti l l very
usefu l . Even if there is no specifica l ly data base-related code, m a ny of the reci pes can a lso be
used (or someti m es especially used) when you a re dea l i ng with data .

www.it-ebooks.info
Delphi Basics ----

Chang i ng you r a p p l i cati o n 's l ook and feel


with VC L styl es and no code
VCL styles a re a major new entry i n t h e latest versions o f Del p h i . They have been i ntrod uced
in Del p h i XE2 and are sti l l one of the less-known featu res for the good old Del p h i developers.
However, as usua l , some busi nessmen say looks matter, so the look and feel of you r
appl ication cou l d b e o n e of t h e reasons to choose you r prod uct over o n e from a com petitor.
Consider that with a few mouse clicks you can apply m a ny d iffe rent styles to you r appl ication
to cha nge the look and feel of you r a p p l i cations. So why not give it a try?

Getting ready

VCL styles ca n be used to reva m p a n old appl ication or to create a new one with a
nonsta n d a rd G U I . VCL styles are a com p l etely d ifferent beast to Fire M o n key styles.
They a re both styles but with completely d ifferent approaches and behavior.

To get started with VCL styles, we' l l use a new appl icati o n . Let's create a new VCL a p p l ication
and d rag-a nd-d rop som e components onto the m a i n form (for exa m ple, two TButton
components, one TListBox component, one TCom boBox component, and a couple of
TCheckBox components) .

T h e fo l l owing screenshot is t h e resu lta nt form that r u n s on a Windows 7 machine:

I'm• sad not styled form ...

I Button! ]I Button I J Label 1


ComboBoxl

D Check!loxl
D Check!loxl

A form without style

How to do it...

Now we've to apply a set of n ice styles. To do this, perform the fol lowing ste ps:

1. Navigate to Project I Options. In the resu lta nt d i a log, go to Application I


Appearance and select a l l the styles that we wa nt to i n c l u d e in o u r a p p l icati o n .

www.it-ebooks.info
2. Using the Preview button, the I D E shows a s i m p l e d e m o fo r m with some controls, a n d
we c a n get a n idea about the fi nal resu lt o f o u r styled form . Feel free to experiment
and choose the style-or set of styles-that you l i ke. O n ly one style wi l l be used at a
ti me, but we can l i n k the necessa ry resou rces to the executa ble and select the proper
one at ru nti me.
3. After selecti ng a l l the req u i red styles from the l ist, we've to select one i n the
com bobox at the bottom of the screen . This style wi l l be the d efa u lt style for our form
and wi l l be loaded as soon as the a p p l ication sta rts. You can delay this choice and
m a ke it at ru nti me usi ng code if you prefer.
4. Click on OK and h it F9 (or navigate to Run I Run) and you r appl ication is styl ed ! The
resu ltant form is shown i n the fol lowi ng screenshot:

Labell

(comb oBox 1 ...,. I


Check8ox1

CheckBox1

The sa me form as the preced i n g one but with the Iceberg Classico style a p p l ied

How it works...

Sel ecti ng one or more styles by navigati ng to Project I Options I Application I Appearance
can ca use the Del p h i l i n ker to l i n k the style resou rce to you r executa ble. It is possible to l i n k
m a ny styles to yo u r executa ble, but you c a n use o n ly o n e style a t ti me. So, how does Del p h i
know w h i c h style you want t o use w h e n there are more t h a n one styles? I f w e check the
Proj e c t fi le (the fi le with the . dpr extension) by navigating to Project I View Sou rce,
you can see where and how this l ittle magic ha ppens.

The fo l l owing l i nes a re the i nteresting part:

begin
App l i ca t i on . In i t i a l i z e ;
App l i ca t i on . MainFormOnTaskbar : = True ;
T S tyl eManager . TryS e t S tyl e ( ' Iceberg C l a s s i co ' ) ;
App l i ca t i on . Create Form ( TMainForm , Ma inForm) ;
App l i ca t i on . Run ;
end .

www.it-ebooks.info
Delphi Basics ----

When we've selected the Iceberg Classico style as the defa u lt style, the Del p h i I D E adds
a line j ust before the creation of the main form , setting the defa u lt style for the appl ication
using the T S tyl eManage r . TryS e t S tyle static method .

T S tyl eManager is a very i m porta nt class when dea l i ng with VCL styles. We' l l see more about
it i n the next rec i pe when we'l l learn how to cha nge a style at ru nti me.

There's more...

Del p h i and C++ Bui lder XE6 com e with 29 VCL styles ava i lable in c : \ Program F i l e s
( x8 6 ) \ Embarcadero \ S tudio\ 1 4 . O \Redi s t \ s tyl e s \vcl \ (with a sta ndard i nsta l lation).

M oreover, it is possible to create you r own styles or mod ify the existing ones by usi ng the
Bitmap Style Designer avai lable at Tools I Bitmap Style Designer m en u . The Bitma p Style
Designer a lso provides test a p p l i cations to test VCL styles.

For more deta i ls on how to create or custom ize a VCL styl e, check the fol lowing l i n k:

http : / / docwi ki . embarcade ro . com/RADS tudio/XE 6 / en/Creat i ng_a_S tyle_


u s ing_the_B i tmap_S tyle_Des igne r

Chang i ng the style of you r VC L a p p l i cati o n


a t ru n t i m e

VCL styles a re a powerfu l way to cha nge the a p pea ra nce of you r appl icatio n , but using them
o n ly as design-ti me tools is way too l i m ited . One of the m a i n featu res of a VCL style is the
a b i l ity to cha nge the style w h i l e a n appl ication is ru n n i ng.

Downloading the example code


You can download the example code files for all Packt books you have
purchased from your account at http : I /www . packtpub . com. If you
purchased this book elsewhere, you can visit http : I /www . packtpub .
com/ support and register to have the files e-mailed directly to you.

Getting ready

Beca use a VCL style is s i m ply a particu l a r kind of binary fi le, we can a l l ow o u r users to load
the i r preferred style at ru nti me, and we can even provide new styles-pu blish i ng them on a
website or sen d i n g them by an e-ma i l to o u r customers.

In th is rec i pe, we' l l be able to cha nge the style w h i l e a n a p p l ication is ru n n i ng using a style
a l ready l i n ked at design time or let the user choose between a set of styles deployed i nside
a folder.

www.it-ebooks.info
How to do it...

Styles m a n i p u lation at ru nti me is done using the class methods of the TStyl eManager class:

1. Create a brand new VCL appl ication and add the Vc l . Theme s and Vc l . S tyl e s
u n its to the m a i n i m plementation form . These u n its a re req u i red to use VCL styles
at ru nti me.
2. Drop on the form a TListBox component, two TButton com ponents, and two
TOpenDialog components. Leave the defa u lt component names.
3. Go to Project I Appearance a n d select eight styles of you r choice from the l ist. Leave
the Default style option to Windows.
4. The T S tyl eManage r . Styl eName s property conta i ns all names of the ava i l a ble
styles. In the FormCreate event h a n d ler, we have to load the a l ready l i n ked styles
present in the executa ble to the l istbox to let the user choose one of them . So, create
a new proced u re cal led S tyl e sL i s tRe f re s h with the fol lowi ng code and ca l l it
from the FormCreate event hand ler:
proc edure TMa inForm . S tyl e s L i stRe f resh ;
var
styl ename : s t ring ;
begin
L i s tBoxl . Clear ;
II retrieve all the styles linked in the executable
for styl ename in T S tyl eManager . S tyl eName s do
begin
L i s tBoxl . I t ems . Add ( styl ename ) ;
end ;
end ;

5. I n t h e B u t tonl C l i ck event hand ler, we've t o set t h e cu rrent style accord i ng to the
one selected from L i s tBoxl using the fol lowing cod e:
TStyl eManager . S etStyle ( L i s tBoxl . I t ems [ L i s tBoxl . I t emindex] ) ;

6. The But ton2 C l i ck event handler should a l low the user to sel ect a style from
d isk. So, we have to create a folder named s tyl e s at level of o u r executa ble and
copy a few . vs f fi les from the defa u lt style d i rectory which is c : \ Program F i l e s
( x8 6 ) \ Embarcadero \ S tudi o \ 1 4 . O \Redi s t \ s tyl e s \vcl \ i n RAD Stud i o XE6.
7. After copyi ng the fi les, write the fol lowi ng code u n d e r the But ton2 C l i ck event
h a n d ler. This code a l l ows the user to chose a style fi le d i rectly from the d isk. Then you
can select one of the loaded styles from the l istbox a n d click on Button1 to a p ply it to
the appl icati o n . The code is as follows:
if OpenD i a l og l . Execute then
begin
if TStyl eManage r . I sVa l i dStyl e ( OpenD i a l og l . F i l eName ) then

www.it-ebooks.info
Other documents randomly have
different content
heavier burden upon his back, and he invented a Model in
pack or frame on which he could carry things on his National
back (Fig. 1) after the manner of one of our modern Museum.)
pack peddlers.

In the course of time man tamed one or more


of the wild beasts which roamed near him. Then
the burden was shifted from the back of a man to
the back of a beast. The first beast of burden in
South America was the llama; in India it was the
elephant; in Arabia it was the camel (Fig. 2). In
Europe and in parts of Asia and in Egypt the horse
FIG. 2.—A SHIP OF first became man's burden bearer and the nations
THE DESERT. which had the services of this swift and strong
animal outstripped the other nations of the world.
"Which is the most useful of animals?" asked one Egyptian god of
another. "The horse," was the reply, "because the horse enables a
man to overtake and slay his enemy."

FIG. 3.—A CART WITHOUT


WHEELS.
(From a Model in the
National Museum.)

It is often easier to drag a thing along than it is to carry it. This


fact led to the invention of what we may call the first and simplest
form of carriage. This was the drag or travail (tra-vay´), a cart
without wheels (Fig. 3). Two long saplings were fastened at the large
end to the strap across the horse's breast and the small end upon
which the burden was placed dragged upon the ground. Mr. Arthur
Mitchell in his delightful book, "The Past in the Present," tells us that
he saw carts of this kind in actual use in the highlands of Scotland as
late as 1864! An improvement upon the travail was the sledge made
of the forked limb of a tree (Fig. 4). This primitive sledge was really a
travail consisting of one piece.

FIG. 4.—A PRIMITIVE SLEDGE.


(From a Model in
National Museum.)

In many
cases it is
easier to roll a
thing than it is
FIG. 5.—THE FIRST CART.
to drag it. This
fact led to
another step in the development of the FIG. 6.—HAULING TOBACCO.
carriage; it led from the cart without (From a Model in
wheels to a cart with a wheel—a most National Museum.)
important step in the history of inventions.
The first wheeled cart was simply a log from each end of which
projected an axle (Fig. 5). The axle fitted in the holes of a frame
upon which the body of the cart was placed and to which the horse
or the ox was attached. As the cart moved along, wheel (log and
axle) turned together. The very ancient method of moving a load by
rolling it along was in use in the United States not so very long ago.
As late as 1860 in some of the southern States hogsheads of tobacco
(Fig. 6) were rolled over country roads in the manner just described
and as late as 1880 the fishermen of Nantucket used as a fish cart a
vehicle that had only a barrel for its wheel. (Fig. 7.) The common
wheel-barrow and the one-wheeled carts which are still used in China
and Japan had their origin in the rolling log.
FIG. 7.—A NANTUCKET FISH
CART.
(From a Model in the
National Museum.)

We are told
by some writers
that the rolling
log (the one-
wheeled cart)
FIG. 8.—A CART WITH was followed by FIG. 9.—CART WITH A SOLID
WHEEL.
WHEELS AND AXLE IN ONE the two-wheeled
PIECE. cart, on which
the wheels were the ends of a log and the
axle was the middle portion of the log hewn down to a proper size
(Fig. 8). Here wheels and axle turned together precisely like a
modern car wheel. This makes a very pretty story but I am afraid the
solid two-wheeled affair represented in Figure 8 is only imaginary,
and that in a true account of the development of the cart it has no
place. The true beginning of the two-wheeled cart may be learned
from Figure 9. Here the wheels are two very short logs through the
center of which are holes in which the round ends (axles) of a piece
of timber (the axle-tree) fit. When the cart moves, the wheels turn
upon the axle. The one-wheeled cart had at first one log turning with
the axle; the two-wheeled cart at first had as its wheels two very
short logs turning on the axles.
FIG. 10.—CART WITH WHEEL
PARTLY SOLID.
(From a Model in the
National Museum.)

The first
two-wheeled
carts were a
FIG. 11.—WHEELS WITH great
FIG. 12.—AN ANCIENT
SPOKES. improvement EGYPTIAN CHARIOT SHOWING
(From National upon the HUB, SPOKES, FELLY AND RIB.
Museum.)) single rolling (From National
log, yet they Museum.)
were exceedingly heavy and clumsy. The
trouble was with the wheel. This was very thick and with the
exception of the hole in which the axle went it was entirely solid.
Wheelwrights at a very early date saw that the problem was to make
the wheel light and at the same time to keep it strong. Little by little
this problem was solved. At first crescent-shaped holes were made in
the wheel (Fig. 10). This made the wheel lighter, but did not weaken
it. In its next form the wheel was even less solid than before. It now
consisted of four curved pieces of wood (Fig. 11) held together by
four spokes. In this wheel there was a hub, but the spokes were not
inserted in it; they were fastened about it. In the Egyptian chariot
(Fig. 12) we find the wheel in the last stage of its interesting and
remarkable development. Here the spokes, six in number, are
inserted in the hub from which they radiate to the six pieces of the
felly or inner rim. Around the felly is the outer rim or tire made of
wood and fastened to the felly with thongs. The wheel of to-day has
more iron in it, and has more spokes and is lighter and stronger than
the old Egyptian wheel, yet in its main features it is made like it.

A light running two-wheeled


carriage was used by all the civilized
nations of the ancient world. Three
thousand years ago in the great and
wicked city of Nineveh chariots raced up
and down the paved streets "jostling
against one another in the broad ways, FIG. 13.—WONDERFUL ONE HOSS
SHAY.
with the crack of the whip, the rattle of
(From National Museum.)
the wheel and the prancing of horses."
The chariot played an important part in
the life of the Greeks and Romans, in their racing contests and in
their wars, and throughout the Middle Ages it was the only vehicle in
general use in Europe. As time passed it was of course made lighter
and stronger and better. The doctor's gig so charmingly described by
Holmes in his "Wonderful One Hoss Shay" may be taken as an
illustration of the full development of the two-wheeled carriage
(Fig. 13).

Bring the hind part of one Egyptian chariot


opposite to the hind part of another, lash the two
chariots together, remove the tongue of one of
the chariots and you have made a chariot of four
wheels or a coach. The form of the most ancient
FIG. 14.—AN ANCIENT of four-wheeled carriages leads to the belief that
ROMAN CHARIOT. the coach was first made by joining together two
two-wheeled chariots in
the way just described. The ancient Egyptians
had their four-wheeled chariots but only their
gods and their kings had the privilege of
riding in them. For centuries none but the
great and the powerful rode in coaches. The FIG. 15.—A COACH OF
Roman chariot (Fig. 14), bad imitations of THE MIDDLE AGES.
which we see nowadays in circus processions,
was used only in the splendid triumphal processions which entered
Rome after a great victory. In the Middle Ages we get a glimpse of a
four-wheeled carriage now and then, but usually the king or a queen
is lounging in it (Fig. 15). The coach could not be generally used in
Europe in medieval times because the roads were so bad. The
excellent roads made by the Romans had not been kept in good
condition. Traveling had to be done either on horseback or in the
two-wheeled carriage. In 1550 there were but three coaches in Paris
and in London there was but one. In 1564, however, we find Queen
Elizabeth riding in a coach (Fig. 16) on her way to see her lover, Lord
Leicester. Insert more spokes and lighter ones in the wheels of this
coach of the queen's, put on rubber tires and mount the body on
elliptical springs17 and we will have the coach of to-day.

FIG. 16.—QUEEN ELIZABETH'S COACH.


THE CARRIAGE
Continued

FIG. 1.—NEWTON'S STEAM


CARRIAGE, 1680.

I n the last chapter the story of the


Carriage was brought up to the reign of
Queen Elizabeth of England. In the century
following Elizabeth's reign a new and most
remarkable step in the development of the
carriage was taken. You remember that in
the seventeenth century there was a great
deal of experimenting with steam (p. 58). FIG. 2.—CUGNOT'S STEAM
Among other experiments was one made CARRIAGE, 1769.

by Sir Isaac Newton. This great philosopher


tried in 1680 to make a steam-carriage, or locomotive, as we call it.
Figure 1 shows the principle upon which he tried to make his carriage
work. The steam was to react against the air, as in the case of Hero's
engine (p. 56) and thus push the carriage along. Newton's
experiment was not satisfactory but the idea of a steam-carriage was
now in men's heads and the hope of making one continued to be
cherished. In 1769 Cugnot, a French army officer, invented a steam-
carriage of three wheels (Fig. 2) but it was a very poor one. It
traveled only three or four miles an hour, it could carry but three
persons, and it had to stop every ten minutes to get up steam.
Cugnot, however, deserves to be ranked among the great inventors
for he showed that a steam-engine could be attached to a carriage
and could push it along. In other words he showed that steam could
be used for transportation as well as for working pumps and turning
the wheels of factories. And that was just what was needed most in
the latter part of the eighteenth century. Man needed assistance in
traveling; he especially needed help in carrying things from place to
place. The steam-engine was keeping the mines dry and making it
possible to mine great quantities of coal and was turning the wheels
of great factories where the spinning-jenny and the new power loom
(p. 119) were consuming enormous quantities of cotton and wool.
Now if the steam-engine could also be made to carry the coal and
cotton and wool to the factory, and the manufactured products from
the factory to the market, the industrial revolution would be complete
indeed.

Inventors everywhere put their wits together to construct an


engine that would draw a load. The great Watt tried to make one,
but having failed, he came to the conclusion that the steam-engine
could do good work only when standing still. Among those who
entered the contest was Richard Trevithick, a Cornish miner, born in
1771. Trevithick when a lad at school was able to work six examples
in arithmetic while his teacher worked one. He proved to be as quick
in mechanics as he was in mathematics. He began his experiments
with steam when a mere boy, and as early as 1796 he had built a
steam-locomotive which would run on a table. By 1801 he had
constructed a steam-carriage (Fig. 7). Three years later (1804)
Trevithick exhibited a locomotive which carried ten tons of iron,
seventy men, and five wagons a distance of nine and one-half miles
at the rate of five miles an hour. This was the first steam carriage
that actually performed useful work. The honor of inventing the first
successful locomotive, therefore, belongs to Richard Trevithick,
although he never received the honor that was due him.
The honor
went to George
Stephenson, of
Wylam, near FIG. 4.—THE "BEST FRIEND."
Newcastle, THE FIRST LOCOMOTIVE
BUILT FOR ACTUAL SERVICE
England. IN THE UNITED STATES.
FIG. 3.—STEVENSON'S Stephenson's
LOCOMOTIVE, 1828. parents were so poor that they could not
afford to send him to school long enough
for him to learn to read and write. In his eighteenth year, however,
he attended a night school and learned something of the common
branches. In his childhood Stephenson lived among steam-engines.
He began as an engine boy in a colliery and was soon promoted to
the position of fireman. At an early age he was trying to build the
locomotive that the world needed so badly, one that would do good
work at a small cost. Trevithick's locomotive was too expensive.
Stephenson wanted a locomotive that would pay its owner a profit.
At the age of thirty-three he had solved his problem. In 1814 he
exhibited a locomotive that would run ten or twelve miles an hour
and carry passengers and freight cheaper than horses could carry
them. Eleven years later he was operating a railroad between
Stockton and Darlington, England. The steam carriage was now a
success (Fig. 3). The iron horse was soon transporting passengers
and freight in all the civilized countries of the world (Fig. 4). Observe
that the first passenger car was simply the old coach joined to a
locomotive.

The locomotive worked wonders in travel and


in carrying loads, yet men were not satisfied with
it. We never are satisfied with our means of
transportation. No matter how comfortably or FIG. 5.—A TROLLEY
cheaply or fast we may travel we always want CAR.
something better. In the latter part of the
nineteenth century the great cities of the world were becoming over-
crowded. The people could not be carried from one part of a city to
another without great discomfort. The street cars drawn by horses
could not carry the crowds and the elevated steam cars were not
satisfactory. Wits were set to work to relieve the situation and about
thirty years ago the electric car (Fig. 5) was invented. Without horse
or locomotive this quick-moving car not only successfully handles the
crowds which move about the city but it also relieves over-crowding
by enabling thousands to reach conveniently and cheaply their
suburban homes. It also does the work of the steam car and carries
passengers long distances from city to city.

FIG. 6.—A HORSELESS


CARRIAGE OF THE SIXTEENTH
CENTURY.

A late
development in
carriage
making is seen
in the
automobile. As FIG. 8.—GOOD-BY TO THE
FIG. 7.—THE FIRST far back as the HORSE.
AUTOMOBILE. sixteenth
century a horseless carriage was invented
(Fig. 6) and was operated on the streets of a German city. But here
the power was furnished by human muscle. The first real automobile
(Fig. 7) was invented in 1801, by the man who invented the first
successful locomotive. Trevithick's road locomotive—for that is what
an automobile really is—did not work well because the roads upon
which he tried it were in very bad condition. Inventors after
Trevithick for a long time paid but little attention to the road
locomotive; they bestowed their best thought upon the locomotive
that was to be run upon rails—the railroad locomotive. In recent
years, however, they have been working on the so-called automobile
and they have already given us a horseless carriage that can run on
a railless road at a rate as great as that of the fastest railroad
locomotives. To what extent is this newest of carriages likely to be
used? It is already driving out the horse. Will it also drive out the
electric car and the railroad locomotive? Are we coming to the time
when the railroad will be no more and when all travel and all hauling
of freight will be done by carriages and wagons without horses on
roads without rails? The answers to these questions can of course
only be guessed.

The last and latest form of the


carriage is seen in the flying-machine, the
automobile of the air. In all ages men
have watched with envy the movements
of birds and have dreamed of flying-
machines, but only in modern times has
man dared to take wings and glide in
bird-like fashion through the air. The first
actual flying by a human being was done
by a Frenchman named Bresnier, who, in
FIG. 9.—SOME UN­SUCCESS­FUL
1675, constructed a machine similar to FLY­ING MACHINES OF A
that shown in the right hand picture at HUNDRED YEARS AGO.
the top of Figure 9. Bresnier worked his
wings with his feet and hands. Once he jumped from a second story
window and flew over the roof of a cottage. From the days of
Bresnier on to the present time man has taxed his wits to the utmost
to conquer the air, and in his efforts to do this he has invented
almost every conceivable kind of machine. About the middle of the
nineteenth century inventors began to apply steam to the flying-
machine, and it is said that in 1842 a man named Philips was able,
by the aid of revolving fans driven by steam, to elevate a machine to
a considerable distance and fly across two fields. In 1896 Professor
Langley, with a flying-machine driven by a small steam-engine, made
three flights of about three-fourths of a mile each over the Potomac
River, near Washington. This was the first time a flying-machine was
propelled a long distance by its own power; it was the first aerial
automobile. But the aerial steam carriage was never a success; the
steam engine was too heavy. In the early years of the twentieth
century inventors began to use the light gasoline engine to drive
their flying-machines and then real progress in the art of flying
began, and so great has been that progress that the automobiles of
the air are becoming rivals of those on the land.

FIG. 10.—A SUCCESSFUL FLYING MACHINE OF TO-DAY.


THE BOAT

FIG. 1.—THE FIRST BOAT.

A t first, when a man wanted to cross a deep


stream, he was compelled to swim across.
But man at his best is a poor swimmer, and it
was not long before he invented a better
method of traveling on water. A log drifting in a FIG. 2.—THE
INVENTION OF THE
stream furnished the hint. By resting his body CANOE.
upon the log and plashing with his hands and
feet he found he could move along faster and easier. Thus the log
was the first boat and the human arm was the first oar. Experience
soon taught our primitive boatman to get on top of the log and
paddle along, using the limb of a tree for an oar (Fig. 1). But the
round log would turn with the least provocation and its passenger
suffered many unceremonious duckings. So the boatman made his
log flat on top. It now floated better and did not turn over so easily.
Then the log was made hollow, either by burning (Fig. 2), or by
means of a cutting instrument. Thus the canoe was invented. Very
often if the nature of the tree permitted it, the log was stripped of its
bark, and this bark was used as a canoe.
The canoe was
one of the earliest of
boats, but it is not in
line with the later
growth. The ancestry
of the modern boat
begins with the log
and is traced through FIG. 4.—A PRIMITIVE
OARLOCK.
FIG. 3.—THE RAFT—
the raft rather than
SHOW­ING ALSO through the canoe. By
EARLY USE OF THE lashing together several logs it was found that
SAIL.
larger burdens could be carried. Therefore the
boat of a single log grew into one of several
logs—a raft (Fig. 3). By the time man had learned to make a raft he
had learned something else: he had learned to row his boat along by
pulling at an oar instead of pushing it along with a paddle. But in
order to row there must be something against which the oar may
rest; so the oarlock (Fig. 4) was invented. Rafts were used by nearly
all the nations of antiquity. Herodotus, the father of history, tells us
that they were in use in ancient Chaldea. In Figure 3 we have a kind
of raft that may still be seen on some of the rivers of South America.
Here a most important step in boat-building has been taken. A sail
has been hoisted and one of the forces of nature has been bidden to
assist man in moving his boat along.

The raft was bound to develop into the large boat. The central
log was used as a keel and about this was built a boat of the desired
shape and size. Stout timbers, called ribs, slanted from the keel, and
on the ribs were fastened planks running lengthwise with the vessel.
To keep out the water the seams between the planks were filled with
pitch or wax. Thus the raft grew into a large spoon-shaped vessel
(Fig. 5). The early boat was usually propelled by oars, although a
single sail sometimes invoked the assistance of the wind. It had no
rudder and no deck, and if there was an anchor it was only a heavy
stone.
FIG. 5.—"THUS THE RAFT
GREW INTO A LARGE,
SPOON-SHAPED VESSEL."

In the early history of the boat there was no


such thing as a rudder. The oarsman had to steer his
craft as best he could. With the appearance of larger
boats, however, a steersman comes into view. He
steers by means of a paddle held over the stern of
FIG. 6.—THE PO­‐ the boat. Within historic times, probably about the
SI­TION OF THE time of Homer (1100 B. C.), the rudder appears as an
RUDDER IN AN­‐
CIENT TIMES.
oar with a broad blade protruding through a hole in
the side of the boat well to the stern (Fig. 6).
Throughout the whole period of ancient history boats were steered
by rudders of this kind.

The anchor came later than the rudder.


Of course even in primitive times there were
methods of securing the vessel to the
ground under water but they were very
FIG. 7.—ANCIENT
crude. Sometimes a sack of sand was used ANCHORS.
as an anchor, sometimes a log of wood
covered with lead was thrown overboard to hold the boat in its place.
In Homer's time the anchor was a bent rod with a single fluke. About
600 B. C. Anacharsis, one of the seven wise men of Greece, gave a
practical turn to his wisdom and invented an anchor with two flukes
(Fig. 7). The invention received the name of "anchor" from the name
of the inventor.
It was in the Mediterranean Sea that the
boat had its most rapid development. As early
as we can get a glimpse of that wonderful body
of water it was alive with boats (called galleys)
FIG. 8.—A ROMAN that had well-laid keels and lofty sides, and
GALLEY OF ONE TIER
OF OARS, INTRO­DUC­‐
rudders, and sails. The greatest of the earlier
ING THE RUD­DER. navigators were the Phœnicians whose boats
had traversed 5,000
years ago the whole course of the
Mediterranean and had even ventured
beyond the Straits of Gibraltar. The
ancient Greeks also were a great sea-
going people, and their merchantmen or
trading boats visited every part of the FIG. 9.—A ROMAN GALLEY
WITH THREE BANKS OF OARS.
known world. But it was the Romans who
at last became masters of the ancient
seas. The Roman galley, therefore, may be taken as the
representative boat of ancient times. What kind of a boat was the
Roman galley? It was propelled chiefly by oars, just as nearly all the
boats of antiquity were. Occasionally a sail was hoisted when the
wind was favorable but the main reliance was the rower's arm. Men
had not yet learned to use the sail to the best advantage. The older
galleys had one row of oarsmen (Fig. 8), but as the struggle for the
mastery of the sea became keener the boats were made larger and
more rowers were necessary. Galleys with two and three, and even
four rows of oarsmen were built by the Roman navy. When there was
more than one row of oars the rowers sat on benches one above
another. The oarsmen were slaves or prisoners captured in war, and
their life was most wretched.18 They were chained to the benches on
which they sat, and were compelled to row as long as a spark of life
was left. Sometimes they dipped their oars to the music of the flute,
but more often it was to the crack of the lash. Figure 9 shows us how
the Roman galley looked when Rome was at the height of her power
(100 A. D.). Here is a vessel about 400 feet long and about 50 feet
across its deck, a part of the boat, by the by, which was not to be
seen in the earlier galleys. The boat is a trireme, that is, it has
openings for three tiers of oars, and it is propelled by several
hundred oarsmen. For steering purposes it has four stout paddles,
two on each side near the stern. Two masts instead of one carry the
sail which, considering the size of the boat, would seem to be
insufficient. This galley of the first century of our era represents the
full development of the boat in ancient times.

After the downfall of Rome (476 A. D.) it


was a long time before there was any real
progress in boat-making. The glimpses we get
now and then of vessels in the Middle Ages
almost make us feel that boat-building was
going backward rather than forward. But such
was not the case. The ship in which William of
Normandy sailed (Fig. 10) when he crossed
FIG. 10.—THE SHIP IN
over the Channel to give battle to Harold (1066
WHICH WILLIAM THE A. D.) was not so impressive as a Roman galley,
CONQUEROR yet it was, nevertheless, a better boat. In the
CROSSED THE
CHANNEL IN 1066.
first place William's boat was a better sailer; it
relied more upon the force of the wind and
less upon the oar. In the second place, it could be steered better, for
the rudder had found its way to its proper place and was worked by
a tiller. Finally, the shape of the Norman boat fitted it for fiercer
battles with the waves.

If we should pass from the


English Channel to the Adriatic we
should find that boat-making had
undergone the same changes. A
Mediterranean galley of the
fourteenth century (Fig. 11) shows
fewer oars and more sails. Instead of
three rows of oars and two sails as FIG. 11.—A MEDI­TER­RA­NE­AN GALLEY
on the Roman galley, there are three OF THE 14TH CEN­TURY.

sails and one row of oars. This was


the tendency of the boat-builder in the Middle Ages; he crowded on
the sail and took off the rowers. A war-boat of the sixteenth century
(Fig. 12) shows that the last row of oarsmen has disappeared.

FIG. 12.—A WAR-BOAT OF THE


16TH CENT­TURY, SHOW­ING
THAT THE LAST ROW OF OARS
HAD DIS­APPEARED.

About the middle of the thirteenth century there


began to appear on the decks of vessels almost
everywhere in Europe, a little instrument that is of
the greatest importance in the history of the boat.
This was the mariner's compass. The use of the
magnetic needle was known in China (Fig. 13) a
thousand years before it was known to the
Europeans, but in this, as in many other instances, FIG. 13.—A
the Chinese did not profit by their knowledge. Sailors CHINESE
COMPASS. AS
have always sailed at night by the North star; but
THE CART
before the use of the compass was understood they MOVED THE
could little more than guess their way when the night HUMAN
FIGURE IN
was dark and the stars could not be seen. With a FRONT
mariner's needle on board they can tell the direction ALWAYS
they are going no matter how dark the night. We can POINT­ED
NORTH.
easily understand that sailors prized very highly the
discovery of the compass. With the appearance of
this faithful guide they became bolder and bolder and were soon
venturing out upon the trackless expanse of the ocean. It was the
compass that led to the discovery of the new world, for without it no
sailor could have held his course due west long enough to reach the
American coast.

After men had learned to carry their burdens on the broad back
of the ocean, boat-building took on new life. All the great nations of
Europe wanted a share in the new world that had just been found;
but no nation could hope to profit greatly by the discovery of
Columbus if its vessels were not swift and strong. So there arose a
grim contest for the mastery of the Atlantic, just as in ancient times
there had been a struggle for the mastery of the Mediterranean.
Spain, France, Portugal, Holland and England all joined in the battle.
When we see the kind of boats she sent out upon the oceans we are
not surprised that England won. Compare the heavy, angular galley
of the first century with the graceful ship of the sixteenth century
and we see at once the progress the boat made in the Middle Ages
(Fig. 14).

The log, the raft, the galley, the sailing-ship,


these were the steps in the development of the
boat up to the end of the seventeenth century.
In the eighteenth century another step was
taken. You remember that in that century
inventors were everywhere trying to make a
FIG. 14.—THE GREAT steam carriage. They were at the same time
HARRY.
trying to make a steam boat. Their efforts to use
steam to drive boats were rewarded with success
earlier than were their efforts to use it to draw carriages. This was to
be expected. Boat-building has always moved along faster than
carriage-building. Men were gliding about in well-built canoes before
they had even the clumsiest of carts. The Londoners who gazed with
admiration upon the Great Harry as it sailed on the Thames, had
never seen as much as a lumbering coach. And so with the
steamboat; it had crossed the Atlantic before the locomotive could
carry passengers from one town to the next.
France, England, Germany and
America were all eager to have the first
steamboat. In this race America won,
although France and England came out
with their colors flying. As far back as 1663
the Marquis of Worcester, of whom we
have heard before (p. 59), described a
vessel that could be moved by steam: "It FIG. 15.—THE MARQUIS OF
roweth," he said, "it draweth, it driveth (if JOUFFROY'S STEAMBOAT,
1783.
needs be) to pass London bridge against
the stream at low water." It was one thing,
however, to describe a steamboat, and quite another thing to make
one. Worcester's steam-vessel existed only in the imagination of the
inventor. Denys Papin, who did so much for the steam-engine, fitted
out a boat with revolving paddles which were turned by horses. This
was nothing new. The ancient Roman galley was sometimes
propelled by paddle-wheels turned by horses or oxen. It is
sometimes claimed that Papin turned the paddle-wheels of his boat
by means of steam, but there are no grounds for the claim. If France
wants the honor of having made the first steamboat she would do
better to turn from Papin and look to Marquis of Jouffroy of Lyons,
This nobleman, it is claimed, built a steamboat (Fig. 15) which made
a successful trip on the river Soane, in the year 1783, before a
multitude of witnesses. This claim may or may not be just. It may be
as the French say: the boat after the trial trip may have been taken
to pieces, the model may have been lost and the French Revolution
may have swallowed up those who witnessed the trip.

About the time the Frenchman is said to have been


experimenting with his steamboat on the Soane similar experiments
were being tried in many other places. In the latter part of the
eighteenth century the idea of a steam-propelled boat seemed to be
in the air. An English poet of the time was bold enough to prophesy:
Soon shall thy arm, Unconquered Steam, afar
Drag the slow barge and draw the rapid car,
Or on wide, waving wings, expanded bear
The flying chariot through the fields of air.

For the most part the prophesy has been fulfilled, although the
steam flying-machine is not yet an accomplished fact. Among those
who helped to make good the words of the poet was James Rumsey,
of Sheppardtown, Virginia. Rumsey in 1786 propelled, by means of
steam, a boat on the Potomac River moving at the rate of five miles
an hour. It is almost certain that this was the first boat ever drawn by
steam. How did Rumsey drive his boat? A piston in a cylinder was
worked by a steam-engine. When the piston was raised it brought
water in and when it was pushed down it forced the water out
behind and the reaction of the jet pushed the boat along. A
remarkable revival of a very ancient idea! Just as Hero turned his
globe by reaction, just as Newton pushed the first steam carriage
along by reaction, so Rumsey pushed the first steamboat along by
reaction.

If you will look on a map of the United States and observe the
vast network of waterways which come to the different parts of the
country you will understand how important a subject steam
navigation must have been to the people of America in the latter part
of the eighteenth century. Here was a tract of land containing
millions upon millions of fertile acres, but it lacked good roads, and
without roads it could not be developed. It was, however, traversed
by thousands of miles of excellent water-roads and it was plain that if
steamboats could be put upon these rivers the gain would be
incalculable. The most pressing need of the time, therefore, was a
steamboat. No one saw this more clearly than John Fitch. This
talented but eccentric man served his country in the Revolution, and
after the war was over roamed hither and thither for several years as
a soldier of fortune. About 1785 he went to Philadelphia with a plan
for a steamboat. He organized a company, and secured enough
money to enable him to carry out his plans. His boat was ready by
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookname.com

You might also like