One of the components (concept)

zhaozj2021-02-16  140

As the beginning of the component, you should understand some concepts, I think these concepts are very important, which will be the theoretical basis for future practice.

First, the brief hierarchy of the component. In general, the component of the VCL can start from Tcomponent. Its most obvious feature is that its attribute can be manipulated by an object in design, in addition, he can have other components. Under Tcomponent, non-visual components and visual components are divided. Non-visual components such as TopEndialog, TTIMER, TTABLE, etc., these components are inherited from Tomponent, so inherit the characteristics that can be manipulated during design. The visual components are started from TControl, which introduces visual properties and methods to allow inheritance from its classes. These visual features. TControl is divided into two types of component types: from the TWINControl (window control), and from the TGRAPHICCONTROL (graphic control). Inheriting a control from TwinControl, encapsulates the Windows control, so there are many features of the Windows control, such as a focus, a unique handle, the user can interact with these controls by sending messages. Inheriting controls from TGRAPHICCONTROL, it is also visible, but there is no handle, which can be called graphic controls, such as TLabel, TBevel, is Delphi drawn, does not take up system resources. Second, a simple look property attribute definition: TsomeObj = class Private FCount: integer; Protected Procedure SetCount (value: Integer); published Property count: integer read FCount write SetCount default 0; // attribute definition End; the property from the private Member Fcount reads the value, and relying on the setCount method to the private member FCOUNT. The advantage of attributes is that it can be very intuitive and different from private members. Because attributes can protect private members by writing access methods: procedure tsomeboj.setcount (value: integer); begin if fcount <> value dam; fct: = value; end; where Default 0 in the property definition is not default (ie object The value displayed by the viewer), the default value is set in the constructor of the component class. And DEFAULT has a role to determine whether or not to save the value of this property in the DFM file, such as DEFAULT 0, that is, when the attribute value is 0, the attribute is not saved to the DFM, if the attribute value is not For 0, the property is saved to the DFM. In addition, attribute definition has a keyword to nodefault, set this keyword, such as Property Count: Integer Read Fction Write SetCount NodeFault; whether it is what the value is, it will be written to the DFM file.

Attributes can have the following types, only given a brief introduction below, and these types of properties will be applied in detail when component production: Simple Type Properties: As defined above, add an example of Property Text: String Read Ftext Write setText Imprint Type Properties: TenumType = (Enum1, Enum2, Enum3); FenumType: TenumType; Property EnumType: TenumType Read FenumType Write FenumType; In the Object Viewer, it is the drop-down column selection value. Collection Type Properties: Tset = (set1, set2, set3); tsets = set of tset; fsets: tsets; property sets: tsets read Fsets Write Fsets; In the object viewer, it is to list several options. false. For example, TFORM's Bordericons property is. Object Type Properties: A property is an object, and this object must be derived from TPERSISTENT or the class under him, in order to expand it in the object viewer, and set the properties inside. Array Type Properties: Array Properties If you want to see it in an object viewer, you need your own property editor (if you don't want to see it in the object, you don't want to see it), it is a more advanced component, and then introduce it in later components. Will be more intuitive. Here only gives its definition form: Property SELECTED [INDEGER]: Boolean read getSelected Write setSelected; Third, event: The event is actually a special property, he is a pointer type, pointing to an event method type. When there is a specific event, it is associated with a period of execution code. Here are an example to explain how the event occurs.

We first define a mouse event type of an event under a mouse point. It is actually the method pointer: type tmouseevent = procedure (sender: TOBJECT; button: tmousebutton; shift: tshiftstate; x, y: integer) OF Object; also define a private member : Mouse event type, ie a method pointer type fonmousedown: TMouseEvent Finally define an attribute: Class to associate the external event handler and fonmousedown together with this property: OnMouseDown: TMouseEvent Read Fonmousedown Write Fonmousedown; When there is a mouse button , the system will be sent to a window the WM_LBUTTONDOWN; message Delphi may intercept the message, the following definition message function: procedure WMLButtonDown (var message: TWMLButtonDown); message WM_LBUTTONDOWN; tune DOMouseDown in this message handler, DoMouseDown and call MouseDown in this which function only to the most important part of the function is such that: procedure TControl.MouseDown (Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin if Assigned (FOnMouseDown) then FOnMouseDown (Self, Button, Shift, X , Y); END; let's take a look at how the user is operating, he defines a homeobjMouseDown; is an event handler, you must be in the form of TMOUSEEVENT: Procedure SomeObjmousedown (Sender: Tobject; Button: TMouseButton; Shift : Tshiftstate; x, y: integer; then he assigns: someobj.onmous EDOWN: = SomeObjMouseDown; When the value is assigned, the class is actually through the onmousedown property, which will be associated with FonmousedWon, that is, the mousedown method is called Fonmousedown (Self, Button, Shift, X, Y); It is equivalent to calling Procedure SomeObjMouseDown (Sender: Tobject; Button: TMouseButton; Shift: TshiftState; X, Y: Integer); So users can write their own code in this custom event method, when the event occurs, the class The scheduling mechanism will automatically call this event method. Some people will ask, why do you want to call Domousedown from a message processing function, Domousedown calls mousedown, then call the event method. Why not call directly in the message processing function WMLButtondown, in fact, the purpose of doing this is to make some protection judgments, and the conversion of some messages added, so that these values ​​look more intuitive.

Ok, the incident tells here, I don't know if you understand, it may be that my expression is not good, but there is no relationship. When I really do it, everyone should understand. Fourth, component production step: has the above basic concept, in fact, the simple component is no longer problem, but to do real components, you need to have a correct process, we will learn to do components later, will come together do. It is mainly as follows: 1, determine a ancestor class. How to determine, it can be determined according to the brief hierarchy of the above components. If you want to do non-visual components, you can start from inheriting Tcomponent. If you want to see a visualization component, you can start from the subclass of TControl. 2 Create a component of the component. This is said when making a component, but it is a few operations in the IDE. 3 Write properties, methods, events, members, and so on. These have been described in detail, and is the core part of the write component, in fact, the main content of the following practice. 4 Test, install components and write help, this content is compared to the secondary, and the following example will install, including a single unit, or install it with a package. And write help, you will be out of range, you will not say it here. About the basic concept of the component is over here, it is practical, with the above knowledge, it is not very difficult to practice, and a lot of things are above, and some advanced features will slowly say later.

The next article is a simple component, in fact, only the basic principles are used here, and the most important thing is to give a complete component production process. When it is more difficult to more difficult, the focus will be on the implementation of the code, and others will be omitted.

转载请注明原文地址:https://www.9cbs.com/read-8918.html

New Post(0)