Para esse resultado, utilize as duas função publicado aqui no blog. Seguem elas:
Função 1 Função 2
Essa função retorna o Handle de uma janela, passando o PID do executável como parametro.
function GetHWNDFromPID(const PID: Cardinal): HWND;
var
MHandle: HWND;
MProcPID: Cardinal;
begin
Result := 0;
MHandle := GetTopWindow( 0 );
while Boolean( MHandle ) do
begin
{ ** O retorno do método não é necessário. Apenas o seu Handle ** }
GetWindowThreadProcessId( MHandle, MProcPid );
if MProcPid = PID then
begin
Result := MHandle;
Break;
end;
{ ** Recuperando a próxima janela ** }
MHandle := GetNextWindow( MHandle, GW_HWNDNEXT );
end;
end;
Retirado do ActiveDelphi
Essa função retorna o PID do executável, passando o nome do mesmo como parametro.
Add Uses TlHelp32
function GetPIDExecutavel(const ANomeExe: String):Cardinal;
var
MContinueLoop: BOOL;
MHandle: THandle;
MProcList: TProcessEntry32;
MLocated: Boolean;
begin
Result := 0;
MHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
MProcList.dwSize := SizeOf(MProcList);
MContinueLoop := Process32First(MHandle, MProcList);
while Integer(MContinueLoop) <> 0 do
begin
MLocated := ((UpperCase(ExtractFileName(MProcList.szExeFile)) =
UpperCase(ANomeExe)) or (UpperCase(MProcList.szExeFile) = UpperCase(ANomeExe)));
if MLocated then
begin
Result := MProcList.th32ProcessID;
Break;
end;
MContinueLoop := Process32Next( MHandle, MProcList );
end;
CloseHandle(MHandle);
end;
Retirado do ActiveDelphi
Instruções para deixar o formulário sempre a frente de outras janelas.
Self.FormStyle := fsStayOnTop;
Segue abaixo uma função que retornar o título da janela ativa.
function GetTituloAtivo: string;
var
Handle: THandle;
Len: LongInt;
Title: string;
begin
Result := '';
Handle := GetForegroundWindow;
if Handle <> 0 then
begin
Len := GetWindowTextLength(Handle) + 1;
SetLength(Title, Len);
GetWindowText(Handle, PChar(Title), Len);
Result := TrimRight(Title);
end;
end;