Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   GUI-Design mit VCL / FireMonkey / Common Controls (https://www.delphipraxis.net/18-gui-design-mit-vcl-firemonkey-common-controls/)
-   -   Click auf Taskbarbutton löst onDeactivate onActivate aus (https://www.delphipraxis.net/211917-click-auf-taskbarbutton-loest-ondeactivate-onactivate-aus.html)

v2afrank 21. Nov 2022 14:20

Click auf Taskbarbutton löst onDeactivate onActivate aus
 
Liste der Anhänge anzeigen (Anzahl: 1)
Ich habe hier eine Anwendung die eigentlich offline gehen soll sobald Sie den Fokus verliert. Jetzt war ein Kollege am Schimpfen das es nicht so funktioniert und wir konnten es sogar relativ einfach nachbauen.
In meinem Fall war es Delphi 2006, aber es ist sicherlich auch interessant ob es bei anderen Delphi Versionen auch so ist.
Und zwar habe ich einfach ein ApplicationEvent objekt genommen und in ein Memo den zeitstempel von onActivate bzw onDeActivate geschrieben. Wenn ich eine andere Anwendung aktiviere funktioniert es wie erwartet. gehe ich über Minimieren im Menü auch. Wenn ich aber das Programmicon in der Taskbar anklicke kommt die Deactivate Botschaft und sofort hinterher die activate. Warum ?

itblumi 21. Nov 2022 16:08

AW: Click auf Taskbarbutton löst onDeactivate onActivate aus
 
Hallo v2afrank,

ich kann dir leider nicht beantworten warum dies geschieht, aber ich kann dir einen workaround anbieten. Den Fehler konnte ich auch mit meinem XE6 nach stellen, aber ich denke das diese Events von Windows selbst ausgeführt werden und das Delphi das nicht kontrolliert. Ich kann mich da aber auch irren.
Da du aber weist das bei einem OnMinimize auch ein OnRestore folgen muss kannst du dies benutzen um den Fehler zu beheben und dann selbst das OnActivate Event ausführen.

Delphi-Quellcode:
  private
    { Private-Deklarationen }
    NeedRestore: Boolean;
  public
    { Public-Deklarationen }
  end;

var
  Form7: TForm7;

implementation

{$R *.dfm}

procedure TForm7.ApplicationEvents1Activate(Sender: TObject);
begin
  if (not NeedRestore) then
    Memo1.Lines.add('Activate: '+TimeToStr(Time));
end;

procedure TForm7.ApplicationEvents1Deactivate(Sender: TObject);
begin
  Memo1.Lines.add('Deactivate: '+TimeToStr(Time));
end;

procedure TForm7.ApplicationEvents1Minimize(Sender: TObject);
begin
  Memo1.Lines.add('Minimize: '+TimeToStr(Time));
  NeedRestore := True;
end;

procedure TForm7.ApplicationEvents1Restore(Sender: TObject);
begin
  Memo1.Lines.add('OnRestore: '+TimeToStr(Time));
  NeedRestore := False;
  if (Assigned(Application.OnActivate)) then
    Application.OnActivate(Application);
end;

itblumi 21. Nov 2022 17:36

AW: Click auf Taskbarbutton löst onDeactivate onActivate aus
 
Man kann dies auch ohne eine extra Variable umsetzen und ist dann auch etwas kürzer.

Delphi-Quellcode:
procedure TForm7.ApplicationEvents1Activate(Sender: TObject);
begin
  if (not Windows.IsIconic(Application.Handle)) then
    Memo1.Lines.add('Activate: '+TimeToStr(Time));
end;

procedure TForm7.ApplicationEvents1Deactivate(Sender: TObject);
begin
  Memo1.Lines.add('Deactivate: '+TimeToStr(Time));
end;

procedure TForm7.ApplicationEvents1Restore(Sender: TObject);
begin
  if (Assigned(Application.OnActivate)) then
    Application.OnActivate(Application);
end;

v2afrank 22. Nov 2022 05:43

AW: Click auf Taskbarbutton löst onDeactivate onActivate aus
 
Vielen Dank Euch beiden. Mit diesem Workaround funktioniert es. Ist aber interessant der Fehler.

v2afrank 22. Nov 2022 08:10

AW: Click auf Taskbarbutton löst onDeactivate onActivate aus
 
Ich habe das Ganze noch einmal mit C# nachgebildet (das Gleiche Verhalten) und daraufhin hier folgendes gefunden
You should be using WM_SETFOCUS and WM_KILLFOCUS to determine if your window has the focus.

With regard to WM_ACTIVEATEAPP, both wParam (TRUE/FALSE) and lParam (thread id) are important to the interpretation of that message. Review the docs, which also state "The message is sent to the application whose window is being activated and to the application whose window is being deactivated." So, under certain circumstances, you can expect to receive two WM_ACTIVATEAPP messages - but only one applicable to your thread.

Under certain circumstances, WM_ACTIVATE may also be sent twice, but, similar to WM_ACTIVATEAPP, you must look at all applicable parameters, i.e., wParam AND lParam, to determine the applicability to your app. EDIT: wParam and lParam do NOT have the same meaning for those messages.

As an example, I was able to duplicate the situation by having both App1 and notepad open on the desktop. App1 has the focus. Following a click of the taskbar icon, App1 is minimized and receives:

[columns = message, wParam, lParam]

WM_ACTIVATEAPP False 6856 // window in thread 6856 deactivated. I.e., App1
WM_KILLFOCUS
WM_ACTIVATEAPP True 0 // window in thread 0 activated - I assume notepad. In any case, not applicable to App1??

EDIT: I think the docs are confused (or, at least, confusing). They state: wParam is TRUE if the window is being activated; FALSE if deactivated. That part appears correct.

However, for lParam, the docs say if wParam is TRUE, lParam is the thread that owns the window being deactivated; and if wParam is FALSE, lParam is the thread that owns the window being activated. It appears that the interpretation of lParam should be the thread owner. Period. Without regard to wParam.

himitsu 22. Nov 2022 09:29

AW: Click auf Taskbarbutton löst onDeactivate onActivate aus
 
Zitat:

kann dir leider nicht beantworten warum dies geschieht
Warum nicht?

Jemand klickt wo anders hin, außerhalb des Programms und wundert sich dann, dass das andere "Programm" (Ja, rate mal was die Taskleiste ist) den Fokus bekommt und das Eigene ihn verliert. :stupid:

v2afrank 22. Nov 2022 09:30

AW: Click auf Taskbarbutton löst onDeactivate onActivate aus
 
Das wäre ja nicht das Problem, aber warum das das eigene Programm dann ein wm_activate bekommt war die Frage

itblumi 22. Nov 2022 10:26

AW: Click auf Taskbarbutton löst onDeactivate onActivate aus
 
Es scheint ein Delphi Bug zu sein denn Delphi wertet den Parameter einfach nicht aus und die Application wird auch wieder Active, was in diesem Fall einfach nicht der Fall ist.
Die Form macht das richtig und prüft den Parameter.

v2afrank 22. Nov 2022 10:30

AW: Click auf Taskbarbutton löst onDeactivate onActivate aus
 
Wie gesagt C# macht es auch so

itblumi 22. Nov 2022 11:02

AW: Click auf Taskbarbutton löst onDeactivate onActivate aus
 
https://learn.microsoft.com/en-us/wi...wm-activateapp

Also ich habe das mit dem wparam so verstanden das eigentlich die Application mit der Message deactiviert wird und in dem Fall ist auch der wparam False
und trotzdem wird die Application aktiviert. Deswegen sieht das für mich wie ein Bug aus, es kann aber auch andere Hintergründe haben die ich vllt. nicht berücksichtigt habe.


Alle Zeitangaben in WEZ +1. Es ist jetzt 10:10 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz