Next, I have to do a component. I think a counter component is relatively simple, but this is not the focus of this chapter. The focus of this chapter is to explain the production process of a component. In it, many components can be learned, of course, these are also learned from the book. Ok, start making it:
This is a component that can be counted. For simple, I am in seconds, when he starts, he starts from 0, and it is displayed, and it returns to 0, so it is cycled. Of course, it can also stop, suspend, continue. In addition, he has a time incident, you can set up a few times to trigger this event, this is a good feature, we can set half an hour to trigger the event, come to a sound in the event handler, then sleep I feel that after half an hour, I will remind you to get up.
At the beginning, we couldn't create a new component unit, but to apply it as a class in an engineering, which is easier to debug, so we create a new project, and create a blank unit to place this class.
First, determine the parent class:
Next, you are called TTIMECount, then what is its parent class, it is to be displayed, you have a capacity to let him show, TPANEL is a good choice, I saw the source code, I found that TPANEL is also Didn't do it, just grate its parent TCUSTompanel's property (this is waiting for this), TCUSTompanel declares a lot of attributes as protected, provides a choice for his subclasses, and his subclasses If you want these properties As seen in the object viewer, you can re-declary in the public, if you don't want it, you don't have to pay attention to him. And our counter components don't have to have so many attributes, just right. So I decided to use TcusomPanel as the parent class.
The class is established as follows:
TTIMECOUNT = Class (TCUSTompanel)
Private
protected
public
Published
END;
Second, determine members, methods and attributes and events
FCount; read-only private member, save the count value
Factive: boolean; / Determine if a class interval event occurs
FINTERVAL: TINTERVAL; This can set time event trigger interval. It will be better with the integer value, but it will learn, here is an enumerated, in Type as follows:
TINTERVAL = (Tenm, TWENTYM, THIRTYM, FORTYM, FIFTYM, SIXTYM);
Ten minutes and 60 minutes respectively.
Timelen, TimeNum: Integer; These two are isolated from the outside in the determination of the time event of the class.
FTIMEOUT: TNOTIFYEVENT; Method of time interval events, implementing his association with external processing functions by scheduling methods.
We want it to count in seconds, and to involve time applications, there is this most important member:
FTIMER: TTIMER;
This member object is to instantiate it in the class constructor, and assign his attribute value, but also in the destructor is released.
as follows:
// Construct the function, inherit the constructor of the parent class, and initialize the members in the class.
Constructor TTIMECount.create (Aowner: Tcomponent);
// Create a time control and set the relevant parameters
Procedure CreateTimer;
Beginftimer: = TTIMER.CREATE (Self);
Ftimer.enabled: = false;
Ftimer.Interval: = 1000;
Ftimer.ontimer: = fTimERTIMER;
END;
Begin
Inherited Create (Aowner);
CreateTimer;
END;
// Destructor, first release the time control, then inherit the destructor of the parent class
Destructor TTIMECount.destroy;
Begin
Ftimer.free;
Inherited destroy;
END;
The appearance and default value of the component is also set in the constructor. Here it is deleted, and the source code is returned.
among them
FTIMERTIMER; is a very important function, there is a declaration in this class:
Procedure fTimertimer (sender: TOBJECT); / / Time Control Event Processing Function
In this processing function, the increment of the count value is implemented and displayed in the container. In addition, it is also triggered if there is a sufficient condition of the time event of the class. If there is, it calls the dotimeout; process, this is the scheduling function of the event:
// Event scheduling function, link the external event handler and the event method pointer of the class
Procedure ttimecount.dotimeout;
Begin
IF assigned (ftimeout) THEN
FTIMEOUT (Self);
END;
And attributes are set according to private members:
public
Property Count: Integer Read FCount Default 0; / / The read-only property of the count value cannot be declared in the public because it is read-only, only the program is running by it.
Published
Property Interval: Tinterval Read Finterval Write SetInterval Default Tenm
Property Active: Boolean Read Factive Write SetActive Default False;
Property Ontimeout: TNOTIFYEVENT READ FTIMEOUT WRITE FTIMEOUT
There are also several custom methods that are
Procedure pause; // Pause count
Procedure resume; // count from the paused count.
Procedure stop; // stop
Procedure start; // Start counting
It is relatively simple.
Third, the translation of the parent class:
TCUSTompanel and its parent class have a lot of properties set to protected, make their subclasses can have more flexible selections, and whether these properties are displayed into object viewers, if they want, re-declare these properties in Publish, I Refer to TPANEL's source code, and select some of these attributes as needed to declare in public, pay attention, the event is also attribute, as long as you translate it, you can set the handling event.
four. The following is the source code for counting components. I believe that there is a speech above, it should not be difficult:
Unit countunit;
Interface
Uses
Sysutils, Classes, Graphics, Controls, Extctrls
Type
// Used to set the interval that occurred at the time event
TINTERVAL = (Tenm, TWENTYM, THIRTYM, FORTYM, FIFTYM, SIXTYM);
TTIMECOUNT = Class (TCUSTompanel)
PrivateFTIMER: TTIMER;
Fcount: integer; // read-only private member, count value
Finterval: Tinterval; // Time Event Interval
Factive: boolean; / / Decide if there is a spacing event
Timelen: Integer; // The length of the event is incident, in seconds.
TimeNum: integer; / / count value, useful together with Timelen to determine if the event should happen
FTIMEOUT: TNOTIFYEVENT; / / Event Method Pointer
Procedure SetInterval (i: tinterval);
Procedure setActive (A: boolean);
Procedure fTimertimer (sender: TOBJECT); / / Time Control Event Processing Function
protected
Procedure Dotimeout; Dynamic; // Scheduling method for association events.
public
Procedure pause; // Pause count
Procedure resume; // count from the paused count.
Procedure stop; // stop
Procedure start; // Start counting
Constructor Create (Aowner: Tcomponent); OVERRIDE;
DESTRUCTOR DESTROY; OVERRIDE;
Property Count: Integer Read FCount; / / Reemption Properties of the Count
Published
Property Interval: Tinterval Read Finterval Write SetInterval Default Tenm
Property Active: Boolean Read Factive Write SetActive Default False;
Property Ontimeout: TNOTIFYEVENT READ FTIMEOUT WRITE FTIMEOUT
// Some properties of explicit ancestors are in the object viewer
Property Bevelinner;
Property begouter;
Property BevelWidth;
Property Color;
Property font;
Property Popupmenu;
Property Showhint;
Property Taborder;
Property Tabstop;
Property visible;
Property Onclick;
Property OnDBLClick;
Property OnMouseDown;
Property OnMouseMove;
Property OnMouseup;
END;
IMPLEMentation
// Construct the function, inherit the constructor of the parent class, and initialize the members in the class.
Constructor TTIMECount.create (Aowner: Tcomponent);
// Create a time control and set the relevant parameters
Procedure CreateTimer;
Begin
Ftimer: = TTIMER.CREATE (Self);
Ftimer.enabled: = false;
Ftimer.Interval: = 1000;
Ftimer.ontimer: = fTimERTIMER;
END;
// The following is set up an appearance
Procedure setView;
Begin
Width: = 100;
HEIGHT: = 50;
Color: = $ 000000;
Font.color: = $ ffffff;
Font.size: = 14;
Font.stylele := [fsbold ,ffsunderline] ;;
Bevelouter: = BVLOWERED;
CAPTION: = '0'; end;
Begin
Inherited Create (Aowner);
Fcount: = 0;
Finterval: = TENM;
FACTIVE: = FALSE;
Timelen: = 600; // Ten minutes, six hundred seconds
TimeNum: = 0;
CreateTimer;
SetView;
END;
// Destructor, first release the time control, then inherit the destructor of the parent class
Destructor TTIMECount.destroy;
Begin
Ftimer.free;
Inherited destroy;
END;
/ / Set the time event interval, pay the corresponding number of intervals
Procedure Ttimecount.setInterval (i: tinterval);
Begin
IF FinterVal <> I THEN
Begin
FINTERVAL: = i;
Case FinterVal of
TENM: Timelen: = 600;
TWENTYM: TIMELEN: = 1200;
ThiRTYM: Timelen: = 1800;
Fortym: Timelen: = 2400;
Fiftym: Timelen: = 3000;
SIXTYM: TIMELEN: = 3600;
END;
END;
END;
Procedure Ttimecount.setActive (A: Boolean);
Begin
IF factive <> a kil
Begin
Factive: = a;
TimeNum: = 0;
END;
END;
Procedure TTIMECOUNT.PAUSE;
Begin
IF ftimer.enabled then
Ftimer.enabled: = false;
END;
Procedure TTIMECount.Resume;
Begin
IF not ftimer.enabled then
Ftimer.enabled: = True;
END;
Procedure TTIMECOUNT.STOP;
Begin
Ftimer.enabled: = false;
Fcount: = 0;
TimeNum: = 0;
CAPTION: = '0'
END;
Procedure TTIMECOUNT.START;
Begin
IF (not ftimer.enabled) and (timenum = 0) THEN
Ftimer.enabled: = True;
END;
// The most important time function is used to call the event triggered height function of the class.
// and display count value in the container
Procedure TTIMECOUNT.FTIMERTIMER (Sender: TOBJECT);
Begin
Inc (FCOUNT);
IF (Fcount Mod 3600) = 0 THEN FCOUNT: = 0;
CAPTION: = INTOSTR (FCOUNT); // This is the value of the display
INC; TIMENUM
IF (Timenum = Timelen) and (Factive) THEN
Begin
Dotimeout;
TimeNum: = 0;
END;
END;
// Event scheduling function, link the external event handler and the event method pointer of the class
Procedure ttimecount.dotimeout;
Begin
IF assigned (ftimeout) THEN
FTIMEOUT (Self);
END;
End.
V. Component registration and delete.
The component class is written, then it will be tested. This is whispered, and you can create it in the project, set the corresponding attribute, specify the time event to see if it is correct. The above is tested. After determining the correct, you will come to see the steps of the assembly:
First, we must put the component unit in a specified folder for later management, and the IDE unified specified path.
I created a Mycom folder in the Delphi directory, used to store component units
Then choose:
Component-> New Component
Start Component Wizard
In the Ancestor Type: Mid-fill TCUSTompanel
Class Name (your component class name) is filled in: Timecount
Palette Page, the default is Samples, I will use this, of course, you can also create one yourself.
Unit File Name (the unit name where the component class is located), write TimeCount.
Search Path (Search Path), you must include the folder path built above and you can pass.
Click on the "..." button, a dialog box appears, there is a "..." button next to the edit box, click on it to browse the folder, select the path of the folder built above, click OK, click OK, go back to new component ;.
At this time, you will click on the OK, and the code is as follows:
Unit timecount;
Interface
Uses
Messages, Sysutils, Classes, Graphics, Controls, ExtCtrls
Type
TTIMECOUNT = Class (TCUSTompanel)
Private
Published
public
Published
END;
PROCEDURE register;
IMPLEMentation
PROCEDURE register;
Begin
RegisterComponents ('Samples', [TTIMECOUNT]); // Register the function for the function, automatically help you
END;
End.
At this time, replace the above components and replace this class. (Of course, the registration function above can be retained). Then save it to the folder built above.
Then you have to install the components, click Component-> Install Component
The first Unit File Name in the dialog is the path name of the component unit. PAS file. Others just by default.
Remember the third item. . /Delphi7/lib/dclusr.dpk, this is the package name of the storage component, wait for the deletion of components.
The next point OK, you can see if Samples has a new component.
However, the component icon on the panel doesn't seem to look good, you should give him a new icon:
But you should first remove it from the panel, File-> Open ... Open DClusr.dpk.
Select Timecount.PAS under Contains, remove the Remove above. Delete it
Then click Compile and re-enhance the package once. Just line. The new components on Samples disappeared.
Next is to make a component icon:
Open Image Eidtor, build a DCR file, build a 24 × 24 bitmap, draw your favorite icon, start the name of the bitmap as the component class name, and use uppercase, namely: TTIMECOUNT starts the DCR file Like the cell name where the component class is located, it is uppercase, namely: TimeCount.
Save the file to the folder where the unit is located,
Finally, in the above method, look at it once, see if there is a beautiful icon.
Sixth, finished, everyone should have a clear concept, which involves a lot of skill. In the next chapter, these content will no longer talk, only the main energy is treated in the assembly implementation technology. Because our components are still relatively simple, there is no high-level topic. But there is no relationship, follow me slowly, you will understand all aspects of the component production.