Delphi foundation: callback function and its use

xiaoxiao2021-03-06  21

1 Overview of the callback function is such a mechanism: the caller is initialized an object (here the object is a general referral, including objects, global functions, etc.), pass some parameters to the object, and call a call The function address that can be accessed is passed to the object. This function is a notification agreement between the caller and the caller. When the agreed event occurs, the called is called by the callback function address when the agreed event occurs. This way, the caller is in a thread, the caller being in another thread. Message: The message can also be seen as a form of callback because the message is also initialized by the caller to pass a handle and a message number to the caller, and send a message to the caller when the arbitraries occur when the agreed event occurs. In this way, the caller is in the main thread, and the caller is in the main thread or the working thread. Delphi Event Model: There are many visual components in the VCL of Delphi, are all using event models, such as TForm's oncreate events. The principle is: Specify event functions when designing, triggered at runtime events, will be called in design Event function. In mechanisms, the Delphi event model is the same as the callback. However, some differences in the specific form, the pure callback function is in the form of a global function, and the Delphi event is the form of an object method, that is, the following adjustment function type Type TCALLBACKFUNC = Procedure (PDATA: POINTER) of Object; 2 callback function instructions The callback function is mainly used in two occasions. The first is that some Windows API requirements use the callback function as its parameter address, and the other is a function of a user to use a callback function as its parameters. Address, for functions of the user's definition, usually used when the function in the dynamic connection library is called. For the following steps: 1. Define a callback function type, there is no difference in the definition of the general function process, but its definition must meet the function requirements of the callback function as needed, the only difference is Behind the definition of function or process must be called for Windows standard calls; example: Type THDFunction = function (i: integer; s: string): integer; stdcall; for the process of declaration: type twprocedure = procedure (S: string); stdcall 2, then define a corresponding function or process based on this protocol, for this function or process, the name is not required, the type of the function thereof and the type of return value must be exactly the same, for the process For example, it is only necessary to have its parameter type. Example: Defines a corresponding function and a corresponding process based on the original shape of the above functions and procedures.

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

New Post(0)