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/') ;