Recent

Author Topic: About the "Application.OnDeactivate" event.  (Read 2011 times)

Veritas

  • Newbie
  • Posts: 2
About the "Application.OnDeactivate" event.
« on: January 20, 2018, 01:01:58 pm »
Hello!
My program should detect the deactivation of the application and respond to it.
The following code is used to perform the task:

Code: Pascal  [Select][+][-]
  1. type
  2.   { TForm1 }
  3.   TForm1 = class(TForm)
  4.     procedure FormCreate;
  5.     procedure ApplicationDeactivate(Sender: TObject);
  6.   private
  7.   public
  8.   end;
  9.  
  10. ...
  11.  
  12. procedure TForm1.ApplicationDeactivate(Sender: TObject);
  13. begin
  14.   SomeActions;
  15. end;
  16.  
  17. procedure TForm1.FormCreate;
  18. begin
  19.   Application.OnDeactivate := @ApplicationDeactivate;
  20. end;
  21.  

Question 1:
If I remove the "@" before "ApplicationDeactivate", it stops working. Why? After all, a reference to RAD Studio Delphi provides the following example:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   Application.OnDeactivate := AppDeactivate;
  4. end;
  5.  
  6. procedure TForm1.AppDeactivate(Sender: TObject);
  7. begin
  8.   Application.Minimize;
  9. end;
  10.  

And there is no "@" in it.

Question 2:
If I remove the parameter (Sender: TObject), then the procedure also stops working. Why? After all, "Sender" does not use anything in this case.
Please, explain to me the work of this code and tell me where I can read about these features.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: About the "Application.OnDeactivate" event.
« Reply #1 on: January 20, 2018, 01:30:38 pm »
FPC supports several modes, which have different syntax restrictions.

{$Mode objfpc} requires all method assignments in that unit to use the @ operator.
{$Mode delphi} is fully Delphi-compatible, and so like Delphi automatically supplies the needed address without use of the @ operator.

Methods in Object Pascal are typed, and so you can only assign a method to an event property if the two are type compatible (i.e. they are both methods, not regular procedures, and they have identical parameter signatures with identically declared parameters).
The compiler will baulk at the slightest difference such as one string parameter being declared const and the method being assigned having a string parameter that is not declared const.
This strictness of type matching is actually a boon, and saves the programmer from making subtle but clearly buggy errors.
So even if Sender is an unused parameter, it must be present to complete the identical-method-type requirement for an assignment to a property of type TNotifyEvent.
« Last Edit: January 20, 2018, 04:00:34 pm by howardpc »

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: About the "Application.OnDeactivate" event.
« Reply #2 on: January 20, 2018, 01:51:02 pm »
Here are the documentations of Free Pascal Compiler Modes:
https://www.freepascal.org/docs-html/user/userse33.html
http://wiki.freepascal.org/Compiler_Mode

Delphi's users should read this before start using Lazarus/FPC:
http://wiki.lazarus.freepascal.org/Lazarus_Documentation#Coming_from_Delphi

Veritas

  • Newbie
  • Posts: 2
Re: About the "Application.OnDeactivate" event.
« Reply #3 on: January 20, 2018, 02:09:52 pm »
howardpc and Handoko thank you for your reply!

 

TinyPortal © 2005-2018