Properties in Delphi (suitable for beginners Delphi)
ProPERY IN Delphi
Preface:
Suitable for Delphi beginners, with object-oriented knowledge and Java or VC programming experience.
One ordinary property
We can often see such code in the class of Delphi: Property Property Type Type Read String 1 Write String 2
The name of the attribute here may be different. All this format: Property property name read string 1 Write string 2
I use Property Left: Integer Read Fleft Write Setleft; as an example, it is the property of TControl, and you can find it in the Controls file. LEFT is an Integer type property. The READ declares that the variable or method to access the variable, Write has declated the variables or methods accessed when the variable is modified. Note: It can be variables, or it is a method, I tell you what is going on behind. Here it is a variable, named Fleft. For the purpose of the package, we generally put this variable in the middle of the private, and sure enough, we can find it in private.
Fleft: INTEGER This code (for naming habits, we named such variables as the attribute name plus a big write f). This way when you read this property, you actually access the value of FLEFT. So you can write some ways to modify FLEFT, indirectly modify the value of Left. Then we look again setleft, here is a way (ask me how to know? Still look at the naming rules, usually use the set of sets, usually in private, let's verify, we are in private See the declaration:
Procedure setleft (value: integer);
Realize the following code:
Procedure tcontrol.setleft (value: integer);
Begin
SetBounds (Value, FTOP, FWIDTH, FHEIGHT);
Include (FSCALINGFLAGS, SFLEFT);
END;
If you write the following code change left: control1.Left: = 23, then the program calls the function setleft (23), setbounds is a function of changing the area, here you understand the benefits of its encapsulation, each time you change Left It will change the size of the area according to the new Left, and this function also changes the size of FLEFT, please refer to the source code of SetBounds.
Procedure Tcontrol.SetBounds (Aleft, ATOP, AWIDTH, AHEIGHT: INTEGER);
Begin
IF Checknewsize (AWIDTH, AHEIGHT) AND
(ALEFT <> FLEFT) OR (ATOP <> FTOP) OR
(AWIDTH <> fwidth) or (ahant <> fheight)).
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 appears to change the value of this property by assigning operations. READ and WRITE can be variables or functions depending on your design. You can of course write this: Property property name Type READ Variable 1 WRITE Variable 2. Variable 1 and variable 2 may be the same. You can also like this Property property name type name read method 1 Write method 2. Let you have a combination. But there are 2 points to pay attention:
1. The naming rule is best to read it by habit, easy to read.
2. If it is a variable, the type of type and attributes are consistent, if it is a method, then the type of portal parameter is consistent with the type of attribute.
Second Event Attribute TEVENT
We often use the component's event attribute, saying that Click events, but we are difficult to see how it is called from the surface, how to trigger it. Let me answer your questions.
We see the name of a method in the right side of the event page onclick in the property manager Object Inspector. We can actually give the event of a component to the next way. With an form1. OnMouseDown: = your method. Note that the entrance parameters of the method are very particular, here is (Sender: TOBJECT)
We are still a tcontrol as an example, we found this code:
Property OnMousedown: TMouseEvent Read Fonmousedown Write Fonmousedown; Similar to the above, there is a special type, TNOTIFYEVENT, is an event type, we find its declaration:
TMOUSEEVENT = Procedure (Sender: Tobject; Button: TMouseButton; Shift: TshiftState; x, y: integer) OF Object;
It can be seen that it is actually a function, but the blue part defines the entrance parameters. Then we correspond to the onMouseDown method by assigning Form1. OnMouseDown: = 'Your Method'. Then we just write a function of intercepting the mouse message, directly or indirectly call Fonmousedown, then the message and handle function corresponds. Here, its indirect call is more, taking time, involving Message type, I suggest you go to see Li Wei's book.
The following is attached to the indirect call, in fact, there is still a lot of messages to occur, and it is not one out: (
Procedure WmrButtondblclk (var message: twmrbuttondblclk); Message WM_RBUTTONDBLCLK; // Intercept message function
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) Thenwith 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;
benefit:
If you write your own classes, you will find how convenient this, but not like Java to write getleft, setleft, then put the text in private, to call different methods when accessing and modify, and Delphi you All just call Contol1.Text to access, control1.text: = 'a string' to modify its value.
In terms of processing message, the base class stipulates the attributes such as OnClick, onMousedown, if you want to use, you can apply to public Inspector, and then easily write processing methods, you can also open, and Assign it in the ctreate function without having to write Listener as complicated like Java.
My research is not deep, what do you not properly correct :). Welcome letter wenjunwu430@163.com
Huazhong University of Science and Technology, Internet Center Laboratory (ITEC.HUST.EDU.CN)
邬文俊
2004-12-4 night