Delphi Attribute property (suitable for beginners Delphi)

Source: Internet
Author: User
Delphi Attribute Property (suitable for beginners Delphi) propery in Delphi

Preface:

For Delphi Beginners, have object-oriented knowledge and Java or VC programming experience to read. a common attribute

We can often see this code in the Delphi class: Propert Property attribute name Type name read String 1 write String 2

The name of the attribute here may be different. It's all the same format: Property attribute name read String 1 write String 2

I use the property Left:integer read Fleft write setleft, for example, it is the Tcontrol attribute that you can find in the controls file. Left is an integer-type property. Read affirms the variables or methods to access the variable, and write affirms the variables or methods that are accessed when the variable is modified. Note: It can be a variable or a method, and I'll tell you what's going on in the back. Here it is a variable, the name is called Fleft. For the purpose of encapsulation, we usually put such variables in the middle of the private, and sure enough, in private we can find

Fleft:integer This code (in the name of the custom, we named this variable as the property name preceded by a capital F). So when you read the attribute, you actually access the value of Fleft. So you can write some methods to modify the Fleft and indirectly modify the left value. And then we look at Setleft, where it's a method (ask me how I know.) Or look at the naming conventions, usually with set in front of the property name, and usually in private, we'll verify that we see the declaration in private:

Procedure Setleft (Value:integer);

and the following code implementation:

Procedure Tcontrol.setleft (Value:integer);

Begin

SetBounds (Value, Ftop, Fwidth, fheight);

Include (Fscalingflags, sfleft);

End

If you write the following code to change the left:control1.left:=23, then the program calls the function Setleft, SetBounds is the function that changes the region, and here you see the benefits it encapsulates. Each time you change the left, it will change the size of the area according to the new right, and this function also changes the size of the fleft, please refer to the SetBounds source code.

Procedure Tcontrol.setbounds (Aleft, atop, Awidth, Aheight:integer);

Begin

If Checknewsize (Awidth, Aheight) and

((Aleft <> fleft) or (atop <> ftop) or

(Awidth <> fwidth) or (aheight <> fheight)) Then

Begin

InvalidateControl (Visible, False);

fleft: = Aleft;

Ftop: = atop;

Fwidth: = Awidth;

Fheight: = Aheight;

Updateanchorrules;

Invalidate;

Perform (wm_windowposchanged, 0, 0);

Requestalign;

If not (csloading in componentstate) then Resize;

End

End

This makes it seem that the value of the property is changed by an assignment operation. Read and write can be variables, or functions, depending on your design. You can certainly write this: Propert property attribute name Type name read Variable 1 write variable 2. Variable 1 and variable 2 can be the same. You can do the same. Propert property attribute name Type name Read Method 1 Write Method 2. let you combine. But there are 2 points to note:

1. Naming rules is best used by custom and easy to read.

2. If it is a variable, the type is consistent with the type of the property and, if it is a method, the entry parameter is consistent with the type of the property.

Two event Properties Tevent

We often use the component's event properties, such as the Click event, but it is hard to see how it is invoked from the surface and how it is triggered. Let me give you the answer.

We saw in the property manager Object Inspector that the event page onclick right corresponds to the name of a method. We can actually match the event of a component to the previous method. Take a form as an example of Form1. Onmousedown:= ' Your method '. Note that the entry parameters of the method are fastidious, here is (Sender:tobject)

We're still a tcontrol for example, we found this code:

Property Onmousedown:tmouseevent Read Fonmousedown write Fonmousedown, similar to the above, but there is a special type, tnotifyevent, is an event type, We found it in the statement:

Tmouseevent = procedure (sender:tobject; Button:tmousebutton; Shift:tshiftstate; X, Y:integer) of object;

As you can see, it's actually a function, but the blue section limits the entry parameters. Then we Form1 by assigning value. Onmousedown:= ' Your method ', it corresponds to the OnMouseDown method. Then we just write a function to intercept the mouse message, in which directly or indirectly call Fonmousedown, then the message and the processing function. Here it indirectly calls the number of layers more, it is more time-consuming to talk about, involving message types, suggest you go to see Levi's book.

The following is attached to the indirect call process, in fact, a lot of information also occurs when the indirect call, it came out: (

Procedure Wmrbuttondblclk (var message:twmrbuttondblclk); Message wm_rbuttondblclk;//function to intercept messages

Procedure Tcontrol.wmrbuttondblclk (var message:twmrbuttondblclk);

Begin

inherited;

Domousedown (Message, Mbright, [ssdouble]);

End

Procedure Domousedown (Var message:twmmouse; Button:tmousebutton;

Shift:tshiftstate);

Procedure Tcontrol.domousedown (Var message:twmmouse; Button:tmousebutton;

Shift:tshiftstate);

Begin

If not (csnostdevents in ControlStyle) then

With the message do

if (Width > 32768) or (Height > 32768) Then

With Calccursorpos do

MouseDown (Button, Keystoshiftstate (Keys) + Shift, X, Y)

Else

MouseDown (Button, Keystoshiftstate (Keys) + Shift, Message.xpos, Message.ypos);

End

Procedure MouseDown (Button:tmousebutton; Shift:tshiftstate;

X, Y:integer); Dynamic

Procedure Tcontrol.mousedown (Button:tmousebutton;

Shift:tshiftstate; X, Y:integer);

Begin

If assigned (Fonmousedown) then Fonmousedown (Self, Button, Shift, X, Y);

End

Benefits:

If you write more of your own class, you will find it convenient to do so, rather than Java to write Getleft,setleft, and then put text in private, access and modify to call different methods, And Delphi you are just call Contol1.text to access, control1.text:= ' A string ' to modify its value.

In terms of processing messages, the base class declares attributes such as onclick,onmousedown as protected, and if you want to use it, you can declare that published can appear in Object Inspector, and then write it in a convenient way, You can also assign a value to it in the Ctreate function without exposing it, rather than writing listener as complex as Java.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.