Skip to content

WebView2 ResFile Demo - Loading HTML from VB6 Resource Files

Overview

Demonstrates how to embed HTML content in VB6 resource files (.RES) and load it at runtime via NavigateToString into WebView2. Suitable for single-file distribution without external HTML file dependencies.

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/resFile directory.

resFile/
  ├── Form1.frm                    # Main form, containing resource loading logic
  ├── vbman2_webview2.RES          # VB6 resource file (containing embedded INDEX.HTML)
  ├── vbman2_webview2.vbp          # VB6 project file (contains ResFile32 reference)
  └── vbman2_webview2.vbw          # VB6 workspace file

Core Code Walkthrough

1. Deferred Initialization (Form1.frm)

vb
Dim WithEvents wv As cWebView2Host

Private Sub Form_Load()
    Set wv = New cWebView2Host
    wv.Initialize Me.hWnd       ' Pass only one parameter, don't navigate immediately
End Sub

Pass only the window handle without a URL, deferring navigation to the wv_Ready event.

2. Loading from Resources in Ready Event

vb
Private Sub wv_Ready()
    wv.NavigateToString VBMAN2.Res(LoadResData("INDEX.HTML", "WWW")).ReturnString()
End Sub

3. Title Synchronization

vb
Private Sub wv_DocumentTitleChanged()
    Me.Caption = wv.DocumentTitle
End Sub

Feature Description

  1. VB6 Resource File Embedding

    • HTML is stored in the .RES file's "WWW" custom resource type
    • Resource ID is "INDEX.HTML"
    • Embedded in the EXE after compilation, no external files needed
  2. NavigateToString

    • Loads HTML string directly into WebView2
    • Content uses about:blank as the base URL
    • Does not support relative path references to external resources
  3. VBMAN2.Res Helper Function

    • LoadResData("INDEX.HTML", "WWW") loads raw byte data
    • VBMAN2.Res() performs encoding conversion
    • .ReturnString() converts to string
  4. Deferred Navigation Pattern

    • Initialize without URL
    • Navigate in wv_Ready event
    • Ensures WebView2 is ready before loading content

Technical Notes

  1. LoadResData is a VB6 built-in function for loading custom resource types. Returns a Byte array

  2. VBMAN2.Res().ReturnString() is a helper method provided by the VBMAN2 library that handles byte-to-string encoding conversion (handling UTF-8, etc.)

  3. NavigateToString Limitations:

    • Base URL is about:blank
    • Cannot use relative paths to reference external CSS/JS/images
    • All resources must be inline (base64 images, inline CSS/JS)
  4. Resource File Editing: Use the VB6 Resource Editor to add custom resources with resource type "WWW" and ID "INDEX.HTML"

Use Cases

  1. Single-file EXE distribution (all HTML/CSS/JS embedded in resources)
  2. About dialogs / help pages
  3. Simple HTML report display
  4. Offline applications without external file dependencies

Extension Suggestions

  1. For HTML with external resource references, combine with SetVirtualHostNameToFolderMapping for resource mapping
  2. Multiple HTML resources can be embedded, dynamically switching display based on conditions
  3. Resources containing HTML can interact with VB6 via JS (postMessage / BindUI)

VB6 and LOGO copyright of Microsoft Corporation