Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected behaviour of Application.OnActivate in Delphi 7

I wrote a D7 application to test the behaviour of Application.OnActivate .
This is the relevant part :

procedure TMainForm.FormCreate (Sender: TObject);
begin
Memo1.Lines.Add (IntToStr (Memo1.Lines.Count + 1) + ' - MainForm.FormCreate - Begin');

  Application.OnActivate := AppActivate;

Memo1.Lines.Add (IntToStr (Memo1.Lines.Count + 1) + ' - MainForm.FormCreate - End');
end;


procedure TMainForm.AppActivate (Sender: TObject);
begin
Memo1.Lines.Add (IntToStr (Memo1.Lines.Count + 1) + ' - MainForm.AppActivate - Begin');

  ShowWidthsHeights (Sender);

Memo1.Lines.Add (IntToStr (Memo1.Lines.Count + 1) + ' - MainForm.AppActivate - End');
end;


procedure TMainForm.ShowWidthsHeights (Sender: TObject);

begin
Memo1.Lines.Add (IntToStr (Memo1.Lines.Count + 1) + ' - MainForm.ShowWidthsHeights -     Begin');

Memo1.Lines.Add (IntToStr (Memo1.Lines.Count + 1) + ' - MainForm.ShowWidthsHeights -     End');
end;

After starting the application , the content of Memo1 is :
1 - MainForm.FormCreate - Begin
2 - MainForm.FormCreate - End
3 - MainForm.AppActivate - Begin
4 - MainForm.ShowWidthsHeights - Begin
5 - MainForm.ShowWidthsHeights - End
6 - MainForm.AppActivate - End

That is correct .

But if I then click on a shortcut in the taskbar , these 4 lines are added to Memo1 :
7 - MainForm.AppActivate - Begin
8 - MainForm.ShowWidthsHeights - Begin
9 - MainForm.ShowWidthsHeights - End
10 - MainForm.AppActivate - End

Why does D7 do that ?
My application hasn't been activated , on the contrary , it has been deactivated !


To test if the Application.OnDeactivate event is also fired , I added this event handler :

procedure TMainForm.AppDeactivate(Sender: TObject);
begin
Memo1.Lines.Add (IntToStr (Memo1.Lines.Count + 1) + ' - MainForm.AppDeactivate - Begin');

Memo1.Lines.Add (IntToStr (Memo1.Lines.Count + 1) + ' - MainForm.AppDeactivate - End');
end;

and added this statement to TMainForm.FormCreate :

Application.OnDeactivate := AppDeactivate;  

After starting the application , Memo1 contained the same 6 lines as in the original case , but clicking on a shortcut in the taskbar resulted in 8 extra lines in Memo1 :

7 - MainForm.AppDeactivate - Begin  
8 - MainForm.AppDeactivate - End  
9 - MainForm.AppActivate - Begin  
10 - MainForm.ShowWidthsHeights - Begin  
11 - MainForm.ShowWidthsHeights - End  
12 - MainForm.AppActivate - End  
13 - MainForm.AppDeactivate - Begin  
14 - MainForm.AppDeactivate - End  

So , my application gets deactivated , then activated , and then deactivated again !
That's quite confusing !

like image 237
Ruud Schmeitz Avatar asked Jun 27 '12 09:06

Ruud Schmeitz


1 Answers

As the comment of David already stated, it is reflecting the receiving of the WM_ACTIVATE message. http://msdn.microsoft.com/en-us/library/windows/desktop/ms632614(v=vs.85).aspx gives the answer:

Sent when a window belonging to a different application than the active window is about to be activated. The message is sent to the application whose window is being activated and to the application whose window is being deactivated.

and the parameters of the message explain further:

wParam:

Indicates whether the window is being activated or deactivated. This parameter is TRUE if the window is being activated; it is FALSE if the window is being deactivated.

like image 137
R-D Avatar answered Sep 22 '22 08:09

R-D