http://blog.csdn.net/cml2030/article/details/3166634Delphi Controls-composite controlsTags: delphidestructorbuttonstringdivconstructor2008-10-28 13:48 1835 people read comments (0) favorite reports
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Composite controls
A composite control is a very important control in a Delphi control, and a composite control is a combination of two or more two controls that are re-assembled into a new control. For example, Tspinedit, Tlabelededit, Tdbnavigator, and so on is a composite control, Tdbnavigator is actually put a number of buttons on a panel. When making a composite control, we typically derive the control from Twincontrol.
The control we do this time is a composite control with an edit box and a button, and the button will display the length of the text in the edit box at any time as the user enters text in the edit box. We show the source of the control to you first.
Unit Editbutton;
Interface
Uses
Sysutils, Classes, Controls, Stdctrls, Messages;
Type
Teditbutton = Class (Twincontrol)
Private
Fedit:tedit;
Fbutton:tbutton;
ftext:string;
Procedure Fsettext (avalue:string);
Procedure Oneditchange (Sender:tobject);
Protected
Procedure Wmsize (var msg:tmessage); message wm_size;
Public
Constructor Create (aowner:tcomponent); override;
destructor Destroy;override;
Published
Property text:string read Ftext write Fsettext;
End
Procedure Register;
Implementation
Procedure Register;
Begin
Registercomponents (' Linco ', [Teditbutton]);
End
Constructor Teditbutton.create (aowner:tcomponent);
Begin
inherited;
Fedit: = Tedit.create (nil);
Fedit.parent: = self;
Fedit.top: = 0;
Fedit.left: = 0;
Fedit.height: = Height;
Fedit.width: = Width Div 2;
Fedit.onchange: = Oneditchange;
Fbutton: = Tbutton.create (nil);
Fbutton.parent: = self;
Fbutton.top: = 0;
Fbutton.left: = Width Div 2;
Fbutton.height: = Height;
Fbutton.width: = Width Div 2;
End
destructor Teditbutton.destroy;
Begin
Fedit.free;
Fbutton.free;
inherited;
End
Procedure Teditbutton.fsettext (avalue:string);
Begin
Fedit.text: = Avalue;
End
Procedure Teditbutton.oneditchange (Sender:tobject);
Begin
Fbutton.caption: = IntToStr (Length (Fedit.text));
End
Procedure Teditbutton.wmsize (var msg:tmessage);
Begin
Fedit.height: = Height;
Fedit.width: = Width Div 2;
Fbutton.left: = Width Div 2;
Fbutton.height: = Height;
Fbutton.width: = Width Div 2;
End
End.
Code Explanation:
(1), we first defined two variables
Fedit:tedit;
Fbutton:tbutton;
Represents text edit boxes and buttons in a composite control, respectively.
(2) The so-called composite control is simple to draw on a common substrate by drawing the individual controls (which can be called child controls) that make up the composite control. So we set up the individual child controls in the constructor, and then set the properties of their positions, respectively.
Take the text edit box as an example:
Fedit: = Tedit.create (nil);
The function is to create an edit box control. If the parameter for create is specified as nil, the child control is in a design state that responds to the user's actions, and if set to self (that is, the parent control of the child control is the base board), the child control is not responsive to user action at design times. If set to self the destructor does not use Fedit.free to destroy the object, and the object is automatically destroyed.
Fedit.parent: = self; the function is to set the parent control of the child control, and the control cannot be displayed without this sentence.
Fedit.top: = 0;
Fedit.left: = 0;
Fedit.height: = Height;
Fedit.width: = Width Div 2;
These four sentences are the relative positions of the control on the base plate, where the top,left is not relative to the form, but relative to the substrate.
Fedit.onchange: = Oneditchange;
It is the handle of the onchange (text change event) that sets the edit box control to Oneditchange;
(1) It is possible for the user to change the size of the control at design time or at runtime, and the order of the control's sub-controls becomes chaotic, so the control's Wm_size event (the event that the control's size changes) is required to reset the child control's position, size, and so on. This is the function of wmsize.
After you install the control, you find that the control is already running correctly, but there is another problem, which is that the control does not have the necessary properties such as Onclick,onchange. All we need to do is increase the event handler property for the control, and then point the Read and write method of the event handle property to the event handle property of the child control. For example, we add the onclick event to the control, which occurs when the user clicks the button, I just add the following code in the pulished section:
Property Onclick:tnotifyevent Read Getonclick write Setonclick
In private, add the following method declaration:
function getonclick:tnotifyevent;
Procedure Setonclick (avalue:tnotifyevent);
The implementations of these two methods are:
function Teditbutton. Getonclick:tnotifyevent;
Begin
Result: = Fbutton.onclick;
End
Procedure Teditbutton. Setonclick (avalue:tnotifyevent);
Begin
Fbutton.onclick: = Avalue;
End
Delphi Control-Composite Control