Einzelnen Beitrag anzeigen

Benutzerbild von Mazel
Mazel

Registriert seit: 11. Nov 2005
Ort: Leipzig
831 Beiträge
 
#13

Re: Handle einer Form aus Taskbareintrag erhalten

  Alt 23. Apr 2007, 12:52
Das wär natürlich eine Idee.

Ich habe jetzt mal beide Codes zusammengefügt, da mir aufgefallen ist, dass mehrere Fragmente in beiden wiederholt werden, im Ergebnis hat sich nichts geändert: es wird immernoch ein Wert ausgegeben, kein Wnd-Handle ist.
Delphi-Quellcode:
{--[TaskbarEntry]--------------------------------------------------------------}

procedure TaskbarEntry;
var
 Pos : TPoint;
 // variables that needed to open taskbar (explorer) process
 TaskbarPID : DWORD; // TaskbarProcessID
 TaskbarPHandle : THandle; // TaskbarProcessHandle
 // variable that helds pointer to a remote buffer in taskbar process
 TaskbarPBuffer : Pointer; // TaskbarProcessBuffer
 lpPoint : Pointer;
 NumBytes : DWORD;
 ButtonIndex : Integer;
 tbButton : TTBButton;
 PtbButton : Pointer;
 dwBytesRead : DWORD;
 lpRemoteData : Pointer;
 localBuffer : array[0..BUFFER_SIZE - 1] of Byte;
 pLocalBuffer : ^Byte;
 ipLocalBuffer : Pointer;
 Wnd : HWND;

begin
 GetCursorPos(Pos);
 Windows.ScreenToClient(Main.TBHandle, Pos);
 TaskbarPID := 0;
 ButtonIndex := 0;
 // Main.TBHandle ist Taskbarhandle, welches beim Start der Anwendung ermittelt wird
 GetWindowThreadProcessId(Main.TBHandle, @TaskbarPID);
 TaskbarPHandle := OpenProcess(PROCESS_ALL_ACCESS, False, TaskbarPID);
 If TaskbarPHandle <> 0 then
  try
   lpPoint := VirtualAllocEx(TaskbarPHandle, nil, SizeOf(TPoint), MEM_RESERVE or MEM_COMMIT, PAGE_READWRITE);
   NumBytes := 0;
   WriteProcessMemory(TaskbarPHandle, lpPoint, @Pos, SizeOf(TPoint), NumBytes);
   // Index ermittlen
   ButtonIndex := SendMessage(Main.TBHandle, TB_HITTEST, 0, Integer(lpPoint));
   If Assigned(lpPoint) then VirtualFreeEx(TaskbarPHandle, lpPoint, 0, MEM_RELEASE);
  finally
   CloseHandle(TaskbarPHandle);
  end;
 TaskbarPID := 0;
 GetWindowThreadProcessId(Main.TBHandle, @TaskbarPID);
 TaskbarPHandle := OpenProcess(PROCESS_ALL_ACCESS, False, TaskbarPID);
 If TaskbarPHandle <> 0 then
  try
   // allocate space for remote buffer in taskbar process
   TaskbarPBuffer := VirtualAllocEx(TaskbarPHandle, nil, BUFFER_SIZE, MEM_COMMIT, PAGE_READWRITE);
   If Assigned(TaskbarPBuffer) then
    begin
     // declare and prepare variables that will held data
     // about PRESSED button in a taskbar
     PtbButton := @tbButton;
     // Now, retrieve information about the PRESSED button in a taskbar / data is placed in remote buffer
     SendMessage(Main.TBHandle, TB_GETBUTTON, ButtonIndex, Integer(TaskbarPBuffer));
     // And now, the remote data about PRESSED button is transferred to a local variable
     dwBytesRead := 0;
     ReadProcessMemory(TaskbarPHandle, TaskbarPBuffer, PtbButton, SizeOf(TBButton), dwBytesRead);
     // the window handle is in dwData field of TBBUTTON structure (the first 4 bytes)
     pLocalBuffer := @localBuffer[0];
     ipLocalBuffer := pLocalBuffer;
     // initialize remote buffer
     lpRemoteData := @tbButton.dwData;
     // and read the dwData fields of a TBBUTTON from remote process
     dwBytesRead := 0;
     ReadProcessMemory(TaskbarPHandle, lpRemoteData, ipLocalBuffer, SizeOf(Pointer), dwBytesRead);
     // obtain window handle, copy first 4 bytes
     Move(ipLocalBuffer^, Wnd, 4);
     If Assigned(TaskbarPBuffer) then VirtualFreeEx(TaskbarPHandle, TaskbarPBuffer, 0, MEM_RELEASE);
    end;
  finally
   CloseHandle(TaskbarPHandle);
  end;
 Main.Caption := inttostr(Wnd);
end;
Marcel Jänicke
www.mj-software.net
  Mit Zitat antworten Zitat