|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV SYS-CON.TV WEBCASTS |
POWERBUILDER LINKS YOU MUST CLICK ON PowerBuilder Writing Your Own Web Browser
Writing Your Own Web Browser
By: Kaushik Datta
Mar. 1, 2000 12:00 AM
Imagine creating your own Web browser that can provide the full power of Internet Explorer (IE) including scripting, parsing and rendering of HTML pages. It has become standard for stand-alone and client/server applications to contain Web browsers. A perfect example is Intuit's popular Quicken program. Quicken has the normal look and feel of a stand-alone application, but the startup sheet is a Web browser that presents up-to-the-minute Intuit information to the user. You too can build this functionality into your PowerBuilder application. In this article I'll describe how to use Microsoft's WebBrowser ActiveX control to build your own Web browser application in PowerBuilder.
About Microsoft's WebBrowser ActiveX Control
Note: To distribute your application with this control to users who don't have IE installed, you need to obtain a license to distribute IE from Microsoft.
Getting Started
Creating the Browser Visual User Object
All the events, properties and methods of this control are well documented at the Web site, http://msdn.microsoft.com/workshop/browser. In the constructor event add the following line of code that will open up the home page by default when the browser is visible. ole_1.object.GoHome()The GoHome method of the browser control takes you to the home page, which depends on the registry key value of HKEY_CURRENT_USER/Software/ Microsoft/Internet Explorer/Main/Start Page. I had set it to "about:blank", so it would initially show me a blank page on the browser. If you want the home page to be the PBDJ Web site, set the registry key value to www.sys-con.com/pbdj. To see this effect, save the user object (u_browser) and close its painter. Create a window object (w_browser), place the user object in it and save. Open the browser window by adding appropriate code in the application's open event and run the application. Voilà! The browser is now ready for surfing the PBDJ Web site.
Navigating with the Browser Control
ole_1.Object.Navigate2(Text)The Navigate2 method of the browser control actually does the trick. The other lines above add the sites to the drop-down list as in IE. Add the following line of code in the clicked event of Back button. ole_1.object.GoBack()Add the following lines of code in the clicked event of Forward button. ole_1.object.GoForward() The GoBack and GoForward methods are used to browse back and forth through the sites maintained in the browser's history list during each browser session. The status of the browser control can be obtained using the Busy property, which indicates whether the control is engaged in a navigation or downloading operation. Using this property, add the following lines of code to the clicked event of the Stop button. IF ole_1.Object.Busy THENYou can set the Cancel property to true for the Stop button so that the clicked event gets fired when you press the escape (Esc) key, as in IE. Just as the Stop method of the browser control stops a Web page from loading, we have a Refresh method to refresh the contents of the current page. Add the following lines of code to the clicked event of the Refresh button. IF NOT ole_1.object.busy THENCall the constructor event of the browser user object from the clicked event of the Home button to navigate directly to the home page, as shown below. Parent.Event Trigger Constructor()To utilize the search engines, use the GoSearch method and add the following line of code in the clicked event of the Search button. ole_1.object.GoSearch()The GoSearch method, which is similar to the GoHome method, depends on the registry key value of HKEY_CURRENT_USER/Software/Microsoft/Internet Explorer/Main/Search Page. I had set it to www.yahoo.com, so it would show me the home page of Yahoo! MSN is the search page by default when you install this browser control.
Useful Events of the Browser Control
IF Pos(Lower(ddlb_1.text), "u:") > 0 OR & This event is one of the most powerful events for canceling the navigation request to the URL for unauthorized Web sites or local and network folders. The NavigateComplete2 event is fired after the browser has navigated to a new location. Add the following line of code in the NavigateComplete2 event so that the drop-down listbox reflects the correct URL currently being navigated. ddlb_1.text = ole_1.object.LocationURLCaution, BeforeNavigate2 and NavigateComplete2 events don't work in PB 6.x but they work in PB 7.0. Add the following line of code in the StatusTextChange event of the browser control to show the exact activity of the browser control. st_2.text = textThis is the event used by IE to show the current downloading status. Add the following lines of code in the TitleChange event of the browser control to set the title of the window in which this user object is placed. window lw_browserThe text, if present within the <TITLE> tag of the HTML page, is actually displayed on the titlebar of the window. There's a trick to disabling and enabling the Back and Forward command buttons. The CommandStateChange event of the browser control is fired when the Forward or Back buttons need to be enabled or disabled, so add the following lines of code in this event. CHOOSE CASE commandThe two arguments - command and enable - of the above event are used to enable or disable the appropriate Back and Forward command buttons in the browser user object. The browser control supports several common file operations, such as Print, Page Setup, Save and Properties, with the ExecWB method. I'll show you how to add the save HTML document functionality using the ExecWB method. Add a command button called Save As (see Figure 2) and add the following lines of code in the clicked event. ole_1.object.ExecWB( 4, 1) Value 4 for the first argument and value 1 for the second argument bring up the Save HTML document dialog box. Similarly, value 6 would bring up the Print dialog box and value 8 would bring up the Page Setup dialog box. To print the Web page, you can use the Print menu item from the popup menu that appears when the user right-clicks on the browser control at runtime. It's good practice to trap the ExternalException event for this control as it returns an OLE exception whenever anything fails, such as trying to navigate backward or forward further than the existing pages or selecting the cancel button in a Save As dialog box. For this purpose, add the following lines of code in this event for a simple error handler to prevent the application from halting because of an Application System Error. MessageBox("OLE External Exception", source) action = ExceptionIgnore!Running the Customized Browser Application Save the user object with all the above lines of code and close the user object painter. Run the application. The customized browser application is now ready with most of the features of IE.
Conclusion
You can download this article and the source code developed using PowerBuilder version 7.0 from the URL www.intac.com/~kaudata/download PBDJ LATEST STORIES . . .
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK BREAKING POWERBUILDER / SYBASE NEWS
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||