Delphi foundation 3

xiaoxiao2021-03-06  56

One: What is Pascal?

The Pascal language is designed by Professor Niklaus Wirth of the Swiss Zurich Technology School in 1971.

2: What is Turbo Pascal?

Turbo Pascal is higher than assembly language and easier to use and learn, so the birth of Turbo Pascal language is special on a personal computer.

popular.

Three: What is the origin of Delphi with Pascal?

After the 9 versions of the Pascal language, Borland released Delphi in 1995 to transfer PasCal into a visual programming language.

One: Object Pascal language lexical

From this chapter, you will have a detailed introduction to the grammar of Object Pascal. If you are still a delphi beginner, you should

Solution to grasp these basic content. In fact, a lot of content inside is not only for Delphi, but also the concept of developing a lot of OOP.

A quite good boot.

I will introduce you to Delphi's lexical articles, this article explains the vast grammatical unit of Object Pascal, which is correct writer.

The order is very important.

In the explanation of the operator, it is also briefly referred to the issue of data type operations.

Comment

Identifier

Assignment operator

@ 标 标

Character pointer operator

Logical operator

Type forced converter

Collection operator

Reserve words and special symbols

Operator

Arithmetic operation operator

Bit operator

Relationship operation operator

Operation type identification operator

String operator

Variable type operation

The above is the content that will be described, which will be described in detail below.

(1) Note

Note The readability of the program is used to increase the program, and you should develop habits that do notices. In Object Pascal, the comment is enclosed in {and} or (* and *)

A word, if only one line is in the comment, or use // as C , as the start, as in the following example:

Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);

Begin

Caption: = 'New Title'; {Title of Form1 is changed to 'New Title'}

Color: = CLBLUE; (* change the background color of Form1 to blue *)

Height: = 100; // change the height of FORM1 to 100

END;

Note You can appear in any space, and the compiler will automatically ignore the comment when the compiler is compiled.

Note: You cannot} or * in the contents of the comment, because the compiler ends them as a comment, and the comments cannot be nested.

