The class invokes the callback function whenever an event
occurs. Applications are not required to implement event handlers for
each event, and may choose to not provide any event handlers by
passing NULL as the callback function parameter. A minimal implementation
would typically create handlers for the
WEBVIEW_EVENT_NAVIGATION_STARTING event which notifies
the application when the browser is beginning to navigate to a new
location, and the WEBVIEW_EVENT_NAVIGATION_COMPLETED
event which indicates the navigation has completed.
When you attach a browser instance to an existing window, the
callback function is always invoked on the same UI thread that owns
that window, ensuring events are processed in the context of the caller's
thread. However, if you create a new browser window using the
Create method, a different thread manages the
top-level window and message loop, and in that case the callback
function is invoked on the thread that owns the browser window rather
than the thread that created it. This distinction is important when
your application accesses shared data or resources from within the
event handler. To avoid race conditions or undefined behavior, ensure
that access to shared state is synchronized using mutexes, critical
sections, or other appropriate synchronization primitives.
It is important to keep event handlers responsive. Because events are raised
on the UI thread, long-running work in an event handler can block the user
interface and stall navigation. Where possible, defer heavy operations to
background threads. Avoid displaying modal dialogs or message
boxes from within event handlers. Also note that some events may fire
multiple times during a single navigation. For example, the security
state can transition through several levels (unknown, neutral, secure,
or warning) before settling on a final value. Applications should be
written to handle these changes incrementally.
After the browser has been attached to a window, it is possible to
change the event handler using the EnableEvents
function. This changes the event handler for the browser instance,
and from that point forward all event notifications will be routed
to the new handler.
When defining an event handler within a C++ class, the event
callback must be declared as a static function with the __stdcall
calling convention. This is required because the library calls the
handler just as it would with any Win32 API callback function,
without knowledge of C++ instance objects. Non-static member functions
include a hidden this pointer parameter, which makes
their signature incompatible with the expected WEBVIEWEVENTPROC
type. A common pattern is to pass the object's this pointer as
the dwParam value, then cast it back inside the static callback
function to access the instance. This preserves object-oriented design
while still meeting the calling convention requirements of the API.