Tutorial - WRITING YOUR FIRST COMPONENT (Part 2)
12 * When to Override?(4/53)
13 * Component "NewComp" (4/54)
14 * Pre-Install the Component to the palette.(4/55)
15 * "Package" in Delphi (4/56-57)
16 * Start a new project and test Component (4/57)
17 * Assign Event Handler by writing code.(4/58)
18 * Indicate Owner property (4/58-59)
19 * Adding Property to an Object (4/66)
20 * Adding Property to a Class (4/71)
21 * Intercept windows messages to redirect interaction
to Component when necessary.(4/73)
22 * Override essential methods in maintaining Component
status.(4/74)
23 * Adding existing event or create new Event to
interact with users's runtime handler.(4/75)
12 * When to Override?(4/53)
Taking Old Component and Override it to create new component
makes programmer's life easier.
When override you have to add command inherited always which indicate
that this "Object" will start with what it use to do first
as we called "default handler". (Already explained in
part 1).
If Override without inherited in Create Method it will translate
that you are going to create this OBJECT all by yourself. You can
manage by writing approximately 1000 lines or more in order to create
OBJECT and enabled it to store in memory and save with form or "Resource
Stream". (It would be incredibly too much work, unless you
are willing to do that).
|
.....Constructor TNewComp.Create(AnOwnwer:TComponent);
........Begin
........inherited Create(AnOwner);
........End;
.....Destructor TNewComp.Destroy;
........Begin
........inherited Destroy;
........End;
|
|
* Color show relationship.
|
Create Constructor Method = Call to use at design time
if component is drop on form when designing. At the moment we dropped
component create method is call.
Destroy Destructor Method = When programmer deletes component
that was dropped on form. (If we create owner of our component as
"Self" we won't need to FREE it.) You can also write your
own code how you want to destroy your component too. (if you dare).
Back
to TOP
13 * Component "NewComp" (4/54)
NewComp is a Component, which allow you to drop it on form
and enable MOUSE to drag FORM on your screen by clicking anywhere
on FORM. Dragging form is done by instant animation. You should
consider how you would like to control action on MouseMove, including
what you want to modify after you drop your component on form.
Back
to TOP
14 * Pre-Install the Component to the palette.(4/55)
If you are a PRO, you can create your component in one long
writing program till completion then installs your component to
the palette. If you are not a PRO yet, you can gradually create
your component and compile to check your mistake. Recommended you
do the second option by starting to draw "ICON" for your
component first.
Go to MainMenu --->choose TOOLS-->then choose Image
Editor.
Choose -->File---> New --->then DCR (Delphi Component Resource)
Then point to --> CONTENT --->click right -->choose NEW-->then
choose BITMAP.
Program will ask you to---> indicate size (24x24 for 800x600)
(32x32 for 600x480)
Point to -->Bitmap then -->Click right -->choose Rename
(Change name to your CLASS, hence name TNEWCOMP)
Then -->press ENTER --> and then double-click TnewComp to
draw.
Once -->finish your drawing -->save file in DCR (same path
to UNIT program we are using or NewComp.PAS, hence this file will
name NewComp.DCR).
You can create your Component with DESIGN TIME,
RUNTIME PACKAGE by clicking "CHECKBOX" name Build with
Runtime Package when you compile your component.
Back
to TOP
15 * "Package" in Delphi (4/56-57)
- Package - Delphi allow you deposit Package or create
new package by your own preferred name. Example like I can use
JDELPHI Package. Your package will named DPL and DPK and
when install in Delphi it will compile only your package.
- Package in DELPHI called Delphi User's Component allows
user to add his personal select component. This File names DclUsr30.Dpk
in ROOT name Lib. You can put TNewComp in easily. You need
to indicate new package to place TNewComp in.
Go to Mainmenu -->Choose Component -->then choose
Install Component
Choose -->TAB Into New package to -->create new package-->
Fill indetails as below sample
|
Unit File Name
|
Put in NewComp.pas (or choose name
from disk)
|
|
Search path
|
Delphi will indicate path for package NewComp.pas
for you
|
|
Package File Name
|
let just name it here as sample Working
Component
|
|
Package Description
|
Reminder for yourself memo of what
this package do
|
Complete fill in details -->clicks OK. Now you have WORKComponent.DPL
or already complied Component.
After finish Complied then click --> INSTALL (for the
first installation) after install you can just compile as normall
Then Close program window and -->Save Package name
WorkComponent. You will see word NEWCOMP show (alone) in package.
If you look at component palette, you will see your component.
Back
to TOP
16 * Start a new project and test Component(4/57)
We will test compile component as well run program at the same time.
Go to -->MainMenu -->choose File -->choose New Application
to start a new program.
Take Component we just created NEWCOMP drop on FORM.
For testing Compile NEWCOMP to change for any correction
-->choose FILE -->choose ADD to Project. Take program NEWCOMP.Pas
(add) and then compile.
** This is a test method, actually
running program DEBUG will already inspect your component.
Back
to TOP
17 * Assign Event Handler by writing code (4/58)
Programmer normally use Object Inspector to assign event handler
OnClick then do this, OnXXX then do that, but you can not do that
with creating COMPONENT. You have to write CODE
Sample of Code Format (Assign Event Handler)
|
Object Instance.Event := Method;
|
OR
Sample of Code Format (Method with parameter or signature)
|
Object Instance.OnXXX := Method;
|
Since NewComp allow us to drag FORM (animation), then we have to
tell FORM that when there is an event of OnMouseDown Mouse Click
be prepare to be dragged. How can we do that then? See below!
Back
to TOP
18 * Indicate Owner property (4/58-59)
TNewComp has Property type of TComponent name Owner, and an Owner
is a TComponent which is higher than Form, therefore we can modify
Owner to Form. Writting code below.
|
FOldForm := TForm(Owner);
|
FOldForm is an Instance we created with the type
of TForm, above sentence declares that FOldfold is the same form
we had dropped our component. Then we can manipulate (event) with
form as we intend such as OnMouseMove.
FOldForm is in Private section
(see only in this component)
FOldForm := TForm(Owner); refer from FROM, so we don't need
command Create
|
TForm(Owner).OnMouseMove := DoFormMouseMove;
TForm(Owner).OnMouseDown := DoFormMouseDown;
|
OR
|
FOldForm.OnMouseMove := DoFormMouseMove;
FoldForm.OnMouseDown := DoFormMouseDown;
|
Back to our TnewComp include with OnMouseMove and OnMouseDown we
are intending to do now.
|
Type
.....TNewComp = Class(TComponent)
.....Private
.....FOldForm : TForm;
.....{private declarations}
.....Protected
............Procedure DoFormMouseMove
(Sender : TObject; Shift : TShiftState ; X , Y :; Integer);
............Procedure DoFormMouseDown
(Sender : TObject; Button : TMouseButton;
............Shift : TShiftState;
X , Y : Integer);
.....{protected declarations}
.....Public
............Constructor
Create (AOwner : TComponent); Override ;
............Desstructor
Destroy ; Override ;
.....{Public declarations}
.....Published
.....{published declarations}
......End;
........
procedure TNewComp.DoFormMouseMove (Sender : TObject;
Shift:TShiftState; X, Y: Integer);
begin
.............
end;
procedure TNewComp.DoFormMouseDown (Sender : TObject;
Button : TMouseButton;
.................... Shift:TShiftState;
X, Y: Integer);
begin
.............
end;
|
|
* You will see Visibility Section of
Object the 4P (private, protected, public and published) The
story does not end here, please feel free to expand real action
into coding now that you know the main concept and how to
do it with real programmer's rules.
|
Back
to TOP
19 * Adding Property to an Object(4/66)
Let us assume we have some more useful PROCEDURE to work with
such as BreakBill (not just OnMouseMove, OnMOuseDown) so that we
can deal with currency and coins. This procedure will be call for
use by another programmer.
|
Var
........B1,B10,B20,B50,B100,B500,B
1000: LongInt;
........Amounted : Extended;
Procedure BreakBill (Amount:Extended;var S50,S1,B1,B10,B20,B50,B100,B1000);
........Begin
...................
........End;
|
|
* Procedure Name "BreakBill"
send variable one-to-one for each type of money, we call for
uselike this BreakBill (Money, TB50, TB1, ....)
|
Now think money start from big bill, medium, small bills down to
coin of 50 cents, 25 cents, 10 cents, 5 cents (depend on that currency
could be YEN or DONG or POUND). Gradually it will start to get complicate
in remember Procedure by names as well it will take up too many
memory (resources).
It's better to create CLASS and take this PROCEDURE put it in CLASS
|
Type
........TBill = Class (TObject)
........Public
........S50,S1,B1,B10,B20,B50,B100,B1000
: LongInt;
........Procedure BreakBill(Value:Extended);
........End;
|
* When you want to use Object we can refer to it with Instance
VAR
..........Bill : TBill; <this
indicate Instance name TBill>
* Before using Instance we neeed to Create (Refer olf lesson)
Bill : = TBill.Create ; <create
instance name BILL to be used>
Bill . BreakBill (53250.00) ; <Call
method BreakBill to break this amount 53250>
Bill . Free ;
Let's assume we have too many Method to remember by name, so we
moved Method name BreakBill to be at Protected section instead and
adding variable name FAmount to store it's value. Indicating
property name "Amount" in Public section. (compare code
to POINT 19 above).
|
Type
........TBill = Class (TObject)
........Protected
............FAmount : Extended
;
............FS50,FS1,FB1,FB10,FB20,FB50,FB100,FB1000
: LongInt;
............Procedure
BreakBill(Value:Extended);
........Public
............Property Amount
: Extrended Read FAmount
Write BreakBill;
........End;
|
|
This call "Direct Access".
We READ FAmount directly in private area. From now you can
create function such as "function GetAmount" and
READ AMOUNT through function by writting code "Method
name SetAmout"
|
Back
to TOP
20 * Adding Property to a Class(4/71)
If we add PROPERTY in Published section by writing adding CODE,
we need to "Rebuild Package" to enable that Property to
emerge at "Object Inspector" and re-compile your NewComp.
Go to Delphi MainMenu --> Choose --> File|Reopen -->Call
your package WorkComponent.DPK and make changes -->double
click NewComp to make change.
Assume we want to add property of DRAGGING FORM, we can put this
command
|
NewComp1.AllowMoved :=False;
|
You can also indicate this property when design Object Inspector
too.
Object Inspector show when designing, so
its PROPERTY will be in Published Section. If you are add
new property, you won't be able to compile immediately but you will
need to Build Component Library (Rebuild Package). Once you had
Build it, you will see new property emerge in Object Inspector.
|
........Published
............Property AllowMoved
: Boolean Read FAllowMoved Write FAllowMoved
;
........End;
|
Back
to TOP
21 * Intercept windows messages to redirect
interaction to Component when necessary.(4/73)
Window signal is call Iteration Cycle. Delphi can detect this signal
with Message Handling Method or Method that follow by Message Handling
Directive, or special "Constant value" such as WM_CLOSE
= close form) or CM_MOUSELEAVE.
Back
to TOP
22 * Override essential methods in maintaining
Component status.(4/74)
Delphi has many important METHOD for OVERRIDE. If you dislike drawing,
then you can override method name PAINT. If you dislike CLICK, you
can override method name CLICK.
IMPORTANT METHOD to secure and preserve your component to work normally
and effectively as Component are: Notification,
Loaded, SetName, SetParent, GetChildren, GetParentComponent, InsertComponent,
RemoveComponent, FindComponent, Paint
|
Important Method
|
Duty of that method
|
|
Notification
|
Event handler for drog or delete of component from Form
|
|
Loaded
|
Event of component when load with form, what it will do
|
|
SetName
|
When user set name for our component
|
|
SetParent
|
When other component create at the same time with our component,
what will you do
|
|
GetChildren
|
Check our component if there are other control cling to it
or not and how to handle that control.
|
|
GetParentComponent
|
Check if our component cling to other control (Have parent?)
|
|
InsertComponent
|
Take our component drog on Form or put in another TWinControl
|
|
RemoveComponent
|
Take our component OUT from Form or from another TWinControl
|
|
FindComponent
|
Find other component if exist
|
|
Paint
|
If there is attempt to draw our component, how we gonna draw
it.
|
Back
to TOP
23 * Adding existing event or create new Event
to interact with users's runtime handler.(4/75)
Existing events are such as TNotifyEvent, TDataChangeEvent, TKeyEvent
to interact with users.
There are 2 kind of events.
- Propagated Event (divided
into 2 types)
System Event - such as open
and close form, start and close program, or intercept window message,
which send from OSM (operating system message).
User's Generated Event - User
activate action such as keyboard or mouseclick.
- Manufacturing Event is an
Event design by programmer follow from existing event or programmer
creates all new events. It's similar to writing "PROPERTY"
and the different would be that Event you create must have TYPE
DECLARATION.
|
TSomeKindofEvent = Procedure (...Signaturized
Parameter...) of Object
|
|
Type
........TMyOwnEvent = Procedure
(Sender :TObject; X, Y: Integer) of Object ;
........Private
............FMyOwnEvent
: TMyOwnEvent ;
........Pubished
............Property OnMyOwnEvent
:TMyOwnEvent Read FMyOwnEvent Write FMyOwnEvent
;
|
Back
to TOP
|