NAVIGATION:
David's Corner
> DIY Delphi > Basic Browser
> Getting Started
STARTING
THE BASIC BROWSER
NOTE:
This section assumes that you know your way around the Delphi
IDE - installing and placing components, writing event handlers,
and so on. It isn't a Delphi tuorial for beginners, but
even so you don't need much knowledge to get by here.
INSTALLING THE MSIE WEB BROWSER
Before anything else, you'll need to add the IE Web Browser
control to your Delphi installation
For D3, in the Delphi IDE menu, click on "Component
| Import ActiveX Control..."
Scroll down the list of ActiveX components available and
select "Microsoft Internet Controls (version 1.1)"
Click on "Install", and follow the dialogues through,
allowing the Delphi packages to be rebuilt and saved. (the
D2 procedure will be slightly different at this point).
You now have two new controls installed on your "ActiveX"
palette. For this project, you'll be using the WebBrowser
control. The WebBrowser_V1 is for backwards compatibilty
with IE 3.02 or higher, and can be used if you need that
compatibility, but some of the code which follows in this
project won't work. If you go down that route, there are
places where you'll be on your own.
THE BROWSER PROJECT
Open a new project in Delphi. You may as well save it immediately
- mine is called "BasicBrw.dpr". I renamed "Form1"
to "MainWindow".
Drop a panel onto your main form, remove the caption, make
the height 40, and align it to alTop.
Place 5 speedbuttons on the panel. Make them each 32x32
in size, and place them 2 pixels below the top of their
parent panel. Align the first two close together, leave
a small gap and then align the next three close together.
From left to right, give the five buttons the following
captions:
"<<" , ">>" , "Refr.",
"Stop", "Home".
(I did say it was a basic browser...).
Place an Edit box to the right of the speedbuttons. Rename
it "AddressBar". Add one final speedbutton (20x20
pixels in size) to the right of the edit box. Vertically
align this last button to the edit box's centre and give
it the caption "GO". Delete the text in the edit
box's "Text" field.
Place a WebBrowser on the main form and align it to alClient.
I renamed it from "WebBrowser1" to "Browser"
(this just saves some typing later on). Set the AddressBar,
FullScreen, MenuBar and StatusBar properties to "False".
I'd suggest a save at this point. Below is an image of what
we have so far:
 |
Again working from left to right, double-click on each of
the first five speedbuttons in turn to bring up its OnClick
event-handling template. Between the "Begin" and
"End;" lines of the template type in the following
code for each button:
BUTTON |
CODE |
<< |
Browser.GoBack; |
>> |
Browser.GoForward; |
Refr. |
Browser.Refresh; |
Stop |
Browser.Stop; |
Home |
Browser.GoHome; |
The code for the last speedbutton (the "GO" button)
is a little more difficult, but not by much. Double-click
on this button to bring up its event handler template. This
time, though, delete the supplied "Begin - End;"
lines, and replace them with the following code:
var a, b, c, d : OLEVariant; //'Variant' in D2
TheURL : string;
begin
if AddressBar.Text <> '' then begin
TheURL := Edit1.Text;
Browser.Navigate(TheURL, a, b, c, d);
end;
end;
Then, open the main form's OnShow event handler, and type
in the following:
Browser.GoHome;
Save and compile the project. For reasons which I'll explain
in a minute, I then recommend that you close Delphi and
run the browser as a stand-alone program outside of the
IDE. When the program starts it will, depending on your
Internet and IE settings, either directly, or via the "Dial-Up
Connection" dialogue, connect to the Internet and behave
like a standard web-browser.
So far, then, you have your own very basic but working Internet
browser. It will have only taken you (at a guess) about
twenty minutes to write and has only needed around a dozen
lines of your own code.
It works; but we need to refine the setup before we do any
real customisation. The program has four problems in its
basic form:
1) New browser windows are opened in Internet Explorer rather
than in our own program.
2) Clicking on the "Back" and "Forward"
buttons produces errors if there aren't pages to go back
or forward to. This is why I recommended running the browser
outside Delphi. If you do, this situation only produces
an error message; if you run it from within Delphi, the
IDE halts and you need to re-run the program.
3) The address bar is awkwardly placed, doesn't respond
to changes in the program window's size, doesn't respond
to URL changes, and can't be activated with the "Return"
key.
4) The program gives no information about the browser's
progress, the page it's loading, or when it's completed
a task.
The next page addresses these four problems.
<< BACK || NEXT >>
|