如何将 Delphi WebBrowser 基本目录设置为与 HTML 位置不同

发布于 2024-08-30 10:58:02 字数 602 浏览 13 评论 0原文

我有一个创建 HTML 文件的 Delphi 程序。稍后,当按下按钮时,会创建 TWebBrowser,并且 WebBrowser.Navigate 会显示 html 页面。

是否有办法设置 WebBrowser“默认目录”,以便它始终是 Delphi 可执行文件的位置而不是 HTML 文件的位置?

我不想将 HTML 中的 Base 值设置为硬编码值,因为当从另一个 Delphi exe 运行 HTML 时,找不到它们。

例如:

如果 exe 从 D:\data\delphi\pgm.exe 运行,则基本位置 D:\data\delphi\ 和 jpg 位于 D:\data\delphi\jpgs\

但如果运行 exe来自: C:\stuff\pgm.exe 我希望基本位置为 C:\stuff\ ,jpgs 位于 C:\stuff\jpgs\

所以我不能在 HTML 中写一行带有基本位置的行它是从另一个 exe 运行的,它会指向该 exe 的错误位置。

因此,我要么需要在创建网络浏览器时以及读取 HTML 之前设置基本位置,要么需要一种方法将位置传递到网络浏览器中,然后我可以在其中设置基本位置。

很抱歉这么啰嗦,但我不知道如何看到我需要的东西。

I have a Delphi program that creates HTML files. Later when a button is pressed a TWebBrowser is created and a WebBrowser.Navigate causes the html page to display.

Is there anyway to set the WebBrowser "default directory" so it will always be the location of the Delphi executable not the HTML file?

I do not want to set the Base value in the HTML to a hardcoded value because then when the HTML is ran from another Delphi exe they are not found.

for example:

if the exe is run from D:\data\delphi\pgm.exe then the base location D:\data\delphi\ and the jpgs are at D:\data\delphi\jpgs\

but if the exe is run from: C:\stuff\pgm.exe i want the base location to be C:\stuff\ and the jpgs to be at C:\stuff\jpgs\

So I cannot write a line in the HTML with the base location since when it is ran from another exe it would point to wrong location for that exe.

So I either need to set the base location when I create the webbrowser and before I read the HTML or I need a way to pass into the webbrowser the location where I can then set the base location.

Sorry for being so long-winded but I could not figure out how to saw what I needed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初熏 2024-09-06 10:58:02

如果 HTML 文件与 JPG 存储在同一文件夹中,则无需设置基本路径。

HTML 文件的基本路径是加载该文件的路径,或者是通过 标记在 HTML 本身中指定的路径。您可以设置一个新的基本路径,该路径可通过 IHTMLDocument2.all.tags('base') 集合访问,但只有在先加载并解析 HTML 文件后才能访问它,这对你来说是第22条军规。如果没有事先加载文档,则无法设置基本路径。

If the HTML file is stored in the same folder as the JPGs, then there is no need to set a base path.

An HTML file's base path is the path from which it is loaded, or a path specified in the HTML itself via a <base href="..."> tag. You can set a new base path, which is accessible via the IHTMLDocument2.all.tags('base') collection, but it is not accessible until after the HTML file has been loaded and parsed first, which is a catch-22 for you. You cannot set a base path without having a document loaded beforehand.

忆沫 2024-09-06 10:58:02

由于 TWebBrowser 只是 Internet Explorer 引擎的包装器,因此如果您允许在浏览器中启用客户端脚本,则可以尝试另一种选择。

  1. 在您的中创建一个自动化对象
    实现 IDispatch 的应用程序
    接口,并给它一个字符串
    返回应用程序的属性
    当前运行路径。
  2. 创建一个自动化对象
    实现 IDocHostUIHandler
    接口,并重写其 GetExternal()
    方法返回一个指向你的
    来自#1 的对象。
  3. 检索浏览器的 ICustomDoc
    接口,并传递您的 IDocHostUIHandler
    对象到其 SetUIHandler() 方法。

有关详细信息,请参阅 MSDN:

http:// msdn.microsoft.com/en-us/library/aa770041.aspx

这样,您的 HTML 就可以包含可使用 window.external 对象检索应用程序路径并更新其路径的脚本。 JPG动态引用,比如在OnLoad事件中,即:

<script language="JScript">
function UpdateJPGs()
{
  var path = window.external.ExePath;
  document.images.item("jpg1").src = path + "1.jpg";
  document.images.item("jpg2").src = path + "2.jpg";
  // etc...
}
</script>

<body onLoad="UpdateJPGs">

Since TWebBrowser is just a wrapper around the Internet Explorer engine, there is an alternative you can try if you allow client-side scripting to be enabled in your browser.

  1. Create an Automation object in your
    app that implements the IDispatch
    interface, and give it a string
    property that returns the app's
    current running path.
  2. Create an Automation object that
    implements the IDocHostUIHandler
    interface, and override its GetExternal()
    method to return a pointer to your
    object from #1.
  3. Retreive the browser's ICustomDoc
    interface, and pass your IDocHostUIHandler
    object to its SetUIHandler() method.

Refer to MSDN for more details:

http://msdn.microsoft.com/en-us/library/aa770041.aspx

This way, your HTML can contain scripting that can use the window.external object to retreive the app's path and update its JPG references dynamically, such as in the OnLoad event, ie:

<script language="JScript">
function UpdateJPGs()
{
  var path = window.external.ExePath;
  document.images.item("jpg1").src = path + "1.jpg";
  document.images.item("jpg2").src = path + "2.jpg";
  // etc...
}
</script>

<body onLoad="UpdateJPGs">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文