Computer languages have developed from machine language to high-level language, from complex to simple (in fact, it is not simple), which reflects the development of computer science and technology. The more advanced a computer language is, the more abstract and human it is, the less it has to do with low-level hardware, and the more convenient it is to use. But no matter how advanced the computer language is, it is an abstraction of the operating system layer, so we can always find out the rules behind understanding high-level languages, that is: the codes written in high-level languages are only to describe people's needs, and these codes must pass " Translator "translates it into machine language form so that it can be recognized and executed by the computer. There are two so-called translation methods: one is the compilation method, in which the code is generated in machine language by the compiler in advance, and then scheduled and executed by the operating system, such as Delphi language, C++ language, etc.; the other is the interpretation method, which is an edge in the calculation. Interpretation and execution does not affect the target program, such as Basic language, scripting language, etc. The characteristics of the interpretation method are slow running speed and relatively high requirements on computer hardware.
Computer languages define rules that describe people's needs. Behind the language is a compiler or interpreter. The main job of a compiler or interpreter is to translate code and become the main channel for communication between humans and computers. In this way, when the operating system remains unchanged, various development tools show their unique abilities, but in the end they must generate computer executable code. Therefore, no matter which computer language a program is written in, whether it is good or bad depends largely on the compiler or interpreter of that language. Delphi's compiler is still the most advanced, best and most efficient compiler in the world.
Judging from the characteristics of high-level languages, they basically encapsulate the service interface provided by the operating system, and add their own language features on this basis, such as OOP, pointers, memory management modes, etc.
From the entire architecture of the Delphi language, when we write programs, we either use VCL directly, call the API provided by the operating system, use Com services, or directly use assembly language to complete our work.
The core and greatest thing about Delphi is the Visual Component Library (VCL - Visual Component Library) and the cross-platform component library (CLX - aComponent Library for Cross-Platform), which are similar to Microsoft MFC, but its architecture is at least two to three years more advanced than MFC. Three generations. When programming in Delphi, you can choose VCL or not start from scratch. Suppose you write a program that creates a window and displays "Hello world". If you don't use VCL, then you have to start by calling CreateWindow of the API. This is such a troublesome thing, but it depends on your requirements. This just shows that it is not impossible to use Delphi programming without VCL, such as console programs.
VCL is a powerful class library, which is the essence and crystallization of the object-oriented features of Delphi language. The class diagram structure is as follows:
This is an abbreviated class diagram structure, but it is very representative. From the class diagram, we can see that VCL has only one root - TObjct, and each of its derived classes has only one ancestor. This is the object-oriented feature of the Delphi language. The most original ancestor of all classes is TObjcet, and the inheritance method is single inheritance. What needs to be explained is:
l Although the VCL design is classic and huge, don’t be intimidated by this. To put it bluntly, it is a class library. It is a class library in which many related classes are put together to complete certain functions. These classes either encapsulate Windows APIs, call Windows COM services, or use assembly to operate directly. Memory. As mentioned before, we can write programs with or without VCL, which means that VCL is not necessary. If necessary, we can inherit and extend any of the classes to make it a class that meets our own requirements. For example, if we want to extend the TPanal class, the corresponding events (OnMouseEnter/OnMouseLeave) can be triggered when the mouse enters and leaves, such as the function of the QQ panel.
u Create a new unit from [File]->[New]->[Unit].
u Write the code as follows, and then save the unit as MyPanelUnit.
unit MyPanelUnit;
interface
uses Classes, ExtCtrls, Messages, Controls;
type
TMouseActionEvent = PRocedure(Sender: TObject) of object;
TMyPanel = class(TPanel)
private
FOnMouseEnter, FOnMouseLeave:
FouseActionEvent;
//Intercept mouse entry message
procedure WMMouseEnter (var Message:
TMessage); message CM_MOUSEENTER;
//Intercept the mouse move message
procedure WMMouseLeave (var Message:
TMessage); message CM_MOUSELEAVE;
published
property OnMouseEnter: TMouseActionEvent read
FOnMouseEnter write FOnMouseEnter;
property OnMouseLeave: TMouseActionEvent read
FOnMouseLeave write FOnMouseLeave;
end;
//Note that the "R" in "Register" must be capitalized. This is the only place in the Delphi language that requires attention to size.
procedure Register;
implementation
{TMyPanel}
procedure Register;
begin
//Register the component in Delphi's IDE and display it on the panel "MyControl"
RegisterComponents('MyControl', [TMyPanel]);
end;
procedure TMyPanel.WMMouseEnter(var Message: TMessage);
begin
//Determine whether there is code when the mouse enters, and if it is executed
if Assigned(FOnMouseEnter) then
FOnMouseEnter(Self);
end;
procedure TMyPanel.WMMouseLeave(var Message: TMessage);
begin
//Determine whether there is code when the mouse enters, and if it is executed
if Assigned(FOnMouseLeave) then
FOnMouseLeave(Self);
end;
end.
u Then click [Component]->[Install Component…], as shown below:
Select the unit we just created, MyPanelUnit.pas, in "Unit file name" and leave other options as default. Then click "OK", select "Yes" in "Package dclusr.bpk will be build then install, Continue?" and save it after compilation and installation. In this way, the TmyPanel control will be installed in Delphi. Scroll the Delphi control panel to the end, and you will see the control page MyControl as shown below:
u Then create a new project, click [File]->[New]->[application], drag MyPanel from the MyControl page to the Form, press the F11 key, and switch to the Events page, as follows:
Compare it with TPanel to see if there are more OnMouseEnter and OnMouseLeave events. Double-click OnMouseEnter and write the code as follows:
procedure TForm1.MyPanel1MouseEnter(Sender: TObject);
begin
ShowMessage('The mouse enters MyPanel1');
end;
Then press F9 to run, move the mouse to MyPanel1, and see the result.
It's that simple. We extended TPanel to have the ability to handle mouse entry and removal events, so VCL is not mysterious. Anyone can rewrite the components (classes) inside to make it what they want.
l Delphi supports interface inheritance, which in a sense implements multiple inheritance. For example, TComponent is defined as follows:
TComponent = class(TPersistent, IInterface,
IInterfaceComponentReference)
l Do not create instances of abstract classes. As long as one method in a class is an abstract method, then the class is an abstract class. For example, TStrings is defined as follows:
TStream = class(TObject)
private
…
protected
…
public
function Read(var Buffer; Count: Longint): Longint; virtual; abstract;
function Write(const Buffer; Count: Longint): Longint; virtual; abstract;
…
end;
As long as there is abstract keyword in the definition of a method, it is an abstract method. There is no point in using an abstract class to create an instance, because Read and Write are not implemented, and an example will appear if called.
(Unfinished, please refer to Behind the Scenes of Delphi---Beginner’s Reference Part 2 (2))
(If you need to reprint, please indicate the source and author http://haitian.myrice.com)