rapid applications programming with windows week 9
TRANSCRIPT
Topics Week 9
Overview of Delphi 5The Delphi 5 Integrated
Development EnvironmentDelphi ProjectsObject Pascal overviewThe Visual Component LibraryThe Code Editor
Overview of Delphi 5
Developed by Borland International (now INPRISE Corporation)
“The world’s fastest optimizing compiler”Stand-alone executables, with no run-time
libraries required to distribute applicationsFully object-oriented, based on Object PascalSupports COM, ActiveX, OLE, ADO Inherent Database connectivity through the
Borland Database EngineOpen architecture: easily add 3rd party tools
The Delphi 5 IDEAn SDI applicationConforms to W95 GUI standardsDelphi 5 is written in Delphi
everything the application does, you can build into your own application
Use Delphi 5 for building: GUI applications, based on the VCL(Visual
Component Library) DLL’s (compiled code that your app. can call) A Win32 console application (runs in a DOS
box) e.g. for a TCP/IP server, mail server etc
The Delphi 5 IDEMain parts to the IDE
Main Menu bar and toolbars Component palette Object Inspector(Properties tab, Events tab) Form Designer Code Editor Code Explorer Project Manager
Add ActiveX controls to the ActiveX tab Add Tools (e.g. PVCS) via Tools|Configure Tools Configure the IDE via Tools|Environment Options
Projects
When you create an application in Delphi 5 you are creating a Project
For each project, Delphi creates at least: 1 project source file (.DPR)
startup code (see example 1)
1 (main) form unit (.PAS)1 for each formcollection of types, constants, variables and
routines for the form
Projects...
1 (main) form file (.DFM)1 for each formbinary - description of form + its componentsview/change it by right-clicking on the form
1 project configuration file (.cfg) 1 project options file (.dof) 1 project resource file (.res) 1 application file (.exe)
To work on multiple computers, transport only .pas, .dfm and .dpr files.
Projects...Project|Options -> Tabbed dialog
Forms Tab specify startup form (Main Form) specify whether forms are ‘auto-created’
when the application loadsApplications Tab specify Help FileDirectories/Conditionals specify output dir. for system-generated
files specify search path for e.g. library routines
Object Pascal
Pascal(late 1960’s) ->Turbo Pascal (1984)-> Borland Pascal with Objects v 7.1 -> Delphi1(Object Pascal)
A unit is a compiled text file (.DPR, .PAS)Have a pre-defined format (See examples 2,3)The uses List
list of external units that this unit references all of the declarations from the interface section of
the included unit can be used in the current unit/program
comma separates entries and semi-colon ends the list
Object Pascal...
The interface Section specifies identifiers (constants, types, variables,
procedure and function declarations etc) from this unit which other units can access and use
ends with the implementation keyword
The implementation Section contains the actual code for the unit visible only to the unit define here all procedures, functions including those
declared in interface Section ends with the next unit keyword (end or
initialization)
Object Pascal...
The initialization Section code here is automatically run when the unit is
loaded into memory e.g. initialize data structures that are defined in the interface section. Can exist without a finalization Section
The finalization Section code here is executed after the main program
has terminated, just before the unit is unloaded from memory e.g. free resources allocated in initialization section. MUST HAVE an initialization section
Object Pascal...Scoping of VariablesWhere Declared Visibility Lifetimeunit’s interface entire unit where it
applicationsection is declared + all units lifetime that use its unitunit’s only in the unit applicationimplementation where it is declared, from lifetimesection point of declaration to end
of unitwithin a procedure to the end of its function while
functionor function or procedure or proc is
executing
Object Pascal...
Stringsstring defines a long string, dynamically allocated e.g. var
message : string; DOES NOT have a 0 element, is not an array of characters terminated
with a null (as in C, C++) use e.g. message.Length, message.SetLength(..) Many Windows functions require a character array parameter, as they
are written in C, C++ Use the Pascal PChar type for a character array
Object Pascal...
The Windows MessageBox functionvar
text : string;caption : string;
begintext := ‘This is a test.’;caption := ‘Test Message’;MessageBox(0, PChar(text), PChar(caption), 0); ORMessageBox(0, PChar(text), ‘Test Message’, 0);
end;
Object Pascal...
Check out String manipulation functions, procedures e.g. Copy, Delete, Format, IntToStr, StrToInt,
StrToXXX(Currency, Date, Time value), XXXToStr, Trim, UpperCase, LowerCase etc (Example 4(b)
If statements (See code example 4(a))Loops
for, while (See Code Example 5)
case Statement (Example 6) expression being tested must evaluate to a
Pascal basic data type e.g. Integer, Byte
Object Pascal...Functionsfunction Multiply(var X : Integer; const Y : Integer) :
Integer;begin
Result := X * Y;X := 10;
end;
Proceduresprocedure SayHello;begin
MessageDlg(‘Hello There!’ , mtInformation, [mbOk], 0);
end; {Use ShowMessage for simple message + OK}
Object Pascal...
Functions and Proceduresprocedure TForm1.Button1Click(Sender: TObject);var
answer : Integer;begin
answer := Multiply(10,20);Label1.Caption := IntToStr(answer);SayHello;
end; Parameters Types are Value, Constant (const)
and Reference (var)
The Visual Component Library (VCL)
An Application Framework “A collection of classes that simplifies
programming in Windows by encapsulating often-used programming techniques” Reisdorph
“Encapsulation means taking a complex programming task and making it easier by providing a simplified interface” Reisdorph
Also known as a class library Used by both Delphi and C++ Builder
The VCL
Is a component-based architecture imposes standard interfaces on the objects all components inherit from TComponent share (e.g.)
ability to be added to lists with other components
standard ways of interacting with each other on forms, in containers
standard ways of setting, retrieving properties public interface of components consists of
properties, methods, events (Check out the VCL source code!)
The VCL...
Does not support multiple inheritanceTObject is the base class for all classesTComponent is the base class for all
components ability to be installed on the component
palette ability to be dropped on a form at design time ability to have its properties set through the
object inspector and component editorsTControl is the base class for all visual controls
Creating Applications
The Object Repository Provides predefined objects for you to use Wizards : Application, Dialog, database
forms, reports, charts etc create new components, packages, ActiveX add components you have created, to the
Repository e.g. password form automatically displayed from File|New you can Copy (default for New Page),
Inherit or Use the selected component
Creating Applications
Two-way interaction between design tools changes made in the Form Designer are automatically
written into the unit code, and vice versa
Try This! Place a button on a form With the button selected, cut it off the form (Ctrl-X) Select the Code Editor, scroll to the end of the code,
then Paste (Ctrl-V). Code is exactly what appears in the .DFM file for the button
Change some property values Cut the code from the editor, Paste it back on the form.
Creating Applications...
The Project Manager window A Project in Delphi is “a collection of files that
work together to create a stand-alone executable file or DLL”
A Project Group can be used forworking on >1 related project concurrentlywork on a DLL and a test EXE for the DLL
concurrently Provides a treeview of all the project components Context menus (right-click) for each level: Project
Group, Project, Unit, File
Creating Applications...
Project Explorer (previously the Object Browser) Hierarchical view of project’s Classes, Units and
Globals Automatically updated as you type
The To-Do List two-way action between to-do list items, and
‘special’ comments in code To-Do list window automatically collects to-do
information from source code as they are typed Can use Copy As to display the To-Do list as a
HTML table or text, then save it from there
Creating Applications…The Component Palette
Set of pages (tabs) containing all the controls available for your Delphi version - add more controls, customise display
Standard: menus, scrollbar, list, combo, memo. radiogroup
Additional: chart, checklistbox, image, grids, bitbtn Win32: Tabs, progress, status bar, calendar, animate System: Timer, paintbox, mediaplayer, OLEcontainer Internet: Sockets, Finger, FTP, HTMLviewer, Email Data Access/Data Controls: Datasource, Table, Query,
Database, DBGrid, DBNavigator, DBLookupCombo Qreport: Components for report-building Dialogs, Samples, ActiveXcontrols, Win3.11
Creating Applications...
The Code Editor Can open multiple files - one tab for each If the unit file (.pas) contains a form, Delphi will
open the form as well Invoke Delphi Help by positioning cursor over any
Object Pascal keyword insert code templates (e.g. for case, array
declarations etc) with Ctrl-J create and insert your own templates via Code
Insight page of Environment Options dialog box. Use for e.g. header information for each unit
The Code Editor... Tooltip Symbol Insight provides info about any
identifier: type, where declared etc Ctrl-cursor placement highlights an identifier, then
click jumps to the code where it is declared Code completion Class completion
type any properties, method names in the interface section, then Ctrl+Shift+C will automatically add method bodies to the implementation section
Code Explorer: easy navigation of types, variables, routines, plus units in uses statements; context menu allows insertion of methods, variables.