19th Chapter-delphi Custom Parts development (i) (1)

Source: Internet
Author: User
Tags abstract requires

In addition to supporting the use of visual components to build applications WYSIWYG, it also supports the design of their own parts for the development of applications.

In this chapter, you will explain how to write parts for the Delphi application. This chapter will serve two purposes:

Teach you how to customize parts

Make your parts an integral part of the Delphi environment

19.1 Delphi Parts Principle

19.1.1 what is a part

The part is the program component of the Delphi application. Although most parts represent visible elements of the user interface, the part can also be an invisible element in the program, such as a database part. To figure out what a component is, it can be examined in three ways: functional definition, technical definition, and experience definition.

1. Functional definition of Parts

From the end user perspective, the part is the element selected on the component palette and manipulated in the form Design window and Code window. From the part writer's perspective, the part is an object in the code. Before you write a part, you will be familiar with the existing Delphi components, so that your parts fit the needs of your users. One of the goals of writing a part is to make the part as similar as possible to other parts.

2. Technical definition of components

From the simplest point of view, the part is any object inherited from Tcomponent. Tcomponent defines the most basic behavior that all parts must have. For example, features that appear on Component palette and edited in the Form Design window. But Tcomponent does not know how to deal with the specific features of your parts, so you have to describe it yourself.

3. Part writer's own definition.

In practical programming, parts are any element that can be inserted into the Delphi development environment. It may have various complexities of the program. In short, as long as you can fit into the component frame, the widget is everything you write in code. The part definition is just an interface description, and this chapter will elaborate on the part framework, explaining the limitations of the parts, just as the limitations of programming are explained. This chapter is not intended to teach you to write each part in the language you are giving, only to tell the method of coding the code and how to integrate the parts into the Delphi environment.

19.1.2 the difference between writing parts

There are three important differences between building parts in a Delphi environment and using parts in an application:

The process of writing a part is non-visual

Writing a part requires a deeper knowledge of the object

You need to follow more rules for writing parts

1. The writing part is non visual

The most obvious difference between writing a part and building a Delphi application is that the part is written entirely in the form of code, which is not visual. Because the visual design of Delphi application requires the completed parts, the creation of these parts will need to be written with object Pascal code.

Although you cannot use visual tools to build widgets, you can use all the programming features of the Delphi development environment, such as code editors, integrated debugging, and object browsing.

2. Writing parts requires a deeper knowledge of the object

In addition to non-visual programming, the biggest difference between building parts and using them is that when you create a new part, you need to inherit from the stored part to produce a new object type and add new properties and methods. On the other hand, component users, when establishing Delphi applications, only use existing parts. Customize their behavior at design time by changing part properties and describing how to respond to events.

When inheriting produces a new object, you have access to the part of the ancestor object that is not visible to the end user. These parts are called the protected interface. In most implementations, descendant objects also need to invoke the methods of their ancestor objects, so the authoring part should be quite familiar with object-oriented programming features.

3. Write parts to follow more rules

Writing a part process takes more traditional programming than visual application generation, and there are more rules to follow than using existing parts. Before you start writing your own parts, the most important thing is to be proficient with Delphi's own parts to get a visual understanding of the naming rules and the desired functionality of the parts users. The most important thing for parts users to expect is that they can do anything with the parts at any time. It is not difficult to write the parts that meet these expectations, as long as you anticipate and follow the rules.

19.1.3 the process of building parts

In short, the process of establishing a custom part consists of the following steps:

Create a library unit that contains a new part

To inherit a new part type from an existing part type

Adding properties, methods, and events

Register Parts with Delphi

To create a help file for a part's property methods and events

If you complete these tasks, the complete part contains the following 4 files

The Compiled library unit (. DCU file)

Select the Board bitmap (. DCR file)

Help file (. HLP file)

Help-keyword file (. KWF file)

19.2 Delphi Parts Programming method

19.2.1 Delphi Parts Programming Overview

19.2.1.1 Delphi Visual Part Class Library

The parts of Delphi are part of the object inheritance tree of the visual Part class library (VCL), and the relationships of the objects that make up VCL are listed below. Tcomponent is the common ancestor of every part of VCL. Tcomponent provides the most basic properties and events for the Delphi component to work correctly. The branches in the library provide additional, more single-minded functionality.

When a part is established, the new object is inherited from an object that is already in the object tree and is added to the VCL.

19.2.1.2 The starting point for building a part

A part is any program element that you want to manipulate at design time. Creating a new part means inheriting the new part object class from an existing type.

The main ways to create new parts are as follows:

Modify existing controls

Establish original control

Set up graphics control

To establish a subclass of Windows control

Building Non-visual Parts

The following table lists the starting classes for different ways to build

Table 19.1 defines the starting point of a part

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Path Initiation Class

─────────────────────────────

Modify an existing part any existing part, such as TButton, Tlistbox

or abstract part objects such as Tcustomlistbox

Establish the original control Tcustomcontrol

Set up graphics control Tgraphiccontrol

Set up the subclass of window control Twincontrol

Building Non-visual Parts Tcomponent

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

You can also inherit other objects that are not part, but you cannot manipulate them in the form design window. Delphi includes many of these objects, such as Tinifile, Tfont and so on.

1. Modification of existing controls

The easiest way to build a part is to inherit an existing, usable part and customize it. can inherit from any part provided by Delphi. For example, you can change the default property values for standard controls, such as TButton.

Some controls, such as the ListBox and grid, have many of the same variables, in which case Delphi provides an abstract control type from which many types can be customized. For example, you might want to create a special type of tlistbox that does not have some attributes of the standard Tlistbox, you cannot remove attributes from an ancestor type, so you need to inherit from a part higher than Tlistbox. For example Tcustomlistbox, the part implements all the properties of Tcustomlistbox but does not publish (Publishing) them. When you inherit from an abstract class such as Tcustomlistbox, you publish the attributes that you want to make available to protect them (protected).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.