opening default browser

Rothrandir
Rothrandir used Ask the Experts™
on
i am writing an html editor, and would like to have it so when i click on a button (preferably speedbutton i suppose) the users default browser opens up and displays the contents of that page.

kind of like the preview funtion in frontpage.  when the user clicks the button "preview"  i would like the default brower to open, and preferably refresh every 2 seconds if possible.

keep in mind that i want the file to open, but not save if possible, the i am using filename (string) as the name of the document whether it is "untitled.html" or "whateverelse.html"


also, i tried running the program at school, but the scrollbar doesn't work and the speedbuttons won't
display, any thoughts on why this might happen?

please excuse the small number of points, i don't have a credit card to order more, or enough knowlege to become an expert anyway...
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
First you should save your file with a valid name.
I think you know how to execute an .EXE file in delphi (ShellExecute/WinExec/ExecuteFile/ etc).
The solution is executing your .HTML file as an .EXE file.
It means supplying name of .HTML file as a parameter to one of those functions which executes .EXE files.
Also you can examine it by typing name of your .EXE file into run window (Start Menu -> Run) and see what happens.
That's it.
VGR

Commented:
no problem at all.
I will expand on what wrote DelphiAssistant (hello ;-)

1)  "executing" an URI/URL is perfectly possible but uses the implicit Zindoze file associations to launch the default browser. You ***perhaps*** would like to control more which browser opens for your temporary file, because of the discrepancies between "W3C-Compliant" IE and HTML-Compliant Netscape Navigator (like display table if /table missing, or /td missing, or display FORM if /select missing, etc etc etc).

You perform this by
poubInt:=ShellExecute(Application.Handle, 'open',PChar('http://www.edainworks.com'),'','',SW_SHOWDEFAULT );
See ShellAPI unit or ShellExecute doc'

2) opening a refreshing "preview" browser is easy :
- if you have a local Apache server with the PHP module, no problemo at all and I will post some code for you, I do this all the time. This can be used on Win32 and on linux.
- if not, then no panic. Javascript can do the trick

Many ideas I have, depending on what you want :
-opening a fixed URL containing a frame that refreshes itself every 2 seconds (that's pretty fast even on localhost, be warned) and the URL/file passed in parameter from inside the Delphi application
-putting the correspondent refresh stuff in the HEADer section of the HTTP response you send to the preview browser. Easy too.
-using PHP is child's play.

3) note also that you could use one of the new components of Delphi >5 that uses an IE OCX or whatever ; that is TWebBrowser I think (never used it)


good luck and tell me the solution you would like me to help you more precisely in ;-)

Author

Commented:
wow, this is way beyond me, maybe i will have to abandon the idea...i hoped i would just have to do some thing like (nowhere accurate i know):

prodedure speedbutton45click(sender: tobject);

begin
  filename execute
end;

that is nowhere even close, but i am trying to figure out a simple way to do it, and preferably without saving the file as i would like the user to be able to preview the document and decide where or not they want to save.  i think i might go witht the twebbrowser thing if i can figure out how to do it with that.  also, this program will be on different computers, so the brower the people use might not be in the same location as mine.

i have practically no experience with delphi, i am actually basing  this entire program on the text editor tutorial supplied with the delphi help.  i used to do a *very* little pascal, but i was about 10 or 11 then and i forgot all of it but vague basics.  thank you.
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

VGR

Commented:
Well, then listen my words and hear my voice ;-))
(joking)

It's rather easy to perform the ShellExecute command : only one line.
I suggest your delphi program temporarily saves the working file under a fake name (foo.html ? ;-) and then open it for viewing using the shell execute command.

For the auto-refresh (althought I don't recommand it), you can either :
-put an HTML FORM & button or link for the user to manually reload the (temporary) file ; it's basically the same as letting him/her do a "refresh" in the browser
- or insert the usual HTTP_META lines in the HEAD section of the generated HTML file for refresh rate and such

IF I were you, I wouldn't throw the baby with the bath's water

Author

Commented:
thank you vgr, that sounds like it should work nicely...but i have no idea how to do it...

Commented:
The simple answer to get you started:

add ShellAPI to your USES clause:
uses
  ShellAPI;

Save your html to a temporary file: 'temp.htm'

Then:

ShellExecute(Form1.Handle, 'Open', 'temp.htm', nil, nil, SW_SHOW);

Be sure to check the docs on "ShellExecute" for
info on error returns, etc.

Author

Commented:
please excuse me, i am very new at this.

how should i save as a temporary file?  i tried copying the filesave and filesaveas stuff with 2's at the end, but this did not work.  i also added temporary to the vars as a string, like i did filename.

thank you so much for your help so far.

Commented:
Example of saving text in a TMemo (or TRichEdit):
MyMemo.Lines.SaveToFile(ExtractFilePath(ParamStr(0)) + 'temp.htm');
This will save the file in the same folder as your App.

Author

Commented:
i did this, but with richedit1 instead of mymemo.  and it didn't work...it compiled just fine, but it didn't save, i even verified this by doing a search of my hard drive.

i added a speebutton to the form, and then double clicked it, and where the cursor was placed, i typed RichEdit1.Lines.Add.SaveToFile(ExtractFilePath(ParamStr(0)) +'temp.html');

Commented:
//Rothrandir:
//Try this:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin

  Memo1.Lines.SaveToFile(ExtractFilePath(ParamStr(0)) + 'temp.htm');

end;

end.

Author

Commented:
that is basically what happened when i did it.  the only difference was the richedit1 instead of memo1, and my program had a bunch of other buttons also.

i don't know why it isn't working, but we must be close as we aren't getting an error message, and it compiles just fine.
Commented:
add the line:
ShowMessage(ExtractFilePath(ParamStr(0)) + 'temp.htm');
just before:
Memo1.Lines.SaveToFile(ExtractFilePath(ParamStr(0)) + 'temp.htm');

Should see a legal filename.

Author

Commented:
thank you klooge! it worked, after i added those, i added the shell thing you described above and it worked perfectly!

...now if only i could have a "would you like to save changes you made?" come up like it does on microsoft...i would wnat this when you click save and not onthe preview however.

Author

Commented:
uh-oh...we have a problem...

this only appears to work when the saved file is in the same directory as my application.

for instance...i was making a webpage, with everything (program, index, and subpages) on my desktop...when i moved them to a different place, they no longer ran when i clicked the preview button, they saved, but would not shell...why is this happening?

Commented:
You need to pass the full pathname to the ShellExecute function if the file in question is not in your app folder.
Probably best way to handle this is to use the 'FileOpen'
dialog and the 'FileSave' dialog in your application.
Be sure to check the return value of ShellExecute so you can handle any errors appropriately.

Author

Commented:
wow.  okay...how should i go about doing that?

Commented:
You can find info on using TSaveDialog and
TOpenDialog in the help docs. Also there's plenty of samples in the Demos folder.

for example:

var
  sFileName: string;
begin
  if OpenDialog1.Execute then { Display Open dialog box }
  begin
    sFileName := OpenDialog1.FileName;
  end;
end;

sFileName contains the full path to the file the user chose in the "Open" dialog.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial