Einzelnen Beitrag anzeigen

Benutzerbild von toms
toms
(CodeLib-Manager)

Registriert seit: 10. Jun 2002
4.648 Beiträge
 
Delphi XE Professional
 
#5

Re: Tray-Icon entfernen nach Beenden des Prozesses

  Alt 4. Dez 2009, 01:08
Hallo,

Du könntest das TNA Icon vor dem Beenden des Prozesses verstecken. Nach dem Beenden des Prozesses ist es dann ganz weg.
// Achtung: Nur mit D6, Vista 32 getestet....

Delphi-Quellcode:
uses
  PsAPI, CommCtrl;

function HideTNAIcon(ProcPath: AnsiString; HideIcon: Boolean = True): Boolean;

  function GetProcPath(wnd: HWND): AnsiString;
  var
    hProc: HWND;
    ProcId, cbNeeded: DWord;
    hMod: HMODULE;
    ModuleName: array[0..MAX_PATH - 1] of Char;
  begin
    Result := '';
    GetWindowThreadProcessId(wnd, @ProcId);
    hProc := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, ProcId);
    if hProc <> 0 then
      try
        if EnumProcessModules(hProc, @hMod, SizeOf(hMod), cbNeeded) then
          if GetModuleFilenameEx(hProc, hMod, ModuleName, SizeOf(ModuleName)) > 0 then
            Result := string(ModuleName);
      finally
        CloseHandle(hProc);
      end;
  end;

  function FindToolBar: HWND;
  var
    wnd: HWND;
  begin
    Result := 0;
    wnd := FindWindow('Shell_TrayWnd', '');
    if wnd > 0 then
      wnd := FindWindowEx(wnd, 0, 'TrayNotifyWnd', nil);
    if wnd > 0 then
      wnd := FindWindowEx(wnd, 0, 'SysPager', nil);
    if wnd > 0 then
      Result := FindWindowEx(wnd, 0, 'ToolbarWindow32', nil);
  end;

  function FindTrayNotify: HWND;
  var
    wnd: HWND;
  begin
    Result := 0;
    wnd := FindWindow('Shell_TrayWnd', '');
    if wnd > 0 then
      Result := FindWindowEx(wnd, 0, 'TrayNotifyWnd', nil);
  end;

type
  TTrayData2 = record
    cbSize: DWORD;
    Wnd: HWND;
    uID: UINT;
    uFlags: UINT;
    uCallbackMessage: UINT;
    hIcon: HICON;
    szTip: array[0..127] of AnsiChar;
    dwState: DWORD;
    dwStateMask: DWORD;
    szInfo: array[0..255] of AnsiChar;
    uTimeout: UINT;
    szInfoTitle: array[0..63] of AnsiChar;
    dwInfoFlags: DWORD;
  end;

type
  TTrayData = record
    wnd: HWND;
    uID: UINT;
    uCallbackMessage: UINT;
    Reserved: array[0..1] of DWORD;
    hIco: HICON;
  end;
var
  pRem, pLoc: PTBButtonInfo;
  NumBytes, ProcId: Cardinal;
  count, loop: Integer;
  hProc, hToolBar, hTrayNotify: HWND;
  IconData: TTrayData; //TNotifyIconData;
  r, r2: TRect;
begin
  Result := False;
  hToolBar := FindToolBar;
  hTrayNotify := FindTrayNotify;
  if hToolBar > 0 then
  begin
    GetWindowThreadProcessId(hToolBar, @ProcId);
    hProc := OpenProcess(PROCESS_VM_OPERATION or PROCESS_VM_READ or
      PROCESS_VM_WRITE, False, ProcId);
    if hProc = 0 then
      MessageDlg(Format('Fehler bei OpenProcess %s',
        [SyserrorMessage(GetLastError)]), mtError, [mbOk], 0)
    else
      try
        pRem := VirtualAllocEx(hProc, nil, SizeOf(TBButtonInfo), MEM_COMMIT, PAGE_READWRITE);
        if Assigned(pRem) then
          try
            pLoc := VirtualAlloc(nil, SizeOf(TBButtonInfo), MEM_COMMIT, PAGE_READWRITE);
            if Assigned(pLoc) then
              try
                count := SendMessage(hToolBar, TB_BUTTONCOUNT, 0, 0);
                if count = 0 then
                  MessageDlg('Fehler bei TB_BUTTONCOUNT', mtError, [mbOk], 0);
                pLoc^.cbSize := SizeOf(TBButtonInfo);
                pLoc^.dwMask := TBIF_LPARAM or TBIF_STATE;
                if WriteProcessMemory(hProc, pRem, pLoc, SizeOf(TBButtonInfo), NumBytes) then
                  for loop := 0 to count do
                    if SendMessage(hToolBar, TB_GETBUTTONINFO, loop, Cardinal(pRem)) <> 0 then
                      if ReadProcessMemory(hProc, pRem, pLoc, SizeOf(TBButtonInfo), NumBytes) then
                        if ReadProcessMemory(hProc, Pointer(pLoc^.lParam),
                          @IconData.Wnd, SizeOf(TTrayData), NumBytes) then
                          if IsWindow(IconData.Wnd) then
                          begin
                            if SameText(ProcPath, GetProcPath(IconData.Wnd)) then
                            begin
                              if HideIcon then
                              begin
                                if pLoc^.fsState and TBSTATE_HIDDEN = 0 then //sichtbar
                                begin
                                  pLoc^.fsState := pLoc^.fsState or TBSTATE_HIDDEN;
                                  if WriteProcessMemory(hProc, pRem, pLoc, SizeOf(TBButtonInfo), NumBytes) then
                                    if SendMessage(hToolBar, TB_SETBUTTONINFO, loop, Cardinal(pRem)) <> 0 then
                                      Result := True;
                                end;
                              end
                              else
                              begin
                                if pLoc^.fsState and TBSTATE_HIDDEN > 0 then //unsichtbar
                                begin
                                  pLoc^.fsState := pLoc^.fsState and not TBSTATE_HIDDEN;
                                  if WriteProcessMemory(hProc, pRem, pLoc, SizeOf(TBButtonInfo), NumBytes) then
                                    if SendMessage(hToolBar, TB_SETBUTTONINFO, loop, Cardinal(pRem)) <> 0 then
                                      Result := True;
                                end;
                              end;
                              Break;
                            end;
                          end;
              finally
                VirtualFree(pLoc, 0, MEM_RELEASE);
              end;
          finally
            VirtualFreeEx(hProc, pRem, 0, MEM_RELEASE);
          end
        else
          MessageDlg(Format('Fehler bei VirtualAllocEx %s',
            [SyserrorMessage(getLastError)]), mtError, [mbOk], 0);
      finally
        CloseHandle(hProc);
      end;
  end
  else
    MessageDlg(Format('Kein gültiges Fenster Handle %d', [hToolBar]), mtError, [mbOk], 0);
end;
Thomas
  Mit Zitat antworten Zitat