我用ActiveForm做了一个B/S程序,后台用SQL Server 2000数据库,现在我是把服务器地址直接写到代码中了,如:ADOQuery.ConnectionString='SQLOLEDB.1;Persist Security Info=False;User ID=sa;Password=123;Initial Catalog=test;Data Source=192.168.1.98,但这样做不灵活,不同的用户的服务器地址肯定不一样,我怎么才能获得ActiveForm所在网页中的IP地址呢? 最好有例子,谢谢!
当然这种的局限性还是比较大的,比如使用端口映射,之后,就几近无法使用。
稍微改一下:
Result:=String(i_hd.location.href);
变成
Result:=String(i_hd.location.hostname);
------------------------------function GetCurrentPageUrl(x : TActiveForm): String;
var
i_hd : IHTMLDocument2;
i_oc : IOleContainer;
begin
Result:='';
try
if (x.ActiveFormControl.ClientSite.GetContainer(i_oc) = S_OK) and
(i_oc.QueryInterface(IHTMLDocument2, i_hd) = S_OK) then
begin
Result:=String(i_hd.location.href);
end
else
Exit;
except
Exit;
end;
end;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; function EnumChildProc(hwnd:HWND;IParam:LPARAM):bool;stdcall;
var
Form1: TForm1; strUrl:string;
implementation{$R *.dfm}function EnumChildProc(hwnd:HWND;IParam:LPARAM):bool;stdcall;
var buf:array[0..250] of char;
rsize:integer;
str:string;
begin
result:=true;
Getclassname(hwnd,buf,sizeof(buf));
if strpas(buf)='Edit' then
begin
rsize:=sendmessage(hwnd,WM_GETTEXT,sizeof(buf),integer(@buf));
if rsize>0 then
begin
strUrl:=buf; //这就是当前打开页面的URL
end;
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
fwnd,hwnd:thandle;
buf2,buf:array[0..250] of char;
begin
fwnd:=GetForegroundWindow;
hwnd:=fwnd;
Getclassname(fwnd,buf,sizeof(buf));
EnumChildWindows(fwnd,@enumchildproc,0);
Edit1.text := strUrl; end;end.