From: http://nio.infor96.com/archives/78
在使用 Delphi 编写程序时,经常需要直接使用浏览器打开一个 URL,简单的方法是用 ShellExecute,例如:
ShellExecute(Application.Handle, 'open', PChar(url), nil, nil, SW_SHOW);
这种方法的缺点是:不会重新创建浏览器实例(就是说不会打开新的浏览器窗口),从而导致先前正在浏览的网页被当前 URL 网页替换掉。虽然可以通过浏览器的“后退”按钮回去,但还是觉得不爽!
以下的代码解决了这个问题。
uses
Registry, ShellAPI;
function BrowseURL(const URL: string) : boolean;
var
Browser: string;
begin
Result := True;
Browser := '';
with TRegistry.Create do
try
RootKey := HKEY_CLASSES_ROOT;
Access := KEY_QUERY_VALUE;
if OpenKey('htmlfileshellopencommand', False) then
Browser := ReadString('') ;
CloseKey;
finally
Free;
end;
if Browser = '' then
begin
Result := False;
Exit;
end;
Browser := Copy(Browser, Pos('"', Browser) + 1, Length(Browser)) ;
Browser := Copy(Browser, 1, Pos('"', Browser) - 1) ;
ShellExecute(0, 'open', PChar(Browser), PChar(URL), nil, SW_SHOW) ;
end;
//使用示例
BrowseURL('http://www.k12.com.cn/') ;
本文介绍了一种在Delphi应用程序中打开URL并确保在新浏览器窗口中显示该URL的方法。通过读取注册表来获取默认浏览器路径,并利用ShellExecute函数启动浏览器。
4582

被折叠的 条评论
为什么被折叠?



