Through examples, VCL components development is the whole process (3)

zhaozj2021-02-16  41

(Connected to above)

Third, add component icons, registration components attribute categories:

In the previous article we have completed the development of the basic function of the component. But unfortunately, you will install the component package, you will find that the component displayed in the Delphi component page does not clearly describe the functionality of our component (because our components inherit from TcustomLabel, the icon is a default Delphivcl icon. If the component inherits the components that have already appeared in the component panel, the icon will also be the same as the component!). Obviously a good component, especially a commercial component to be released, requires a target with its own characteristics, let's take this work:

Open the Image Editor (ToolsàImage Editor) of Delphi, create a new component resource (fileànewàcomponent resource file (.dcr), right-click New New New a Bitmap bitmap resource to adjust the size of the bitmap (we use 24 * 24) After the color depth, it is determined that double-click the creation of a bitmap name or a picture (the use of the drawing tool is almost almost the use of the drawing program, this is slightly), and we need to be a bitmap file after completion. Take a name (right click on Bitmap) because Delphi enforces the name of this bitmap to be the same as the name of the component, and we have to write, here we take: Tclock. Finally, save this resource file to our component package (DPK file) directory, named clockdcr.dcr. Finally, add a compiler switch in the Interface section in the CLOCK code: {$ r clockdcr.dcr} then recompile the update component (Remember how to update?), The component icon will become a bitmap we have just made. !

Next we will classify the properties of the components we developed and introduce important features in component development: attribute category.

In order to register some of our components and clocks to a new category to separate them and Label's properties, let component users can easily discover new features of components, we inherit the base class TPROPERTYCATECATECATECATEGORY In Delphi5, this requires a reference unit DSGnintf, but should pay special attention to this base class in Delphi7, and there is no such cell file. Registering new attribute categories can be done by directly using RegisterPropertyIncategory, completed in the following code. There will be two methods in the corresponding place and explain their differences.) And override two types of methods, and finally use RegisterPropertyIncategory in the Register process (in Delphi5 in Delphi7 in Designintf unit, Note: Some units of Delphi are not installed, including the two units we point out and will be pointed out later, these units belong to the Open Tools API of Delphi is used to facilitate us, especially component developers Extend Delphi. If your Delphi does not have these units, copy the PAS file in the Toolsapi folder in the Source folder in the Delphi installation directory to the lib directory, and you need to use the program to use these units to compile Delphi. Will automatically compile these units) Method registration attribute category. We add the following code to the original code of the components we developed:

Uses

Designintf; // delphi7 // Delphi5 uses dsgnintf /// This part of this code is not required if it is Delphi7.

Type

Tclockgategory = Class (TPROPERTYCATEGORY) // Create a new attribute category

Class function name: string; override; // Name of the property category

Class Function Description: String; Override; / / Property Category Description

END;

......

Class function tclockgategory .name: string;

Begin

Result: = 'clockpro';

END;

Class Function Tclockgategory. Description: String;

Begin

Result: = 'Our Component Clock Description';

END;

The next thing we have to do is to modify the register process:

PROCEDURE register;

Begin

Registercomponents ('ClockandTime', [TCLOCK]);

This is the code of Delphi7 /

RegisterPropertyIncategory ('ClockPro', Tclock, 'State');

RegisterPropertyIncategory ('ClockPro', Tclock, 'Active');

RegisterPropertyIncategory ('ClockPro', Tclock, 'Begintime');

RegisterPropertyIncategory ('ClockPro', Tclock, 'Waketime');

RegisterPropertyIncategory ('clockpro', tclock, 'allowwake');

RegisterPropertyIncategory ('clockpro', tclock, 'onwakeup');

RegisterPropertyIncategory ('ClockPro', Tclock, 'Ontimeup');

//

/// This is the code of Delphi5 /

{

RegisterPropertyIncategory (TclockGategory, Tclock, 'State');

RegisterPropertyIncategory (TclockGategory, Tclock, 'Active');

RegisterPropertyIncategory (Tclockgategory, Tclock, 'Begintime');

RegisterPropertyIncategory (Tclockgategory, Tclock, 'Waketime');

RegisterPropertyIncategory (TclockGategory, Tclock, 'allowwake ");

RegisterPropertyIncategory (TclockGategory, Tclock, 'onwakeup');

RegisterPropertyIncategory (TclockGategory, Tclock, 'ontimeup');

}

END;

After recompilation, make a test program. At this time, as long as the component user right click Object Inspector to select Arrangeàby Category, you can see that the property has been clearly classified, as shown below:

However, it should be understood that the attribute category is definitely not abuse, because too much use of this technology will make the component user more troublesome and mind to find a certain attribute. In the next article, we will continue to study two very useful components characteristics.

(Endlessly)

转载请注明原文地址:https://www.9cbs.com/read-27353.html

New Post(0)