function GetHWNDofProcess(AProcessID: Longint): HWND; stdcall;
var
H: THandle;
function EnumWndProc(AWnd: HWND; AlParam: LPARAM): Bool; stdcall;
var
pID: Longint;
begin
Result := True;
GetWindowThreadProcessId(AWnd, @pID);
if pID = AProcessID then
begin
Result := False;
H := AWnd;
end else
Result := True;
end;
begin
H := 0;
EnumWindows(@EnumWndProc, 0);
if H > 0 then
Result := H;
end;
function GetIEUrlAddress(H: Cardinal): THandle; stdcall;
var
hWin: HWND;
begin
hWin := GetHWNDofProcess(H);
Result := FindWindowEx(hWin, 0, 'TEdit', 0);
end;
然后在线程里调用上面的函数:
procedure TUrlThread.Execute;
var
i: Integer;
hi: HWND;
begin
Lock;
try
hi := GetIEUrlAddress(ProcessInfo.dwProcessId);
while True do
begin
for i := 0 to UrlList.Count - 1 do
begin
if Terminated then Exit;
if hi <> 0 then
begin
SendMessage(hi, WM_SETTEXT, Integer(UrlList), 0);
SendMessage(hi, WM_SETTEXT, Integer(#13), 0);
end;
{用事件做等待,用作定时器, 间隔即超时时间}
WaitForSingleObject(hStopEvent, FreshInterval * 1000);
end;
end;
finally
Unlock;
end;
end;
发生一个奇怪的现象,如:明明传进去ProcessID是: 2898
但是判断时:
GetWindowThreadProcessId(AWnd, @pID);
if pID = AProcessID then
明明pID=2898,就是不执行:
if pID = AProcessID then
begin
Result := False;
H := AWnd;
end
谁知道为什么?