TWebView2
Source: source/classes/twebview2.prg
TWebView2 embeds the Microsoft Edge (Chromium) browser engine into FiveWin applications. It provides a modern HTML5/CSS3/JavaScript rendering engine that can navigate URLs, display local HTML, execute JavaScript from Harbour, and receive JavaScript callbacks back into Harbour code. This replaces the older Internet Explorer-based TActiveX("Shell.Explorer.2") approach.
Architecture
TWindow] end subgraph "WebView2 Runtime" C[Edge Chromium
Renderer] D[User Data Folder] end subgraph "Content" E[Web URLs] F[Local HTML] G[JavaScript] end A --> B A -->|"hWebView"| C C --> D C --> E C --> F A <-->|"Eval / bOnBind"| G
Key DATA Members
| DATA | Type | Description |
|---|---|---|
hWebView | Numeric | WebView2 internal handle |
oWnd | TWindow | Parent window object |
bOnBind | Block | Callback block when JavaScript calls SendToFWH() |
bOnNavigationCompleted | Block | Callback when page navigation finishes |
bOnEval | Block | Callback with result of JavaScript evaluation |
cUserDataFolder | Character | Path for cookies, cache, and user data storage |
cBrowserExecutableFolder | Character | Optional Fixed Version Runtime folder (Microsoft browserExecutableFolder). Empty = system Evergreen Runtime |
Methods
| Method | Description |
|---|---|
New( oWndParent, cUserDataFolder, cBrowserExecutableFolder ) | Create WebView2 control. If oWndParent is nil, creates its own window. Optional 2nd param = isolated user-data folder; optional 3rd = Fixed Version Runtime root (folder with msedgewebview2.exe). Both must be passed at construction because the WebView2 environment is created immediately. |
Navigate( cURL ) | Navigate to a URL |
SetHtml( cHtml ) | Display HTML string directly |
Eval( cJavaScript ) | Execute JavaScript code in the web page |
InjectJavascript( cScript ) | Inject a <script> element into the page head |
SetTitle( cText ) | Set the window title |
SetSize( nWidth, nHeight ) | Resize the WebView2 control |
SetUserAgent( cUserAgent ) | Set custom User-Agent string |
SetParent( oWnd ) | Re-parent the WebView as a child window |
Center() | Center the parent window on screen |
Run() | Activate the parent window (starts message loop) |
OpenDevToolsWindow( lOnOff ) | Open/close the Chrome DevTools panel |
ShowDownloads( lOnOff ) | Show/hide the downloads panel |
End() | Destroy the WebView2 instance |
Harbour <-> JavaScript Communication
Example: Navigate to a URL
#include "FiveWin.ch"
function Main()
local oWV
oWV := TWebView2():New()
oWV:SetTitle( "FiveWin Browser" )
oWV:Navigate( "https://www.fivetechsoft.com" )
oWV:Center()
oWV:Run()
oWV:End()
return nil
Example: Display Local HTML
#include "FiveWin.ch"
function Main()
local oWV, cHtml
TEXT INTO cHtml
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Segoe UI; margin: 40px; background: #1a1a2e; color: #eee; }
h1 { color: #e94560; }
.card { background: #16213e; padding: 20px; border-radius: 10px; margin: 20px 0; }
button { background: #e94560; color: white; border: none; padding: 10px 20px;
border-radius: 5px; cursor: pointer; font-size: 14px; }
button:hover { background: #c73a52; }
</style>
</head>
<body>
<h1>FiveWin + WebView2</h1>
<div class="card">
<p>This HTML is rendered by the Chromium engine inside a FiveWin window.</p>
<button onclick="SendToFWH({action:'hello', msg:'Button clicked!'})">
Click Me
</button>
</div>
</body>
</html>
ENDTEXT
oWV := TWebView2():New()
oWV:SetTitle( "HTML Demo" )
oWV:SetHtml( cHtml )
oWV:bOnBind := { |hParams, oWV| MsgInfo( hParams[ "msg" ] ) }
oWV:Center()
oWV:Run()
oWV:End()
return nil
Example: Call JavaScript from Harbour
#include "FiveWin.ch"
function Main()
local oWnd, oWV, oBtn
DEFINE WINDOW oWnd TITLE "WebView2 + Controls"
oWV := TWebView2():New( oWnd )
oWV:Navigate( "https://www.example.com" )
// After navigation completes, inject JavaScript
oWV:bOnNavigationCompleted := { |cUrl|
// Change the page background
oWV:Eval( "document.body.style.backgroundColor = '#f0f0f0'" )
// Get the page title
oWV:Eval( "document.title" )
}
// Receive the result of Eval
oWV:bOnEval := { |cJson| oWnd:SetText( "Page: " + cJson ) }
ACTIVATE WINDOW oWnd MAXIMIZED
oWV:End()
return nil
Example: Embed in a Dialog
#include "FiveWin.ch"
function Main()
local oDlg, oWV
DEFINE DIALOG oDlg TITLE "Embedded WebView" SIZE 800, 600
ACTIVATE DIALOG oDlg ;
ON INIT ( ;
oWV := TWebView2():New( oDlg ), ;
oWV:Navigate( "https://www.fivetechsoft.com" ) ;
) ;
VALID ( oWV:End(), .T. )
return nil
Helper: FW_WebView()
// Quick one-liner to display HTML in a WebView
FW_WebView( cHtml, oWnd )
FW_WebView() is a convenience function that creates or reuses a TWebView2 to display
HTML content. If oWnd is an existing TWebView2, it reuses it; otherwise creates a new one.
Fixed Version Runtime (standalone)
On older systems (for example Windows Server 2012 R2) the current Evergreen WebView2
Runtime may no longer work, while Microsoft still supports distributing a
Fixed Version (standalone) package with the application. Pass the package
folder as the third parameter of New():
// System Evergreen Runtime (default)
oWV := TWebView2():New( oWnd )
// Fixed Version Runtime shipped with the app
oWV := TWebView2():New( oWnd, , "C:\MyApp\WebView2Runtime" )
// Isolated profile + Fixed Version
oWV := TWebView2():New( oWnd, "C:\MyApp\UserData", "C:\MyApp\WebView2Runtime" )
The path must be the Fixed Version root (the folder that contains
msedgewebview2.exe). Internally FWH loads
{folder}\EBWebView\x64\EmbeddedBrowserWebView.dll (or x86 for 32-bit).
If a folder is given but the DLL is not found there, FWH does not fall back
to the system Evergreen Runtime (so packaging errors are not hidden).
Requirements
- WebView2 Runtime: either the Evergreen runtime installed on the machine, or a Fixed Version package distributed with the app (see above)
- Evergreen download: WebView2 Runtime (ships with Windows 11 by default)
- User data (cookies, cache) is stored in
cUserDataFolder. If not specified, a default under the user's AppData is used