Contents - Index - Top


PowerBuilder Web Browser example

 

This example shows you how-to use the control as a basic browser.

 

First look at the open event at window level.

 

event open

// example on how you can unlock the control once you acquired a license.

edgewebbrowser.Object.UnlockControl("ExampleCompany", "WI5PO2-2KSU3Q-HWFXFU-IUMU2V-QF8P2F")

end event

 

You always need to unlock the control with UnlockControl. You can do this globally if you have multiple AntView controls.

 

Now at the oncreate event at AntView level.

 

event oncreate()

Object.CreateWebViewOnCreate = TRUE // so we don't have to call CreateWebView()

edgewebbrowser.Object.EventsUseHexadecimal = TRUE // trigger the events that don't use Int64 data types

 

end event

 

We're setting the CreateWebViewOnCreate property to true to automatically create the WebView2 control when the AntView control is instantiated.

As PowerBuilder doesn't support the Int64 datatype, we also set the property EventsUseHexadecimal to true so that we can use those events that have Int64 style datatypes.

 

That's the part on setting up the control. Now we can use it.

 

event oncreatewebviewcompleted(long hresult)

//

// Using a virtual hostname allows you to access resources that are normally

// restricted to CORS checks.

// we're configuring "site" https://rijksmuseum.local to point to a local folder in our demo folder

string ls_path

string ls_hostname

constant long hrakAllow = 1 // All cross origin resource access is allowed, including accesses that are subject to (CORS) checks

 

ls_path = strGetPowerBuilderDemoPath() + "\html"

ls_hostname = "rijksmuseum.local"

edgewebbrowser.object.SetVirtualHostNameToFolderMapping(ls_hostname, ls_path, hrakAllow)

end event

 

Once the WebView2 control is initialised, it will trigger the OnCreateWebViewCompleted event.

We are using that event here to setup a so called "virtual host name mapping". This is a technique where you map a local file path to a hostname. In our example we are mapping the folder "C:\Users\Public\Documents\AntView\Examples\PowerBuilder\html" to hostname "rijksmuseum.local" so that we can access files in that folder using URL "https://rijksmuseum.local".

By setting this with SetVirtualHostNameToFolderMapping you can use features that normally won't be available with file based URI's.

 

The OnNavigationCompletedHex event is used to update the address combobox with the actual URL.

 

event onnavigationcompletedhex(boolean issuccess, integer weberrorstatus, string navigationidhex)

if issuccess then

 // update the URL in the address box with what we are looking at.

 addressbox.text = edgewebbrowser.object.source

end if   

end event

 

The Go button takes whatever is in the address combo and navigates to that page via the navigate method.

 

string ls_address

int ll_pos

 

ls_address = addressbox.text

ll_pos = Pos(ls_address, "://")

if ll_pos = 0 then

  ls_address = "https://" + ls_address

end if    

edgewebbrowser.object.navigate(ls_address)

 

The only thing extra there is that it inserts the "https://" protocol identification if not typed in by the user.

 

The print button click event has this code:

 

event clicked

// print to pdf

string ls_filepath

string ls_settings

 

ls_filepath = "" // we're keeping it empty and the browser will prompt us where to save

ls_settings = "" // this is a JSON serialize string for settings

//  ' We're going to add the following JSON:

//  ' { "displayHeaderFooter" : true,

//  '   "headerTitle" : "AntView PrintToPdf Demo",

//  '   "printBackground" : true,

//  '   "scale" : 0.5

//  ' }

ls_settings = "{"

ls_settings = ls_settings + ' "displayHeaderFooter": true, '

ls_settings = ls_settings + ' "headerTitle": "AntView PrintToPdf Demo", '

ls_settings = ls_settings + ' "printBackground": true, '

ls_settings = ls_settings + ' "scale": 0.5 '

ls_settings = ls_settings + "}"

edgewebbrowser.object.PrintToPdf(ls_filePath, ls_settings)

 

end event

 

to print the current page to Pdf via the PrintToPdf method.

You can supply a filepath to use, but as we didn't supply one, the control will ask where to save the Pdf via the standard save as windows file dialog.

 


AntView - The MS Edge WebView2 ActiveX control Date last changed: 09/25/2024