如何取得当前打开的所有IE窗口正在访问的URL?(41分)

  • 主题发起人 主题发起人 ynduanlian
  • 开始时间 开始时间
Y

ynduanlian

Unregistered / Unconfirmed
GUEST, unregistred user!
通过findwindow找到IE窗口的HANLDLE, 再次窗口下枚举所有子窗口,找到地址栏的窗口句丙,取下内容。可以使用spy++辅助开发
 
unit WebListTrd;

interface

uses
Classes, ShDocVw, SysUtils, ActiveX;

type
TGetWebListThread = class(TThread)
private
function GetWebPointCollection(var nCount: Integer):IShellWindows;
function GetWebPageUrl(const shellWindows: IShellWindows; Index: Integer): string;
procedure CreateWebList(var sltWebList: TStringList);
protected
procedure Execute; override;
public
sltWebList:TStringList;
Constructor Create;
end;

implementation

constructor TGetWebListThread.Create;
begin
FreeOnTerminate:= True;
inherited Create(True);
Resume;
end;

procedure TGetWebListThread.Execute;
begin
CreateWebList(sltWebList);
end;

procedure TGetWebListThread.CreateWebList(var sltWebList: TStringList);
var
ShellWindows: IShellWindows;
URL: string;
I,nCount: integer;
begin
//Initializes the COM library
CoInitialize(nil);

//Clear UrlList
sltWebList:=TStringList.Create;
sltWebList.CaseSensitive:=false;
sltWebList.Clear;

// Try to get all IE windows point
ShellWindows:=GetWebPointCollection(nCount);

//Try to get url and add it to UrlList
for I:=0 to nCount-1 do
begin
URL:=GetWebPageUrl(ShellWindows,I);
if URL='' then continue;
sltWebList.Add(url)
end;

//Closes the COM library
CoUnInitialize;
end;

function TGetWebListThread.GetWebPointCollection(var nCount: Integer): IShellWindows;
begin
try
Result:=CoShellWindows.Create;
nCount:=Result.Count;
except
Result:=nil;
nCount:=0;
end;
end;

function TGetWebListThread.GetWebPageUrl(const shellWindows: IShellWindows; Index: Integer): string;

function IsUrlValid(URL: String):boolean;
begin
result:=(URL<>'')and(Pos('http://',LowerCase(Url))<>0);
end;

var
WebBrowser:IWebBrowser2;
url: string;
begin
try
WebBrowser:=shellWindows.Item(index) as IWebBrowser2;
except
WebBrowser:=nil;
end;

if WebBrowser=nil then
url:=''
else
url:=WebBrowser.LocationURL;

if not IsUrlValid(url) then url:='';

Result:=url;
end;

end.
 
后退
顶部