AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Remove tray icon when killing program

Ein Thema von WojTec · begonnen am 17. Okt 2013 · letzter Beitrag vom 25. Jan 2014
Antwort Antwort
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
44.372 Beiträge
 
Delphi 12 Athens
 
#1

AW: Remove tray icon when killing program

  Alt 18. Okt 2013, 08:50
TerminateProcess ist nicht der "normale" Weg, um eine Anwendung zu beenden.
Man könnte natürlich auch versuchen sein Programm so zuverlässig zu schreiben, daß ein derartiger Schritt nicht notwendig ist.
Ein Therapeut entspricht 1024 Gigapeut.
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
482 Beiträge
 
Delphi XE6 Professional
 
#2

Re: Remove tray icon when killing program

  Alt 18. Okt 2013, 09:59
Windows don't have any routine to refresh tray, like graphical objects has in Delphi? How it do it when program remove icon themself?

Ok, anyway, I have another idea, but don't know how to code it: send close command and whait let's say 2secs to execute, if not executed then kill it. 1: how to perform close command when only icon in try? 2: How to wait for execution n seconds?
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.659 Beiträge
 
Delphi 12 Athens
 
#3

AW: Remove tray icon when killing program

  Alt 18. Okt 2013, 10:10
Just a few thoughts: you know the ProcessHandle, which is needed for TerminateProcess. To retrieve the corresponding ProcessID you could call OpenProcess. Once you have this ID you could call EnumWindows and within its callback compare your ProcessID to the one of the current window using GetWindowThreadProcessId. If they match, send your Close-Command to that window. I think this should work, but did not try it.
Detlef
"Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen

Geändert von DeddyH (18. Okt 2013 um 10:32 Uhr) Grund: I confounded ProcessID and ProcessHandle, corrected
  Mit Zitat antworten Zitat
Der schöne Günther

Registriert seit: 6. Mär 2013
6.199 Beiträge
 
Delphi 10 Seattle Enterprise
 
#4

AW: Remove tray icon when killing program

  Alt 18. Okt 2013, 10:27
That might indeed sound pretty complicated but there are a lot of free code snippets for "How do I find a window handle if I only have the process ID?". It's also what I'd strongly suggest since TerminateProcess is pretty much like shooting someone in the head... from behind.

Always give the other application a fair change to shut down and clean up. This also includes the remaining unwanted tray icon (blood splatter?).

Here's an excerpt of how I search for the Window handle of a process I only have the ID of for later sending a WM_CLOSE message. I believe it has a few shortcomings like only returning the first window that matches but it always served me well.

Delphi-Quellcode:
   TEnumInfo = record
      ProcessID: DWORD;
      HWND: THandle;
   end;

   //Prozess-ID muss stimmen. Das Fenster muss nicht aktiviert oder sichtbar sein!
   function EnumWindowsProc(Wnd: DWORD; var EI: TEnumInfo): Bool; stdcall;
   var
      PID: DWORD;
   begin
      GetWindowThreadProcessID(Wnd, @PID);
      Result := (PID <> EI.ProcessID);
      //or IsWindowVisible(WND)) or (not IsWindowEnabled(WND));

      if not Result then EI.HWND := WND; //Die Enumeration durch alle Fenster
      // geht NUR weiter, wenn diese Funktion TRUE liefert!
   end;

   //Wird kein Fenster gefunden, ist die Rückgabe 0
   function FindFirstWindow(PID: DWORD): DWORD;
   var
      EI: TEnumInfo;
   begin
      EI.ProcessID := PID;
      EI.HWND := 0;
      EnumWindows(@EnumWindowsProc, Integer(@EI));
      Result := EI.HWND;
   end;
Most of it was probably "borrowed" from this very forum.

You can then just get the Windowhandle
evilWindowHandle := FindFirstWindow(evilProcess.dwProcessId); and send a WM_CLOSE: PostMessage(evilWindowHandle, WM_CLOSE, LPARAM(False), WPARAM(False));
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
482 Beiträge
 
Delphi XE6 Professional
 
#5

Re: Remove tray icon when killing program

  Alt 18. Okt 2013, 10:33
But in most cases closing window (if there is tray icon) don't closing program, aren't they? I try write something, I'll ask if next problem occur
  Mit Zitat antworten Zitat
Der schöne Günther

Registriert seit: 6. Mär 2013
6.199 Beiträge
 
Delphi 10 Seattle Enterprise
 
#6

AW: Remove tray icon when killing program

  Alt 18. Okt 2013, 10:38
If the application is going to shut down, it has to take care of removing the tray icon. A well written application should do that. Let's hope for the best.

However, I sometimes have indeed dead tray icons lying around that vanish when you touch them. Looks like a rather common problem
Also, have a look at this post on StackOverflow. It really looks like you can either hope that the application cleans up after itself (if you just let it) or just accept your fate.

Geändert von Der schöne Günther (18. Okt 2013 um 10:40 Uhr)
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.659 Beiträge
 
Delphi 12 Athens
 
#7

AW: Remove tray icon when killing program

  Alt 18. Okt 2013, 10:40
I have found an interesting thread concerning the problem (except the tray icon): https://forums.embarcadero.com/threa...threadID=66986
Detlef
"Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen
  Mit Zitat antworten Zitat
Benutzerbild von jaenicke
jaenicke

Registriert seit: 10. Jun 2003
Ort: Berlin
9.960 Beiträge
 
Delphi 12 Athens
 
#8

AW: Remove tray icon when killing program

  Alt 18. Okt 2013, 10:46
The application has to have a window handle and a message loop. The window does not have to be visible. So the solution, which should work in most if not all cases, would be to send WM_CLOSE to alle window handles found for the application.
Sebastian Jänicke
AppCentral
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 14:04 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz