function TActFrm.GetOwnerUrl:string; var site: IOleClientSite; sp: IServiceProvider; hr: HResult; temp_URL : string; shttp : string; i : integer; begin // This code obtains the automation interface of the web browser // that the ActiveForm is embedded into, and uses it to // get the current web address, LocationURL. shttp := 'http://'; if (ComObject as IOleObject).GetClientSite(site) = S_OK then if site <> nil then begin hr := site.QueryInterface( IServiceProvider, sp ); hr := sp.QueryService( IWebBrowserApp, IWebBrowserApp, browser ); temp_URL := browser.LocationURL; if LowerCase (Copy (temp_URL, 1, Length (shttp))) = LowerCase (shttp) then Delete (temp_URL, 1, Length (shttp)); i := pos('/',temp_URL); if i =0 then Result := temp_URL else Result := copy(temp_URL, 1, i-1); end; end;注意:这个函数好象必须在CREATE完成之后运行
我有更简单的方法:use mshtml....function ......var r , ht, pn , u : String; i_hd : IHTMLDocument2; i_oc : IOleContainer; i_ow : IOleWindow; begin if (ActiveFormControl.ClientSite.GetContainer(i_oc) = S_OK) and (i_oc.QueryInterface(IOleWindow, i_ow) = S_OK) and (i_ow.QueryInterface(IHTMLDocument2, i_hd) = S_OK) then begin ht := String(i_hd.location.protocol+'//'+i_hd.location.host); pn := String(i_hd.location.pathname); end ...//可以参照一下javascript,i_hd 就是 javascript的window.document
但我自己写的ocx(内部自己建http连接)则必须要传"http://xxxxxxxx/aaa/bbb.asp"郁闷!
那样不是很完美呀!
555555555555555
var
site: IOleClientSite;
sp: IServiceProvider;
hr: HResult;
temp_URL : string;
shttp : string;
i : integer;
begin // This code obtains the automation interface of the web browser
// that the ActiveForm is embedded into, and uses it to
// get the current web address, LocationURL.
shttp := 'http://';
if (ComObject as IOleObject).GetClientSite(site) = S_OK then
if site <> nil then
begin
hr := site.QueryInterface( IServiceProvider, sp );
hr := sp.QueryService( IWebBrowserApp, IWebBrowserApp, browser );
temp_URL := browser.LocationURL;
if LowerCase (Copy (temp_URL, 1, Length (shttp))) = LowerCase (shttp) then
Delete (temp_URL, 1, Length (shttp));
i := pos('/',temp_URL);
if i =0 then
Result := temp_URL
else
Result := copy(temp_URL, 1, i-1);
end;
end;注意:这个函数好象必须在CREATE完成之后运行
r , ht, pn , u : String;
i_hd : IHTMLDocument2;
i_oc : IOleContainer;
i_ow : IOleWindow;
begin
if (ActiveFormControl.ClientSite.GetContainer(i_oc) = S_OK) and
(i_oc.QueryInterface(IOleWindow, i_ow) = S_OK) and
(i_ow.QueryInterface(IHTMLDocument2, i_hd) = S_OK) then
begin
ht := String(i_hd.location.protocol+'//'+i_hd.location.host);
pn := String(i_hd.location.pathname);
end
...//可以参照一下javascript,i_hd 就是 javascript的window.document
在你写ActiveForm时,在工程的Type Library里相应的Interface对象下面添加一个(写入)属性。(这是在开发OCX时做的事,下面是在写脚本或是ASP时用的)在脚本里写入
document.all.<组件在网页里的实列名>.<刚刚设的属性> = Request或是别的方法取得URL这样子就OK了,在ActiveForm里声明一个私有变量,然后接受写入的属性,就可以在ActivForm里随意使用了,呵呵其实这种方法跟myy()的方法有异什么什么同工之妙,呵呵。都是用了接口来干事的。
myy()用的是IE本身的接口来取得URL,而这边用的是自己声明接口来传入URL。
document.all.myobj.setname( "wang")
其中,myobj是
<object
ID = "myobj"
>