There is a special case, if {or (* is tightening with a $ symbol (no space), the compiler uses it as a compilation switch, not comment

.

(2) Identifier OBJECT

The Pascal language is made of various lexical units such as constants, variables, types, processes, functions, units, etc., to use these words in the program.

The method is first defined by defining the identifier. The so-called identifier is to take a name to these lexical units to distinguish between other lexical units.

In Object Pascal, define the identifier to pay attention to:

(1) The identifier does not distinguish between, such as MyProc and MyProc are the same identifier.

(2) The identifier can only be headed by letters or underline, and cannot be numbers.

(3) The identifier can be any length, but only 63 is effective. (4) There is no space in the middle of the identifier.

(5) Do not allow the reserved word of the Object Pascal language as an identifier such as for, do, etc. (see the reserved word and compilation instruction), try not to

To use a predefined standard identifier as your own identifier, such as Integer, Word, True, False, etc., because it may cause confusion. (6) In Object Pascal, there are many objects that have the same identifier, etc., when references these features or methods, to limit

Set symbol, for example:

Panel1.caption: = '...';

Button1.caption: = '...';

In the above example, it is a modification of the CAPTION characteristic, but one is panel1 and the other is Button1.

(3) @ identifier

@ Is a one-dollar operator, used to get an operand address pointer, the operand can be a variable, process, function, and method in the class type, program example

as follows:

Procedure ChangeValue (x: integer);

VAR PTR: ^ integer;

Begin

PTR: = @ x;

Writeln (PTR ^);

PTR ^: = 15;

END;

Suppose the main program is as follows:

Var param: integer;

Begin

Param: = 10;

Changevalue (param);

Writeln (param); {10}

END;

In the above example, the CHANGEVALUE process first declares a pointer PTR pointing to the shaping number, and then takes out the x's address to the PTR pointer with the @ operator.

And display the number of PTR pointer points to the last change. But pay attention: After the ChangeValue is called, although the value of the parameter X is changed to

15, but the value of the arrameters of the param is still 10.

three

When the operand is a variable, the @ operator returns to the pointer to this number.

When the operand is the numerical parameter of the process or function, the @ operator returns the address in the shape in the stack.

When the operand is a variable parameter of the process or function, the @ operator returns to the address of the argument.

When the operand is a process or function, the @ operator returns the entry of the process or function, which is usually used to pass a process or function to the compilation.

The routine written by the language. Some API functions require an entry of a function address or use it.

⑷ Assignment Operator In Object Pascal, the assignment operator is: =.

Be careful not to be confused with comparison symbols, in most development languages, there is no distinguishing between these. Like C and VB and other programs, assignment numbers and ratios

The next is the same.

⑸ Logic operator logic operator operand is Boolean expression,

The result is also a Boolean value, which can be thought that the logical operator constitutes a composite Boolean expression.

Other logical operators are two yuan operators except for NOTs.

NOT (logic is not) turning true into false, turn false into true, for example:

IF not (x> y) Then ... {When X is not more than y ...}

And (logic and), which is equivalent to "and" meaning in Chinese, for example:

IF (x> y) and (m> n) Then ... {When x is greater than y and M is greater than n, ...}

OR (logic or), "or" meaning in Chinese, for example:

IF (x> y) or (m> n) Then ... {When X is greater than Y or M greater than n ...}

XOR (Logical Digital or) When the operand on both sides is true for true, the result is true, and when the number of operands on both sides are true or false, the result is FALSE.

There is a compilation switch $ b in Object Pascal. The default is {$ b-} status, when the composite Boolean expression value is already obvious, don't

The value of each operand is then calculated, and when one of the operands contain a function call, this mode can save time and code length. Compile

When the value of {$ b }, even if the value of the composite Boolean expression is obvious, the value of each operand is still calculated, including function calls.

⑹ Character pointer operator

See the data type PCHAR for the concept of character pointers.

The character pointer operator is and -, which is used to increase the pointer or reduce an offset, if two character pointers are reduced, resulting in two pointers

the distance between.

The usage of the character pointer operator will be given below, assuming that P and Q are two character pointers, k is a constant number,

P K, generate a new pointer, pointing to K characters behind the P.

P-K, generate a new pointer, pointing to K characters in front of P.

P-Q, calculate the distance between the two pointers.

Note: P and Q must be a pointer to the same character array to perform P-Q calculations, otherwise the result is meaningless.

⑺ Type Forced Converter Object Pascal provides a special type forced converter AS, whose syntax is as follows:

Object AS object type

For example, the above example can be changed to:

Procedure TFORM1.CLICK (Sender: TOBJECT);

Begin

IF sender is tbutton dam .height: = 50;

END;

In the above example, the Sender converted to the TButton type through the AS operator.

The AS operator is usually used in the WITH statement, and the program is now as follows:

Procedure TFORM1.CLICK (Sender: TOBJECT);

Begin

IF sender is tbutton dam .height: = 50;

END;

Pay attention to two points using the AS operator:

First, its priority is low than the reference operator, so if you want to reference a target, you must use parentheses, for example:

(Sender as tbutton) .caption: = '& mybutton';

Second, pay attention to the operand on both sides of the operator must be the type of assignment, otherwise the conversion may cause an exception.

⑻ Collection Operator Collection Operator is used to perform some operations for the data of the collection type.

Note: Although the collection operator borrows a symbol of the arithmetic operator, the symbol of the arithmetic operator is, but the arithmetic operation is not possible to the collection type.

Some symbols have special meaning, and we will explain the usage of these operators. Suppose there are two sets A and B:

A b results are a collection of elements that belong to set A or collections B (panel).

A-B results are a collection of elements that are not part of the collection B in the set A.

A * B The result is a collection (intersection) of the elements of collecting A and collections B.

There is also a special collection operator is in (or said a relational operator), used to determine if an element is in a collection, its result is Boolean

Value, for example:

Read (CH);

IF chin ['a' .. 'z'] the ...

In the above example, first read a character, then determine if this character is in a collection. ⑼ Reserved words and special symbols

The so-called reserved word is the words with special and fixed meaning in the Object Pascal language, such as For, and, IF, Begin, etc. Object.

Pascal does not allow the reserved word as an identifier.

Since Object Pascal is not case sensitive, the reserved word is not case sensitive, such as the AND and AND the same word.

By default, Delphi's code editor displays a reserved word with a black body to distinguish between other languages.

In addition to reservation, Object Pascal also predefines some special symbols as shown below:

- * / = <> []., (): ^ @ {} $ # <=> =: = .. (*)

(.)

Some of them are as an operator, and some are used as a delimiter.

⑽ Operator Object Pascal provides the following basic operations (arranged by priority):

Top priority: @ not

Secondary priority: * / div MOD AS AND SHL SHR

Priority again: - or xor

Minimum priority: = <> <> <=> = in is

The operation is particularly important to pay attention to the priority of the operator, which is generally such a principle:

This operand belongs to an operator with high priority when the priority of the operator on both sides is different. For example: 2 3 * 4 results are 14

Instead of 20.

This operand belongs to the operator on the left side of the operator on the operator on both sides. For example: 10 MOD 3 * 2 results

2, not 4.

The parentheses have the highest priority, and the expression in parentheses can be used as an operand.

Each operator is described below.

1 arithmetic operation operator

The arithmetic operation operator is a two-dimensional operator for performing operations such as addition and subtraction, and 6 arithmetic operation operators in Object Pascal:

(Plus sign) - (minus sign) * (Multiply)

The above three operators, when the two operands are integers, the results of operations are integers, and the number of operands as long as one of the operations is the real number.

/ (Summary) Regardless of the operand is an integer or a real number, the operation results are real.

DIV (integer), the two operands must be an integer, the result is the integer part of the merchant

MOD, the two operands must be an integer, the result is the remainder of the two numbers.

Note: The number of operators on the right of the above operators cannot be 0, otherwise an exception will be triggered. Object Object

Pascal stipulates that when the model is performed, when the number of operands on the left is positive, the result of the number of operands on the left is negative, and the divisor

Not related. For example: -28 MOD 3 results are -1, 28 mod -3 results 1.

In addition, the three arithmetic operators can be treated as other operators later.

2 Operators In addition to NOT is a one-dollar operator, other bit operators are two-dimensional operators, and their operands can only be integer, and the results are integers.

NOT (bits are not), that is, to reverse each bit of the operand, 1 becomes 0,0 to 1, for example, NOT 15 is 240. (15 of the binary value of 00001111, the binary value of 240 is 11110000).

And (bits), when both bits are 1, the bit of the result is 1.

OR (bits or), when there is one in the two bits 1, the bit of the result is 1.

XOR (press or), when two bits are different, the bit of the result is 1.

SHL (left shift), for example: 2 SHL 7 results are 256 (equivalent to E1 multiplied by E2 at 2, wherein E1 is the number of operands on the left, E2 is the right side

Number. (2 of the binary value of 10,256 is 100000000).

SHR (right shift), for example: 256 SHR 7 results 2 (equivalent to E1 divided by an integer part of E2).

3 The relationship operation operator relationship is compared to the two identical types of operands, returns a Boolean value based on the comparison result.

=, Determine if the two operands are equal, the operands can be simple types, classes, class references, pointers, collections, strings, or variable types.

<>, It is determined whether the two operands are not equal, and the operands can be simple types, classes, class references, pointers, collections, strings, or variable types.

<, Determining if the number of operands on the left is less than the number of operands, the number of operands can be simple type, string, pchar or variable type

>, Determine if the number of operands above the left is greater than the number of operands, the number of operands can be simple type, string, pchar, or variable type.

<=, It is judged whether or not the number of operands in the left is less than or equal to the right number of operands, and the number of operands may be simple type, string, pchar, or variable type.

> =, Determine if the number of operands above the left is greater than or equal to the right operand, and the number of operands can be simple type, string, pchar, or variable type.

<=, A comparison for two set types.

> = For comparison of two set types.

⒒ Operation type identification operator

The IS is an operator for access to an object type in the runtime period, which is a Boolean value.

When the object is the specified object type or the inheritance type of the object type, the result of the IS is True, which is false.

The IS operator is actually a relational operator, so the IS operator is usually used in the IF statement, and the program is now as follows:

Procedure TFORM1.CLICK (Sender: TOBJECT);

Begin

If sender is tbutton dam. HEIGHT: = 50;

END;

In the above example, if the object that generates this event is TButton, the sender is forced to convert, and the height of the button is changed to 50. note,

Even if Sender is TButton type, it is not possible to directly reference Sender's characteristics, and must be enforced to convert the type.

Also note: The priority and relational operators of the IS are the same level.

⒓ String Operator Operator can also be used for strings or characters, and the result is a string.

If the two operands are short-string, the additional result is more than 255 characters, and the exceeding portion will be cut off. If two operands

One is a long string, the result is a long string (see the concept of the long-short string, see the data type). The program is now as follows:

The name of the Caption: = 'window is:' 'myWindow';

In the above example, add two strings and assign it to the CAPTION;

⒔ variable type operation

Object Pascal provides a new data type Variant, running this type of operation with , -, *, /, div, mod, sh, shr,

And, OR, XOR, NOT, etc.

The next chapter will introduce: Object Pascal language data type

Attached Delphi TIPS:

(I will attach a more useful TIPS for everyone after each chapter, these TIPs are actually used, I believe that I can help everyone.

Actually solve some problems. )

How to choose a computer in the network neighbor?

Category: System

Uses

SHLOBJ, ACTIVEX;

Function BrowseComputer (Dialogtitle: String; Var Compname: String): Boolean;

VAR

Browseinfo: TBROWSEINFO;

ItemidList: pitemidlist;

Computername: array [0..max_path] of char;

Title: String;

Windowlist: Pointer;

Shellmalloc: IMALLOC;

Begin

IF failed (SHGETSPECIALFOLDERLOCATION (Application.handle, CSIDL_NETWORK, ITEMIDLIST))

Raise Exception.create ('Unable Open Browse Computer Dialog');

Try

Fillchar (Browseinfo, Sizeof (Browseinfo), 0);

Browseinfo.hwndowner: = Application.handle;

Browseinfo.pidlroot: = itemidlist;

Browseinfo.pszdisplayName: = computername;

Title: = dialogtitle;

Browseinfo.lpsztitle: = pchar (Pointer);

Browseinfo.ulflags: = Bif_BrowseForComputer;

Windowlist: = disabletaskwindows (0);

Try

RESULT: = shbrowseforfolder (browseinfo) <> nil;

Finally

EnabletaskWindows (Windowlist);

END;

If Result the Compname: = computername;

Finally

If succeeded (SHGETMALLOC (shellmalloc))

Shellmalloc.free (itemidlist);

END;

END;

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

New Post(0)