Building a Windows VCL Web Browser Application
Go Up to How To Build Windows VCL Applications
Creating the Web browser application consists of the following steps:
- Create a VCL Form.
- Add a TWebBrowser component to the form.
- Add controls to enter a URL, launch and navigate using the browser.
- Write the code for launching and navigating.
- Run the application.
Contents
Design the User Interface
- Open File > New > Windows VCL Application - Delphi
- From the Win32 page of the Tool Palette, place a TToolBar component on the form.
- From the Standard page of the Tool Palette, place three TButton components and a TEdit on the ToolBar component.
- Set the Caption property of each TButton as follows: Back, Forward and Home.
- Select the Edit box on the Form Designer, go to Object Inspector and select alClient for the Align property.
- From the Internet page of the Tool Palette, place a TWebBrowser component on the form.
- Select the TWebBrowser component on the Form Designer, go to Object Inspector and select alClient for the Align property.
- Note: If the window is not large enough to display a browser page in its entirety, the TWebBrowser component adds scrollbars when you run the application and launch the browser window.
- After completing these steps, the form should be similar to the following picture:
Add the Code that Uses the WebBrowser
- From the Object Inspector create event handlers for the OnClick event of each TButton component, and the OnChange event of the TEdit component.
- Add the following code to the OnClick event handler of the first Button :
In Delphi:
procedure TForm1.Button1Click(Sender: TObject);
begin
WebBrowser1.GoBack;
end;
In C++:
void __fastcall TForm1::Button1Click(TObject *Sender) {
WebBrowser1->GoBack();
}
In Delphi:
procedure TForm1.Button2Click(Sender: TObject);
begin
WebBrowser1.GoForward;
end;
In C++:
void __fastcall TForm1::Button2Click(TObject *Sender) {
WebBrowser1->GoForward();
}
In Delphi:
procedure TForm1.Button3Click(Sender: TObject);
begin
WebBrowser1.GoHome;
end;
In C++:
void __fastcall TForm1::Button3Click(TObject *Sender) {
WebBrowser1->GoHome();
In Delphi:
procedure TForm1.Edit1Change(Sender: TObject);
begin
WebBrowser1.Navigate(Edit1.Text);
end;
In C++:
void __fastcall TForm1::Edit1Change(TObject *Sender) {
WebBrowser1->Navigate2(Edit1->Text);
}
Run the application
- Choose Run > Run to build and run the application.
- Enter a URL to a Web page in the edit box. The browser launches in the TWebBrowser window.
- The result at run time: