CWebView Class Events  
 

The class library reports state changes and notifications through a user-defined callback function which is specified when a browser instance is created or attached to a window. For more information, refer to the WebViewEventProc function. It is not required for an application to implement handlers for each event. When there is no event handler for a specific event, the browser takes its default action for that event. For example, if your application does not implement a handler for the WEBVIEW_EVENT_ACCELERATOR_KEYPRESS event then all accelerator key events are processed normally by the browser control.

Event Constants

Name Description
WEBVIEW_EVENT_WINDOW_ATTACHED This event is raised after the WebView2 control has been successfully attached to the specified host window. At this point the browser environment has been created, the controller has been initialized, and the browser instance is ready to accept navigation requests and configuration changes. Applications often use this event to perform initialization tasks such as setting a custom user agent, applying option flags, or starting the first navigation to a default URL. The event confirms that the control is operational and that subsequent API calls will succeed. The wParam value indicates success (1) or failure (0). If the browser could not be attached to the window, the lParam value will contain the HRESULT code returned by the browser's COM interface.
WEBVIEW_EVENT_WINDOW_DETACHED This event occurs when the WebView2 control has been detached from its host window, either because the host window is being destroyed or because the application explicitly requested detachment. After this event has fired, the instance is no longer valid and should not be used for navigation or script execution. Applications typically use this event to clean up resources associated with the WebView, save state, or update their user interface to reflect that the browser view is no longer available. Any references to the WebView instance should be considered invalid after detachment.
WEBVIEW_EVENT_FOCUS_CHANGED This event is raised whenever the browser window gains or loses keyboard focus relative to its host window. The wParam value indicates if focus was gained (1) or lost (0). This allows the application to track when user input is directed to the embedded browser versus other controls in the window. For example, you might use this event to update menu state, adjust shortcut key handling, or provide visual cues when the browser window is active.
WEBVIEW_EVENT_ACCELERATOR_KEYPRESS This event is raised when the user presses an accelerator key combination such as the arrow keys, Page Up and Page Down, Insert, Delete, or other keys that the embedded browser normally handles. The wParam value is the virtual-key code and lParam is the accelerator flags. The event gives your application a chance to intercept these keystrokes before they are processed by the browser. This is especially useful when you want to provide custom navigation, prevent certain keys from being used, or integrate the control with application-level keyboard shortcuts. To prevent the default behavior being taken by the browser, call the KeyEventHandled method from within this event.
WEBVIEW_EVENT_NAVIGATION_STARTING This event is fired when a new navigation request begins. The wParam value indicates success (1) or failure (0) and lParam is the navigation flags. The event is raised before any network request is made, ensuring that no data is sent until the handler has had a chance to approve or deny the navigation. The GetNavigationUrl method can be called to determine the new URL the browser is navigating to and the CancelNavigation method can be used to prevent the navigation.
WEBVIEW_EVENT_NAVIGATION_COMPLETED This event is raised when a navigation request has finished loading. It is triggered regardless of whether the navigation succeeded or failed, with the wParam value indicating success (1) or failure (0). If the navigation has failed, the lParam value is the error code. Applications typically handle this event to update their UI once the content has loaded, or to implement custom error handling when navigation fails. Because the event always fires, even on errors, it is a reliable notification point for the end of a navigation sequence.
WEBVIEW_EVENT_SECURITY_CHANGED This event occurs when the security level of the current connection changes. The wParam value is the security level and lParam is the security flags. For example, the level may change from neutral to secure once a certificate has been validated, or from secure to warning if mixed content is detected. Additional information about the state of the connection can be obtained by calling the GetSecurityInfo method. Applications often use this event to update visual indicators such as a padlock icon or to apply additional security-related policies. It is important to note that this event may be raised multiple times during a single navigation, and earlier values should be discarded in favor of the most recent state.
WEBVIEW_EVENT_HISTORY_CHANGED This event occurs whenever the browsing history changes. The wParam value is the history movement flags. Navigating to a new page, using the GoBack or GoForward methods, or programmatically calling navigation methods can trigger this event. Your application can respond by enabling or disabling UI elements such as toolbar buttons depending on whether it is possible to go back or forward.
WEBVIEW_EVENT_DOWNLOAD_STARTING This event occurs when a file download is initiated either by a user clicking on a link or navigating to a URL which specifies content that cannot be rendered in the browser. The wParam value indicates success (1) or failure (0) and lParam is the unique file identifier which can be used to identify the specific download. This identifier can be used with the GetDownloadInfo method to obtain information about the download in progress.
WEBVIEW_EVENT_DOWNLOAD_COMPLETED This event occurs when a file download has completed. The wParam value indicates success (1) or failure (0)  and lParam is the unique file identifier. If the file download is canceled, it is considered to be a failed download. The GetDownloadInfo method can be used to obtain information about the download, such as the total number of bytes copied, how much time elapsed and the full path of the local file that was created.
WEBVIEW_EVENT_MESSAGE_RECEIVED This event occurs when a message is posted from JavaScript code in the web page using window.chrome.webview.postMessage. The message is delivered as a string, which can be retrieved using the GetWebMessage method. The message may include structured data such as JSON or it may be a simple JSON formatted string. This event provides a two-way communication channel between the page content and the host application, allowing developers to implement hybrid scenarios where the web page can notify the native host of user actions, requests, or state changes. If messages are not enabled, this event will not be raised.
WEBVIEW_EVENT_SCRIPT_COMPLETED This event is raised when a script execution initiated by the application completes. The wParam value indicates success (1) or failure (0) and lParam is the user-defined parameter provided when the AsyncExecute method was called. The GetWebResult method can be called to obtain the result of the script execution. If the script contained errors or could not be executed, the status will reflect a failure condition. Applications typically use this event to retrieve values from the page, execute custom logic in response to script results, or detect when asynchronous script operations have finished. Because script execution is asynchronous, this event is the definitive way to know when a script call has finished.
WEBVIEW_EVENT_DOM_CONTENT_LOADED This event occurs when the browser has finished parsing the HTML document and the Document Object Model (DOM) is ready. This event typically occurs before the WEBVIEW_EVENT_NAVIGATION_COMPLETED event and does not indicate that all external resources, such as images and stylesheets, have finished loading. The event is raised for the top-level document and does not occur separately for embedded frames. It may be used to determine when the document is ready for interaction or JavaScript execution.

Remarks

Some events, such as WEBVIEW_EVENT_NAVIGATION_STARTING and WEBVIEW_EVENT_ACCELERATOR_KEYPRESS, allow your application to influence behavior by canceling an action or handling a keypress. Others, like WEBVIEW_EVENT_HISTORY_CHANGED or WEBVIEW_EVENT_SECURITY_CHANGED, are informational and allow you to update user interface elements or record state.

If your application does not enable specific options (such as allowing scripts, dialogs, or message passing), the corresponding events will not be raised. For example, if messages are disabled, the WEBVIEW_EVENT_MESSAGE_RECEIVED event will never occur. This makes it important to configure options appropriately for the scenarios you wish to support.

See Also