您的位置:首页 > 编程语言 > Delphi

Delphi打开长于260个字符的网页

2015-09-15 20:21 351 查看
当用

function OpenURL(const URL: string): Boolean;

var

LUrl: string;

begin

Result := False;



LUrl := Trim(URL);

if Length(LUrl) > 0 then

begin

LUrl := '"' + LUrl + '"';

Result := ShellExecute(Application.Handle, nil, 'explorer.exe', PChar(LUrl), nil, SW_SHOWNORMAL) > 32;

end;

end;

时,Url的参数大于260个字符时,就会被截断,解决办法是,获取默认浏览器,来打开网页。'explorer参数长度有限制。





implementation

uses

Registry, ShellAPI;

{$R *.dfm}

function GetDefBrower: string;

var

LReg:TRegistry;

begin

// HKEY_CLASSES_ROOT\Http\shell\open\command

Result := '';

LReg := TRegistry.Create;

try

LReg.RootKey := HKEY_CLASSES_ROOT;

if LReg.OpenKey('Http\shell\open\command', False)then

if LReg.ValueExists('')then

Result:=LReg.ReadString('');

LReg.Closekey;

finally

LReg.Free;

end;

end;

function OpenURL(const URL: string): Boolean;

var

LUrl: string;

LDefBrower: string;

LStrList: TStringList;

begin

Result := False;

LStrList := TStringList.Create;

try

LStrList.Delimiter := '"';

LStrList.StrictDelimiter := True;

LStrList.DelimitedText := GetDefBrower;

if LStrList.Count > 0 then

LDefBrower := LStrList.Strings[0];

finally

LStrList.Free;

end;

if not FileExists(LDefBrower) then

LDefBrower := 'iexplore.exe';



LUrl := Trim(URL);

if Length(LUrl) > 0 then

begin

LUrl := '"' + LUrl + '"';

Result := ShellExecute(Application.Handle, nil, PChar(LDefBrower), PChar(LUrl), nil, SW_SHOWNORMAL) > 32;

end;

end;

procedure TForm1.Button1Click(Sender: TObject);

begin

OpenURL('baidu.com');

end;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: