Tha*_*Guy 3 html browser delphi
我想在默认浏览器中打开本地HTML文件。
例如:默认浏览器是Mozilla Firefox。
要打开的文件:C:\ My Custom Path \ New Folder \ AFile.htm
请注意,路径中有空格。根据条件,我想在URL末尾附加一个ID。
最终URL为C:\ My Custom Path \ New Folder \ AFile.htm#12345
如果我手动打开浏览器,并粘贴URL“ C:\ My Custom Path \ New Folder \ AFile.htm#12345”。
工作正常。无法找到通过代码执行此操作的最佳方法。
ShellExecute/Ex()不能直接在网址中使用"open"带锚(#)的动词。即使您使用file://协议,锚也会被忽略。
最好的方法是获取默认浏览器的路径,您可以使用FindExecutable,然后执行它,并将URL作为参数传递。
uses
ShellAPI;
procedure TForm1.Button1Click(Sender: TObject);
var
Res: HINST;
Buffer: array[0..MAX_PATH] of Char;
SEInfo: TShellExecuteInfo;
HtmlFile, Anchor: string;
begin
HtmlFile := 'd:\1 - Copy.html';
Anchor := '#123';
FillChar(Buffer, SizeOf(Buffer), 0);
Res := FindExecutable(PChar(HtmlFile), nil, Buffer);
if Res <= 32 then
raise Exception.Create(SysErrorMessage(Res));
FillChar(SEInfo, SizeOf(SEInfo), 0);
SEInfo.cbSize := SizeOf(SEInfo);
with SEInfo do
begin
lpFile := PChar(string(Buffer));
lpParameters := PChar(Format('"file:///%s"', [HtmlFile + Anchor]));
nShow := SW_SHOWNORMAL;
fMask := SEE_MASK_FLAG_NO_UI; // Do not display an error message box if an error occurs.
end;
if not ShellExecuteEx(@SEInfo) then
RaiseLastOSError;
end;
Run Code Online (Sandbox Code Playgroud)
编辑:file:///在URL包含查询字符串参数(例如file.html?param=foo#bar)或将URL ?转义到%3F(在Chrome中测试)的情况下,URI方案很重要
| 归档时间: |
|
| 查看次数: |
1329 次 |
| 最近记录: |