Skip to content

WebView2 Base Demo - Minimal Browser Embedding

Overview

This is the simplest "Hello World" demo for cWebView2Host, showing how to embed an Edge browser in a VB6 form with minimal code.

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

base/
  ├── Form1.frm              # Main form, containing WebView2 initialization code
  ├── vbman2_webview2.vbp    # VB6 project file
  └── vbman2_webview2.vbw    # VB6 workspace file

Core Code Walkthrough

1. Main Form (Form1.frm)

vb
Dim wv As New cWebView2Host

Private Sub Form_Load()
    wv.Initialize Me.hWnd, "https://vb6.pro"
End Sub

Feature Description

  1. Minimal Initialization

    • Uses Dim wv As New cWebView2Host for auto-instantiation, no Set statement needed
    • Initialize takes two parameters: window handle + URL, completing all initialization in one step
  2. Auto-Navigation

    • The second parameter provides the URL; Initialize automatically navigates after completion
    • No need to handle Ready event or manually call Navigate
  3. No Event Handling Required

    • Does not use WithEvents, handles no events
    • Suitable for scenarios that only need to display web pages without page interaction

Technical Notes

  1. Dim wv As New is VB6's auto-instantiation syntax — the object is automatically created on first access to wv
  2. Initialize Me.hWnd passes the form handle; WebView2 will be embedded as a child window
  3. Using the New keyword prevents WithEvents declaration; for event handling, use Dim WithEvents wv As cWebView2Host + Set wv = New cWebView2Host

Use Cases

  1. In-app help document browser
  2. Information display panel showing web content
  3. Minimal test to quickly verify WebView2 runtime environment

Extension Suggestions

  1. Add Form Resize event handling to respond to window size changes
  2. Add event handling for title synchronization, navigation control, and other interactive features

VB6 and LOGO copyright of Microsoft Corporation