Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Control to embed WebView2 in WinForms.
public class WebView2 : System.Windows.Forms.Control
public class WebView2 : System.Windows.Forms.Control, System.ComponentModel.ISupportInitialize
type WebView2 = class inherit Control
type WebView2 = class inherit Control interface ISupportInitialize
Public Class WebView2 Inherits Control
Public Class WebView2 Inherits Control Implements ISupportInitialize
This control is effectively a wrapper around the [WebView2 COM API](https://aka.ms/webview2).
You can directly access the underlying ICoreWebView2 interface and all of its functionality by accessing the CoreWebView2 property. Some of the most common COM functionality is also accessible directly through wrapper methods/properties/events on the control.
Upon creation, the control's CoreWebView2 property will be null. This is because creating the CoreWebView2 is an expensive operation which involves things like launching Edge browser processes. There are two ways to cause the CoreWebView2 to be created: 1) Call the EnsureCoreWebView2Async(CoreWebView2Environment) method. This is referred to as explicit initialization. 2) Set the Source property. This is referred to as implicit initialization. Either option will start initialization in the background and return back to the caller without waiting for it to finish. To specify options regarding the initialization process, either pass your own CoreWebView2Environment to EnsureCoreWebView2Async or set the control's CreationProperties property prior to initialization.
When initialization has finished (regardless of how it was triggered) then the following things will occur, in this order: 1) The control's CoreWebView2InitializationCompleted event will be invoked. If you need to perform one time setup operations on the CoreWebView2 prior to its use then you should do so in a handler for that event. 2) If a Uri has been set to the Source property then the control will start navigating to it in the background (i.e. these steps will continue without waiting for the navigation to finish). 3) The Task returned from EnsureCoreWebView2Async(CoreWebView2Environment) will complete.
For more details about any of the methods/properties/events involved in the initialization process, see its specific documentation.
Accelerator key presses (e.g. Ctrl+P) that occur within the control will fire standard key press events such as OnKeyDown. You can suppress the control's default implementation of an accelerator key press (e.g. printing, in the case of Ctrl+P) by setting the Handled property of its EventArgs to true. Also note that the underlying browser process is blocked while these handlers execute, so:
- You should avoid doing a lot of work in these handlers.
- Some of the WebView2 and CoreWebView2 APIs may throw errors if invoked within these handlers due to being unable to communicate with the browser process.
Create a new WebView2 WinForms control.
After construction the CoreWebView2 property is
Enable/disable external drop.
Returns true if the webview can navigate to a next page in the
navigation history via the GoForward() method.
This is equivalent to the CanGoForward.
If the underlying CoreWebView2 is not yet initialized, this property is
The underlying CoreWebView2. Use this property to perform more operations on the WebView2 content than is exposed on the WebView2. This value is null until it is initialized and the object itself has undefined behaviour once the control is disposed. You can force the underlying CoreWebView2 to initialize via the EnsureCoreWebView2Async(CoreWebView2Environment, CoreWebView2ControllerOptions) method.
Protected CreateParams property. Used to set custom window styles to the forms HWND.
Gets or sets a bag of options which are used during initialization of the control's CoreWebView2. This property cannot be modified (an exception will be thrown) after initialization of the control's CoreWebView2 has started.
The default background color for the WebView.
The Source property is the URI of the top level document of the
WebView2. Setting the Source is equivalent to calling Navigate(String).
Setting the Source will trigger initialization of the CoreWebView2, if not already initialized.
The default value of Source is
The zoom factor for the WebView.
Cleans up any resources being used.
Explicitly trigger initialization of the control's CoreWebView2.
Explicitly trigger initialization of the control's CoreWebView2.
Protected focus handler.
Protected OnGotFocus handler.
Overrides the base OnPaint event to have custom actions in designer mode
Protected OnParentChanged handler.
Protected SizeChanged handler.
Protected VisibilityChanged handler.
Protected command key handler.
Protected Select method: override this to capture tab direction when WebView control is activated
Overrides the base WndProc events to handle specific window messages.
AcceleratorKeyPressed dispatches when an accelerator key is pressed and before it gets handled by the WebView2.
You can write a custom handler to suppress handling by the Webview by doing
ContentLoading dispatches after a navigation begins to a new URI and the content of that URI begins to render. This is equivalent to the ContentLoading event.
This event is triggered either 1) when the control's CoreWebView2 has finished being initialized (regardless of how it was triggered or whether it succeeded) but before it is used for anything OR 2) the initialization failed. You should handle this event if you need to perform one time setup operations on the CoreWebView2 which you want to affect all of its usages (e.g. adding event handlers, configuring settings, installing document creation scripts, adding host objects).
This event is triggered when the control's CoreWebView2 has finished being initialized (regardless of how initialization was triggered) but before it is used for anything. You should handle this event if you need to perform one time setup operations on the CoreWebView2 which you want to affect all of its usages (e.g. adding event handlers, configuring settings, installing document creation scripts, adding host objects).
NavigationCompleted dispatches after a navigate of the top level document completes rendering either successfully or not. This is equivalent to the NavigationCompleted event.
SourceChanged dispatches after the Source property changes. This may happen during a navigation or if otherwise the script in the page changes the URI of the document. This is equivalent to the SourceChanged event.
WebMessageReceived dispatches after web content sends a message to the
app host via
Explicit Interface Implementations