views:

331

answers:

2

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.

A: 

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.

Remy Lebeau - TeamB
A: 

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">
Remy Lebeau - TeamB