Difference between revisions of "callback"
From Free Pascal wiki
Jump to navigationJump to search (Categorise page) |
|||
(One intermediate revision by one other user not shown) | |||
Line 19: | Line 19: | ||
== Callback from library == | == Callback from library == | ||
+ | <syntaxhighlight lang=C> | ||
+ | DWORD LpprogressRoutine( | ||
+ | LARGE_INTEGER TotalFileSize, | ||
+ | LARGE_INTEGER TotalBytesTransferred, | ||
+ | LARGE_INTEGER StreamSize, | ||
+ | LARGE_INTEGER StreamBytesTransferred, | ||
+ | DWORD dwStreamNumber, | ||
+ | DWORD dwCallbackReason, | ||
+ | HANDLE hSourceFile, | ||
+ | HANDLE hDestinationFile, | ||
+ | LPVOID lpData | ||
+ | ) | ||
+ | </syntaxhighlight> | ||
<syntaxhighlight lang=pascal> | <syntaxhighlight lang=pascal> | ||
Line 55: | Line 68: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | [[Category:Lazarus]] | ||
+ | [[Category:Development]] | ||
+ | [[Category:LCL]] |
Latest revision as of 05:28, 3 January 2022
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
DWORD LpprogressRoutine(
LARGE_INTEGER TotalFileSize,
LARGE_INTEGER TotalBytesTransferred,
LARGE_INTEGER StreamSize,
LARGE_INTEGER StreamBytesTransferred,
DWORD dwStreamNumber,
DWORD dwCallbackReason,
HANDLE hSourceFile,
HANDLE hDestinationFile,
LPVOID lpData
)
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 );