WebView2 LoadFile Demo - Loading Local HTML Files
Overview
Demonstrates how to load HTML files from the local file system via cWebView2Host, perfectly combining desktop applications with rich web frontends.
Project Structure
Download Source [ Note: Re-register the DLL file in the bin directory ]
Source code is available in the VBMAN2 project's demos/webview2/loadFile directory.
loadFile/
├── Form1.frm # Main form, containing local file loading logic
├── www/
│ └── index.html # Local HTML page (922-line complete landing page)
├── vbman2_webview2.vbp # VB6 project file
└── vbman2_webview2.vbw # VB6 workspace fileCore Code Walkthrough
1. Declaration and Initialization (Form1.frm)
Dim wv As New cWebView2Host
Private Sub Form_Load()
wv.Initialize Me.hWnd, App.Path & "\www"
End SubWhen the second parameter of Initialize is a local folder path, cWebView2Host automatically maps it as WebView2-accessible resources.
2. Local HTML Page (www/index.html)
www/index.html is a complete single-page website featuring:
- SVG logo and branding
- CSS animations and transitions
- IntersectionObserver scroll reveal effects
- Responsive layout
- Complete modern web page effects
This proves that WebView2 can perfectly render complex modern web content.
Feature Description
Folder Path Auto-Mapping
- Pass a local path like
App.Path & "\www" - Internally auto-maps to
http://vbman2.com/index.html - Relative paths in pages (CSS, JS, images) are automatically resolved
- Pass a local path like
Full Web Capabilities
- Supports CSS3 animations and transitions
- Supports ES6+ JavaScript
- Supports modern APIs like IntersectionObserver
- Supports SVG vector graphics
Zero-Configuration Local Serving
- No need to start a local HTTP server
- No configuration files or parameters needed
- Simply pass the folder path
Technical Notes
App.Path: VB6 runtime property that returns the directory where the EXE is located. Using
App.Path & "\www"ensures HTML files are found regardless of where the program runsPath Mapping Principle: cWebView2Host uses WebView2's
SetVirtualHostNameToFolderMappingAPI to map folders as virtual host namesResource References: Relative path references in HTML (e.g.,
<link href="style.css">,<script src="app.js">) all work normallyProtocol Limitation: Folder mapping mode uses
http://protocol; ifhttps://protocol is needed, manually callSetVirtualHostNameToFolderMapping
Use Cases
- Desktop application embedded help documentation
- Offline web applications (no remote server needed)
- Data visualization dashboards
- Rich UI interfaces implemented with HTML/CSS/JS
Extension Suggestions
- Use
SetVirtualHostNameToFolderMappingto specify custom domain and https protocol - Local HTML can communicate with VB6 via
window.chrome.webview.postMessage - Combine with BindUI/BindData for two-way interaction between local HTML UI and VB6 data