Deep Delphi (1)
Project Files
By Machine
Delphi is my favorite programming language, powerful, in general, can be replaced by c, and open all control source code, it is easy to write its own control. And my favorite place is that the compilation is very fast (Borland said Delphi compiler is the fastest in the world), which can greatly improve software development speed, so I usually use Delphi without Borland C Builder. Ok, let's not say anything, why do I want to write this article? The Delphi tutorial on the market is enough, but there are friends complaining that many Delphi tutorials are almost, but they only have the use of controls. After reading, they still want to have a deeper understanding of Delphi, so I will plan It is like this, just like this, but if you have psychological preparation, I am really stinking, I don't know how to take into account various readers, so everyone may have to read a few times before I understand me. What do you want to say? The first thing to talk about is a structure of a Project in Delphi. The main document of the project is essential, extension is basically the same as the .dpr (Delphi Project), the format is basically the same as the general Unit (unit). Then, in this DPR, other units are generally referenced (using the "use" in Delphi, INCLUDE in C, which typically uses PAS as an extension, and the format is of course extended from the previous PASCAL. . The format of the DPR is basically the same as the format of the unit file, and DPR is the entrance to the entire engineering program, which is exactly the same as Pascal! This will be explained in detail later in this chapter. Delphi uses the menu command Project-> View source to see the contents in the DPR. In addition, if the Form (Form) is added, there will be some .dfm (Delphi form) file, in fact, a resource file containing the form and control properties, that is, can be opened like a RES file. When it comes to the RES file, this file is also common in the project, which contains the program icon, version information, etc. will later explain the use of this file in different occasions. Corresponding DPR files generally have a DOF (Delphi OptionFile) and a CFG file. The DOF file contains the compiler, the parameters of the connector, and other content that can be set in the Delphi menu command project-> Options. The CFG file is the command line parameter of the compiler and connector generated according to the DOF file. The last file is a DCU (Delphi Compiled Unit) file, which is a compiled unit file. According to the engineering compiler settings, it will generally contain debugging information. The default setting is the compiler to generate debug information to the DCU, but The connector does not connect the debug information to the exe. The above things may not be understood for a while, but don't matter, I just said. The above says that only DPR files are essential, that is, a project can only contain only one DPR file, like "Guangxiang Girl" and "Guangxiao Ghost". Here is a very simple example:
PROGRAM Project1;
Uses windows;
Begin Messagebox (0, 'file corrupted!', nil, mb_ok or mb_iconstop); END.
Compile running this project, you will find that the generated file is very small, less than 20K! Is it very enjoyable? Oops, forgot how to build a such project. When you create a new project in Delphi, you will automatically add a form and unit, but you can close the form and unit files, and ask if you save you if you save, then select Project-> in the menu. View Source Opens the project file, and finally modify the content inside to the above example! After this program is running, an error prompt box is displayed, just like "girl", after the determination, the program exits. The source code should be so easy to understand, it is obvious that the program starts from the Begin, ending at END. At the end, only one thing is done, that is, calling the MessageBox This API function displays a message box, because the MessageBox function is Declared in this unit of Windows, so you must add the cell name Windows to the USES statement. What is a declare? The statement is what it doesn't know likes to Delphi. In Delphi, you can declare a custom data type (DATA TYPE), a function, similar to the function, but no return value) and variable (variable) and constant. Many people can't understand the difference between the data type and the class. In fact, if the people who will C know, the Class is the same as Structure, but this data structure is very special, it is data Collection, that is, the class can contain multiple data fields. Record is also a collection of data types, but the class also has inheritance and other features (the word meaning of Class itself is grading, but I don't know who is first translated into class). Like this statement: Type Tcolor24 = Record Red: Byte; Green: Byte; Blue: Byte; End;
Define a new data type, indicating that this data type is TColor32, which is a record (RECORD), and defines the name and data type of its member content RED, Green, and Blue. In short, tell Delphi this data type. The structure so that the Delphi knows how to store the variable of this data type. The data type will be described in detail later.
Declaring data type Use Keyword Type, declare variables, use keyword VAR, such as:
Var rgbcolor: tcolor24;
Declare a variable RGBColor and indicate the data type of the variable to TCOLOR24 (the data type previously declared, the custom data type can be used as Delphi predefined). The so-called declaration variable is to tell Delphi to assign the amount of memory required for this variable (which is why you must specify the cause of the data type), and record the address of the memory to access the variable, access the corresponding Allocated memory. This correspondence is completed by the compiler, and the friends who learn VB may not be very concerned about the data type and memory allocation, but I think these is still very beneficial! Data types sometimes determine how the parameters in the function are passed, and this will be said later. Ok, let's finally say, the variable is just a name, deciding this variable characteristic is its data type.
The keywords that declare constant are const. The declaration constant is very similar to the declared variable, but except for the name and data type to specify the constant, it is necessary to specify the value of the constant, because the value of constant cannot be changed, and cannot be assigned in the program as variables: const rgbyellow: tcolor24 = (Red: 255; Green: 255; Blue: 0);
I think the statement of the decod constant in delphi is a bit blame, but don't take it. The keywords above are said, including Uses, Type, Var, Const, Begin, End. These keywords, and examples in the project, are already in the old Pascal, in one project. In the middle, these keywords must appear in a certain order, probably like this:
Program sample1;
Uses Windows, Sysutils;
Var s: string;
TYPE long = integer;
Const Errcode: long = -2147483640;
Begin S: = SYSERRORMESSAGE (Errcode); MessageBox (0, PCHAR (S), NIL, MB_OK or MB_ICONSTOP); END.
Program specifies the name of the project, this statement must appear, and the project name and engineering file name must be consistent, otherwise it will be wrong when compiling. Since the compilation order of Delphi is performed in writing in the written order in the source code, you must pay attention to the source code, you can only declare, then call, and you can't call, and then declare. For example, the Uses statement must be tight with the Program statement, which is before all statements other than the ProGram statement, which is a bit different from the include in c, and later know why it is in the format of Unit. Then the data type, variable, constant declaration, these three declaration order is not limited, and it can have multiple times, that is, there can be multiple type, var, and const statements, but it is also necessary to follow the rules of the first declaration. For example, the declaration of Errcode above uses the data type long, and long must declare the Errcode, so push. The final part of the project is the main section, starting with Begin, end. The end ended in the general function is a partition number, but the END of the main program segment is the end of the period, so that this rule is also generated from the old PASCAL, it is bored. The next chapter will explain the Unit format, which can be said to be the extension of the Project format, so everyone will understand the content of this chapter.