//还有别忘了在 uses 部分加上TLHelp32单元
function GetAppName(const AWindowHandle: THandle): string;
var
PI: DWORD;
ContinueLoop:BOOL;
SnapshotHandle:THandle;
ProcessEntry32:TProcessEntry32;
begin
Result := '';
GetWindowThreadProcessId(AWindowHandle, @PI);
SnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
ProcessEntry32.dwSize := Sizeof(ProcessEntry32);
ContinueLoop := Process32First(SnapshotHandle,ProcessEntry32);
while ContinueLoop do
begin
if ProcessEntry32.th32ProcessID = PI then
begin
Result := ProcessEntry32.szExeFile;
break;
end;
ContinueLoop:=Process32Next(SnapshotHandle, ProcessEntry32);
end;
CloseHandle(SnapshotHandle);
end;
用以上的方法找到窗口HANDLE和PROCESSID后,用GETWINDOWTHREADPROCESS这个API函数可以得到创建窗口的PROCESSID,再和PROCESSID比较一下,可以找到!