Difference between revisions of "callback"
From Lazarus wiki
Jump to navigationJump to search (Created page with "'''Callback''' functions are in widespread usage in Lazarus; in fact any Event is made available using 'callback'. type TForm = class(TForm) btn: TButton; proc...") |
|||
Line 1: | Line 1: | ||
'''Callback''' functions are in widespread usage in Lazarus; in fact any [[Event]] is made available using 'callback'. | '''Callback''' functions are in widespread usage in Lazarus; in fact any [[Event]] is made available using 'callback'. | ||
− | + | <syntaxhighlight lang=pascal> | |
type | type | ||
TForm = class(TForm) | TForm = class(TForm) | ||
Line 11: | Line 11: | ||
// dosomething | // dosomething | ||
end; | end; | ||
+ | </syntaxhighlight> | ||
The ''btnClick'' method is a callback from within the TButton implementation. | The ''btnClick'' method is a callback from within the TButton implementation. | ||
Apart from their use in the userinterface of a form, callbacks are used in many [[Library|libraries]] that were linked. | Apart from their use in the userinterface of a form, callbacks are used in many [[Library|libraries]] that were linked. | ||
+ | |||
+ | == Callback from library == | ||
+ | |||
+ | |||
+ | <syntaxhighlight lang=pascal> | ||
+ | |||
+ | TMyForm = class( TForm ) | ||
+ | progressbar: TProgressbar; | ||
+ | bIsAborted: boolean; | ||
+ | ... | ||
+ | end; | ||
+ | |||
+ | // the callback function as needed by CopyFileEx | ||
+ | function _CopyCallback( | ||
+ | TotalFileSize, TotalBytesTransferred, StreamSize, StreamBytesTransferred: Int64; | ||
+ | dwStreamNumber, dwCallbackReason: DWORD; | ||
+ | hSourceFile, hDestinationFile: THandle; | ||
+ | f: TMyForm ): DWORD; stdcall; | ||
+ | var | ||
+ | newpos: Integer; | ||
+ | const | ||
+ | PROCESS_CONTINUE = 0; | ||
+ | begin | ||
+ | result := PROCESS_CONTINUE; | ||
+ | if dwCallbackReason = CALLBACK_CHUNK_FINISHED then begin | ||
+ | newpos := Round( TotalBytesTransferred / TotalFileSize * 100 ); | ||
+ | with f.Progressbar do | ||
+ | if newpos <> Position then | ||
+ | Position := newpos; | ||
+ | Application.ProcessMessages(); | ||
+ | end; | ||
+ | end; | ||
+ | |||
+ | ... | ||
+ | |||
+ | // copy 'fromfile' to 'tofile' showing progress in myFor.Progressbar and obey to abort in myForm.bIsAborted | ||
+ | result := CopyFileEx( 'fromfile', 'tofile', @_CopyCallback, pointer(myForm), @myForm.bIsAborted, 0 ); | ||
+ | |||
+ | </syntaxhighlight> |
Revision as of 14:01, 15 April 2021
Callback functions are in widespread usage in Lazarus; in fact any Event is made available using 'callback'.
type
TForm = class(TForm)
btn: TButton;
procedure btnClick(Sender: TObject);
end;
...
procedure TForm.btnClick(Sender: TObject);
begin
// dosomething
end;
The btnClick method is a callback from within the TButton implementation.
Apart from their use in the userinterface of a form, callbacks are used in many libraries that were linked.
Callback from library
TMyForm = class( TForm )
progressbar: TProgressbar;
bIsAborted: boolean;
...
end;
// the callback function as needed by CopyFileEx
function _CopyCallback(
TotalFileSize, TotalBytesTransferred, StreamSize, StreamBytesTransferred: Int64;
dwStreamNumber, dwCallbackReason: DWORD;
hSourceFile, hDestinationFile: THandle;
f: TMyForm ): DWORD; stdcall;
var
newpos: Integer;
const
PROCESS_CONTINUE = 0;
begin
result := PROCESS_CONTINUE;
if dwCallbackReason = CALLBACK_CHUNK_FINISHED then begin
newpos := Round( TotalBytesTransferred / TotalFileSize * 100 );
with f.Progressbar do
if newpos <> Position then
Position := newpos;
Application.ProcessMessages();
end;
end;
...
// copy 'fromfile' to 'tofile' showing progress in myFor.Progressbar and obey to abort in myForm.bIsAborted
result := CopyFileEx( 'fromfile', 'tofile', @_CopyCallback, pointer(myForm), @myForm.bIsAborted, 0 );