2. Establish original control
Standard controls are visible at run time. These standard controls are inherited from Twincontrol, and when you build the original control, you use Tcustomcontrol as the starting point. A key feature of standard control is that it has a window handle, and the handle is stored in the attribute handle, which controls:
can accept input focus
Ability to pass handles to Windows API functions
If control does not need to accept input focus, you can make it graphical control, which may save system resources.
3. Establish graphic control
Graphic controls are very similar to custom controls, but they do not have window handles and therefore do not occupy system resources. The biggest constraint on graphics control is that they cannot receive input focus. You need to inherit from Tgraphiccontrol, which provides a graphical canvas and can handle WM_PAINT messages that you need to override paint methods.
4. Inherit window control
Windows has a concept called a window class, similar to the concepts of object-oriented objects and classes. A window class is a collection of information that is shared between different instances of the same window or control in Windows. When you create a new control using the traditional Windows programming method, you define a new window class and register it in Windows. You can also create a new window class based on an existing window class. This is called inheriting from the window class. In traditional Windows programming, if you want to build a customized control, you have to do it in a dynamic-link library, like standard Windows control, and provide an access interface. Using Delphi, you can create a part that wraps over an existing window class. If you already have a client-controlled library and want to run it in your Delphi application, you can create a widget that allows you to use existing controls and get new controls. There are many examples of this in the library unit Stdctrls.
5. Create non-visual Parts
Abstract object Type tcomponent is the underlying type of all parts. The part that you create directly from Tcomponent is a non-visual part. Most of the parts you write are visual controls. Tcomponent defines the basic properties and methods required for a part in Formdesigner. Therefore, any part inherited from Tcomponent has design capability.
There are quite a few non-visual parts, mainly using them as interfaces for Non-visual program units (such as database units) and dialog boxes.
19.2.1.3 method of building new parts
There are two ways to create new parts:
Create parts by hand
Using Component Expert
Once the establishment is complete, the required minimum functional units are available and can be installed on the component palette. Once installed, you can place the new part in the Form window and test it in both the design and run stages. You can also add new features to the widget, update the selection board, and test again.
1. Create parts by hand
Obviously the easiest way to create a part is to use component Expert. However, you can also accomplish the same step by hand.
The following three steps are required to create a part manually:
Create a new library unit
Inheriting a Part object
Registering parts
⑴ Create a new library unit
The library unit is an independent compilation unit of Object Pascal code. Each form has its own library unit. Most parts (logically a group) also have their own library units.
When you build a part, you can create a library unit for the part, or you can add the new part to an existing library unit.
① Create a library unit for the part, select File/new ..., and select Unit,delphi in the New Items dialog box to create a new file and open it in the Code Editor
② to add parts to an existing library unit, simply select File/open to select the source code for the existing library unit. Only part code can be included in the library unit, and if there is a form in the library unit, an error is generated
⑵ inherits a Part object
Each part is a descendant object of the Tcomponent. Can also inherit from Tcontrol, Tgraphiccontrol, etc.
To inherit a part object, add the object type declaration to the interface portion of the library unit.
For example, to create a simplest part that inherits Non-visual directly from Tcomponent, add the following type definitions to the interface part of the part unit.
Type
Tnewcomponent=class (tcomponent)
......
End
Now you can register tnewcomponent. But the new widget is no different from the tcomponent, you just create your own widget frame.
⑶ Registered Parts
The registration part is to tell Delphi what parts are added to the Part gallery and which page to join component palette.
To register a part:
The ① adds a register process to the interface portion of the part unit. The register takes no parameters, so the declaration is simple:
Procedure Register;
If you add a part to a library unit of an existing part, because there is a register process, there is no need to modify the declaration.
② the implementation part of the library unit to write the register process for each part you want to register calls process registercomponents, process registercomponents with two parameters: Component Palette the page name and set of part types. For example, to register a part named Tnewcomponent and place it in the Samples page of component palette, use the following procedure in your program:
Procedure Register;
Begin
Registercomponents (' Samples ', [tnewcomponent]);
End
Once registered, Delphi automatically displays the part icon on the component palette.
2. Use Component Expert (component specialist)
You can use component expert to create new parts. Using component Expert simplifies the initial phase of creating new parts, because you only need to describe three things:
The name of the new part
Ancestor type
Component Palette page name to be added to the new part
Component expert performed the same work in a manual manner:
Create a new library unit
Inherit from new Part object
Registering parts
However, component expert cannot add parts to an existing unit.
You can choose File/new ..., and in the New Items dialog box, select Component to open the Component Expert dialog box.
After completing each field of the Component Expert dialog box, select OK. Delphi establishes a library unit that includes new parts and register processes and automatically adds uses statements.
You should immediately save the library unit and give it a meaningful name.
19.2.1.4. Test for parts that are not installed
You can test the action of the part at run time before installing the new part in Component palette. This is especially useful for debugging new parts, and can also test any part with the same technique, regardless of whether the part appears on the component palette.
Essentially, you test a component that is not installed by imitating the action of the Delphi that the user placed the part in the form.
You can test for parts that are not installed by following these steps
1. Add the name of the unit that contains the part in the uses statement of the form cell
2. Add an Object field to the form to represent the part
This is the main difference between adding parts and Delphi to add parts.
You add the Object field to the public section at the bottom of the form type declaration. Delphi adds the Object field to the top of the bottom declaration.
You cannot add a field to the top of the form type declaration of Delphi Management. The object domain declared in this section will be stored in the DFM file accordingly. Adding a component name that does not exist on the form causes an error that the DFM file is invalid.
3. Attach the form to the OnCreate event handling process
4. Construct the part during the OnCreate process of the form
When you call a part's construction process, you must pass the owner parameter (which is responsible for the destructor of the part) and generally always take self as the incoming parameter of owner. In OnCreate, self refers to a form.
5. Assign value to component's Parent property
Setting the Parent property is often the first thing to do when you construct a part. Parent is in the form of a part, usually the parent is forms or Goupbox, Panel. The parent is usually assigned to self, the form. It is best to assign a value to parent before setting other properties for the part.
6. Assign values to other properties of parts as needed
Suppose you want to test a new part named Tnewcomponent, the library unit named Newtest. The form library unit should be like this;
Unit unitl;
Interface
Uses Sysutils, Windows, Messages, Classes, Grophics, Controls, Forms, Dialogs,
Newtest;
Type
TFORML = Class (Tform)
Procedure Formcreate (Sender:tobject);
Private
{Private Declaration}
Public
{Public declaration}
Newcomponent:tnewcomponent;
End
Var
FORML:TFORML;
Implementation
{$R *. DFM}
Procedure Tforml.formcreate (Sender:tobject);
Begin
Newcomponent: = Tnewcomponent.create (Self);
Newcompanent.parent: = Self;
Newcompanent.left: = 12;
End
End.