callback: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: callback
| Old | New | Line | |
|---|---|---|---|
| 1 | + | **Callback** functions are in widespread usage in Lazarus; in fact any [[Event]] is made available using 'callback'. | |
| 2 | + | ||
| 3 | + | ``` pascal | |
| 4 | + | type | |
| 5 | + | TForm = class(TForm) | |
| 6 | + | btn: TButton; | |
| 7 | + | procedure btnClick(Sender: TObject); | |
| 8 | + | end; | |
| 9 | + | ... | |
| 10 | + | procedure TForm.btnClick(Sender: TObject); | |
| 11 | + | begin | |
| 12 | + | // dosomething | |
| 13 | + | end; | |
| 14 | + | ``` | |
| 15 | + | ||
| 16 | + | The *btnClick* method is a callback from within the TButton implementation. | |
| 17 | + | ||
| 18 | + | Apart from their use in the userinterface of a form, callbacks are used in many [[Library|libraries]] that were linked. | |
| 19 | + | ||
| 20 | + | ## Callback from library | |
| 21 | + | ||
| 22 | + | ``` C | |
| 23 | + | DWORD LpprogressRoutine( | |
| 24 | + | LARGE_INTEGER TotalFileSize, | |
| 25 | + | LARGE_INTEGER TotalBytesTransferred, | |
| 26 | + | LARGE_INTEGER StreamSize, | |
| 27 | + | LARGE_INTEGER StreamBytesTransferred, | |
| 28 | + | DWORD dwStreamNumber, | |
| 29 | + | DWORD dwCallbackReason, | |
| 30 | + | HANDLE hSourceFile, | |
| 31 | + | HANDLE hDestinationFile, | |
| 32 | + | LPVOID lpData | |
| 33 | + | ) | |
| 34 | + | ``` | |
| 35 | + | ||
| 36 | + | ``` pascal | |
| 37 | + | ||
| 38 | + | TMyForm = class( TForm ) | |
| 39 | + | progressbar: TProgressbar; | |
| 40 | + | bIsAborted: boolean; | |
| 41 | + | ... | |
| 42 | + | end; | |
| 43 | + | ||
| 44 | + | // the callback function as needed by CopyFileEx | |
| 45 | + | function _CopyCallback( | |
| 46 | + | TotalFileSize, TotalBytesTransferred, StreamSize, StreamBytesTransferred: Int64; | |
| 47 | + | dwStreamNumber, dwCallbackReason: DWORD; | |
| 48 | + | hSourceFile, hDestinationFile: THandle; | |
| 49 | + | f: TMyForm ): DWORD; stdcall; | |
| 50 | + | var | |
| 51 | + | newpos: Integer; | |
| 52 | + | const | |
| 53 | + | PROCESS_CONTINUE = 0; | |
| 54 | + | begin | |
| 55 | + | result := PROCESS_CONTINUE; | |
| 56 | + | if dwCallbackReason = CALLBACK_CHUNK_FINISHED then begin | |
| 57 | + | newpos := Round( TotalBytesTransferred / TotalFileSize * 100 ); | |
| 58 | + | with f.Progressbar do | |
| 59 | + | if newpos <> Position then | |
| 60 | + | Position := newpos; | |
| 61 | + | Application.ProcessMessages(); | |
| 62 | + | end; | |
| 63 | + | end; | |
| 64 | + | ||
| 65 | + | ... | |
| 66 | + | ||
| 67 | + | // copy 'fromfile' to 'tofile' showing progress in myFor.Progressbar and obey to abort in myForm.bIsAborted | |
| 68 | + | result := CopyFileEx( 'fromfile', 'tofile', @_CopyCallback, pointer(myForm), @myForm.bIsAborted, 0 ); | |
| 69 | + | ``` | |
| 70 | + | ||
| 71 | + | --- | |
| 72 | + | *From the Free Pascal wiki, page "callback", as it stood on 2022-01-03 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* | |
| 73 | + | Category: Development | |
| 74 | + | Category: LCL | |
| 75 | + | Category: Lazarus |
