Purpose You want to use the new WebView2 control instead of Internet Explorer. The WebView2 control uses Microsoft Edge (Chromium) as the rendering module to display the web content in native apps. |
Solution
Example
|
How to implement First create a Windows Forms application in which you use the new webView2 control. You can find instructions on how to do this, for example, here: Get started with WebView2 in Windows Forms The project is then compiled as a class library, giving us a .DLL file that we can use with GuiXT. A more detailed example is described in the following tutorial: Embedding windows forms with GuiXT You can also download and customize the project for this example at the bottom of the page or just use the libraries. Hint: At the current time (August 2021), it is necessary to install either the developer version of the Edge browser or the WebView2 Runtime. It is expected that Microsoft will ship the control as part of regular updates in the future. |
Exchange data between GuiXT and the webView2 control GuiXT -> JavaScript We can send any JavaSCript code as text to the current site in the webView2 control. This code can manipulate the DOM, read data or just call existing JavaScript functions. Please have a look at the InputScript "run_javascript.txt" JavaScript -> GuiXT There are several ways to send messages and data from JavaScript to the VB.NET control. One simple way is to call the following code: window.chrome.webview.postMessage('Message from GuiXT'); The message is the handled in: ' Recieve messages from JavaScript Private Sub WebView21_WebMessageReceived(... Class "guixt" in VB |
GuiXT Script title "GuiXT Controls - webView2"
TextBox
(22,-0.8)
(27.1,68.3)
name=
"mycode"
Pushbutton
(21,52)
"@ME@navigate to"
_
process=
"navigate_to_url.txt"
size=
(1,16)
using
URL =
[myurl]
Pushbutton
(21,18)
"@OA@run javascript"
_
process=
"run_javascript.txt"
size=
(1,14)
Pushbutton
(21,34)
"@73@open debugmode"
_
process=
"open_debugmode.txt"
InputField
(20,0)
"URL"
(20,18)
_
size=
50
name=
"myurl"
default=
"https://www.whatismybrowser.com/"
// area for VB.NET control.
Control
(-0.6,-0.8)
(18.7,124.5)
_
progid=
"Shell.Explorer"
name=
"r1"
initflag=
"r1init"
-closeonhide
// embed control
if
V[r1init]
callVB
guixt_webview2.webview2.setUserDir
dir:=
"C:\guixtscripts"
CallVB
guixt_webview2.webview2.embed
"&V[r1]"
endif
callVB
guixt_webview2.webview2.setSize
width:=
1000
height:=
500
|
InputScript "navigate_to_url.txt" Navigates to a given URL in the webView2 control Parameter URL
clear
V[r]
callVB
r =
guixt_webview2.webview2.navigate2
url:=
"&U[URL]"
if
V[r]
// Exception occured
message
"&V[r]"
-statusline
endif
return
|
InputScript "open_debugmode.txt" Opens the debug window of the browser (F12 mode). callVB guixt_webview2.webview2.opendevmode
return
|
InputScript "run_javascript.txt" Here JavaScript code can be executed from GuiXT on the loaded page. callVB guixt_webview2.webview2.injectJS code:= "&text[mycode]"
return
|
VB.NET CodingImports guinet Imports Microsoft.Web.WebView2.WinForms Public Class webview2 ' interface to GuiXT Private myguixt As guixt Private userDir As String = "C:\temp" ' Cache directory for the webVie2 control Public Sub setUserDir(dir As String) userDir = dir End Sub ' embed into SAP GUI window Public Sub embed(ByVal w As Object) guixt.EmbedForm(Me, w) End Sub ' Sets the size of the whole application Public Sub setSize(width As Integer, height As Integer) Me.Width = width Me.Height = height End Sub ' Open an URL in the webView2 control Public Function navigate2(url As String) As String Dim r As String = "" Try WebView21.CoreWebView2.Navigate(url) Catch ex As Exception r = ex.Message End Try Return r End Function ' Run some javaScript code in the control Public Sub injectJS(code As String) WebView21.CoreWebView2.ExecuteScriptAsync(code) End Sub ' Open F12 developer mode Public Sub openDevMode() WebView21.CoreWebView2.OpenDevToolsWindow() End Sub ' Recieve messages from JavaScript Private Sub WebView21_WebMessageReceived(sender As Object, _ e As _ Microsoft.Web.WebView2.Core.CoreWebView2WebMessageReceivedEventArgs) _ Handles WebView21.WebMessageReceived ' Handle massages here MsgBox(e.TryGetWebMessageAsString) End Sub Private Sub webview2_Load(sender As Object, e As EventArgs) _ Handles Me.Load InitializeAsync() End Sub Private Async Sub InitializeAsync() ' Important: Provide a caching directory for the control, ' default is path of the executable/dll ' -> SAP GUI directory if embedded with GuiXT Try Dim createProps As New CoreWebView2CreationProperties createProps.UserDataFolder = Me.userDir WebView21.CreationProperties = createProps Await WebView21.EnsureCoreWebView2Async(Nothing) Catch ex As Exception MsgBox(ex.Message) End Try End Sub End Class |
Downloads VB.NET project guixt_webview2.zip Classlibraries only guixt_webview2_dll.zip GuiXT Scripts guixt_webview2_scripts.zip |
Components |