function FindIEWebBrowser: IWebBrowser2; function FindIEWindow(ParentHandle, ChildHandle: HWND): Boolean;
function CallScript: Boolean; end;
3.按Ctrl + Shift + C生成程序体代码,完善代码如下:
function TActiveFormX.FindIEWebBrowser: IWebBrowser2; var tmpShell: IShellWindows; tmpIntf: IDispatch; tmpIE: IWebBrowser2; i, Count: Integer; begin try if FWebBrowser = nil then begin Count := 0; repeat tmpSHell := CoShellWindows.Create; for i := 0 to tmpShell.Count - 1 do begin tmpIntf := tmpShell.Item(i); if tmpIntf = nil then continue; tmpIntf.QueryInterface(IID_IWebBrowser2, tmpIE); if tmpIE = nil then Continue; if (Integer(Handle) = tmpIE.HWND) or FindIEWindow(Integer(tmpIE.HWND), Handle) then FWebBrowser := tmpIE; end; Inc(Count); Sleep(50); Application.ProcessMessages; until (FWebBrowser <> nil) or (Count > 50); end; Result := FWebBrowser; except end; end;
function TActiveFormX.FindIEWindow(ParentHandle, ChildHandle: HWND): Boolean; var tmpHandle : HWND; begin tmpHandle := GetParent(ChildHandle); if tmpHandle = 0 then begin Result := False; Exit; end else begin if tmpHandle = ParentHandle then begin Result := True; Exit; end else begin Result := FindIEWindow(ParentHandle, tmpHandle); end; end; end;
function TActiveFormX.CallScript: Boolean; var tmpDocument2: IHTMLDocument2; tmpDispID: Integer; tmpScriptName: WideString; tmpDispParams: TDispParams; tmpResult: Variant; tmpParam1Value, tmpParam2Value: WideString; tmpExcepInfo: TExcepInfo; begin try tmpDocument2 := FindIEWebBrowser.Document as IHTMLDocument2; //获取角本函数的指针,角本函数名为"CallScript" tmpScriptName := 'CallScript'; OleCheck(tmpDocument2.Script.GetIDsOfNames(GUID_NULL, @tmpScriptName, 1, LOCALE_SYSTEM_DEFAULT, @tmpDispID));