Property in Delphi Property (suitable for beginners Delphi) ProPERYIN DELPHI
Foreword: Suitable for Delphi beginners, with object-oriented knowledge and Java or VC programming experience. One Ordinary Attribute We often see this code in the class of Delphi: Property property name type name 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 take Property Left: Integer Read Fleft Write Setleft; as an example, it is TControl's properties, 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 it is, in private, we can find Fleft: Integer's code (for naming habits, we name this variable as a property name) Add 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); 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 it, each time you change Left, it The size of the region 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 for SetBounds. 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)) The 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; so it looks like just outside The value of this property is changed 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 rules are best available, 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 Attributes TEVENT We often use components's event properties, 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 a form1. OnMouseDown: = 'Your Method'. Note that the entrance parameters of the method are very particular, this is a tControl as an example, we find 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 find its declaration: tmouseevent = procedure (sender: Tobject; Button: tMouseButton; Shift: tshiftstate; x, y: integer) of object; can be seen, it is actually a function, but blue The color section 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; // message interception 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) then with 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);
Benefits: If you write your own class, you will find how convenient this is, don't write getleft, setleft like Java, then put the text in private, access and modify, to call different methods, and Delphi You 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.