function htGetWindowProcessId(Handle: THandle): THandle; //获取指定窗口所在的进程ID.
var
PID: dword;
begin
if Handle <> 0 then
begin
GetWindowThreadProcessID(Handle, @PID);
Result := PID;
end
else Result := 0;
end;
function htGetWindowProcessPath(Handle: THandle): string; //获取指定窗口所在的进程的exe文件全路径.
var
Hand: THandle;
ModName: array[0..Max_Path - 1] of Char;
hMod: HModule;
n, ProcessID: DWORD;
begin
Result := '';
ProcessID := htGetWindowProcessId(Handle);
Hand := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, ProcessID);
if Hand > 0 then
try
ENumProcessModules(Hand, @hMod, Sizeof(hMod), n);
if GetModuleFileNameEx(Hand, hMod, ModName, Sizeof(ModName)) > 0 then
Result := ModName; //得到路径和文见名
except
end;
end;
procedure TForm1.tmr1Timer(Sender: TObject);
var
s: string;
i: integer;
it: IStrIntMapIterator;
begin
mmo1.Clear;
s := htGetWindowProcessPath(GetForegroundWindow());
caption := s;
st.Add(s);
// s := s + ',' + datetimetostr(now);
if timemap.Count(s) = 0 then
timemap.Items[s] := 0
else
timemap.Items[s] := timemap.Items[s] + 1;
for i := 0 to st.Count - 1 do
begin
it := timemap.Find(st.Strings[i]);
mmo1.Lines.Add(it.Key + ':' + inttostr(it.GetValue));
end;
end;