TsgcWebView2
TsgcWebView2 is a visual VCL component that wraps Microsoft Edge WebView2 (Chromium). Drop it on a form to embed a modern, full-featured web browser in your Delphi application. It supports Delphi 7 through Delphi 13.
Requirements
- Microsoft Edge WebView2 Runtime (included with Windows 10/11 or downloadable from Microsoft)
- WebView2Loader.dll (included, place next to your executable)
- Windows only
Quick Start
Drop TsgcWebView2 on a form and navigate to a URL:
sgcWebView21 := TsgcWebView2.Create(Self);
sgcWebView21.Parent := Self;
sgcWebView21.Align := alClient;
sgcWebView21.DefaultURL := 'https://www.example.com';
Features
Properties
- DefaultURL: string — URL to navigate to after initialization.
- AutoInitialize: Boolean — when True, the WebView2 is created automatically when the window handle is allocated. Default: True.
- ZoomFactor: Double — zoom level (1.0 = 100%).
- UserDataFolder: string — path for cookies, cache, and permissions storage.
- BrowserExecutableFolder: string — path to a fixed-version WebView2 Runtime distribution.
- Settings: TsgcWebView2Settings — design-time browser settings (ScriptEnabled, DevToolsEnabled, etc.).
- URL: string (read-only) — current page URL.
- DocumentTitle: string (read-only) — current page title.
- CanGoBack: Boolean (read-only) — whether back navigation is possible.
- CanGoForward: Boolean (read-only) — whether forward navigation is possible.
- Initialized: Boolean (read-only) — whether WebView2 is ready.
- IsMuted: Boolean — mute/unmute audio.
- IsDocumentPlayingAudio: Boolean (read-only) — whether the page is playing audio.
- FaviconURI: string (read-only) — URI of the current page favicon.
- StatusBarText: string (read-only) — current status bar text.
- CookieManager: TsgcWebView2CookieManager (read-only) — cookie management object.
- WebView: ICoreWebView2 (read-only) — direct COM interface access.
- Controller: ICoreWebView2Controller (read-only) — direct COM interface access.
- Environment: ICoreWebView2Environment (read-only) — direct COM interface access.
Methods
- Navigate(aURL: string) — navigate to a URL.
- NavigateToString(aHTML: string) — load HTML content directly.
- GoBack / GoForward — navigate history.
- Reload / Stop — reload or stop loading.
- ExecuteScript(aScript: string) — execute JavaScript asynchronously (result in OnScriptExecuted).
- ExecuteScriptSync(aScript: string): string — execute JavaScript synchronously, returns JSON result.
- AddInitScript(aScript: string) — add JavaScript that runs on every page load.
- RemoveInitScript(aScriptId: string) — remove a previously added init script.
- NavigateWithPostData(aURI, aMethod, aPostData, aHeaders: string) — navigate with custom HTTP method, body and headers.
- PrintToPdf(aFilePath: string) — save the page as PDF.
- ShowPrintUI — show the native print dialog.
- CapturePreviewToFile(aFilePath: string; aFormat: Integer) — screenshot (0=PNG, 1=JPEG).
- OpenDevToolsWindow — open Edge DevTools.
- OpenTaskManagerWindow — open Edge task manager.
- SetVirtualHostNameToFolderMapping(aHostName, aFolderPath: string; aAccessKind: Integer) — map hostname to local folder.
- ClearVirtualHostNameToFolderMapping(aHostName: string) — remove mapping.
- PostWebMessageAsString(aMessage: string) — send message to web content.
- PostWebMessageAsJson(aJson: string) — send JSON to web content.
- GetProfileName: string — get the browser profile name.
- ClearBrowsingData(aKinds: Cardinal) — clear specific browsing data types.
- ClearAllBrowsingData — clear all browsing data.
- PostSharedBufferToScript(aSharedBuffer: IUnknown; aAccess: Integer; aAdditionalDataAsJson: string) — share memory buffer with web content.
- InitializeWebView / FinalizeWebView — manual initialization control.
Events
- OnInitialized — fired when WebView2 is ready.
- OnNavigationStarting(aURI; aIsUserInitiated, aIsRedirected: Boolean; var aCancel: Boolean) — before navigation.
- OnNavigationCompleted(aIsSuccess: Boolean; aWebErrorStatus: Integer) — after navigation.
- OnSourceChanged(aIsNewDocument: Boolean) — URL changed.
- OnDocumentTitleChanged(aTitle: string) — page title changed.
- OnWebMessageReceived(aSource, aWebMessageAsJson, aWebMessageAsString: string) — message from web content.
- OnNewWindowRequested(aURI: string; aIsUserInitiated: Boolean; var aHandled: Boolean) — popup/new tab requested.
- OnScriptExecuted(aErrorCode: HRESULT; aResultAsJson: string) — async JS result.
- OnContentLoading(aIsErrorPage: Boolean) — content started loading.
- OnHistoryChanged — navigation history changed.
- OnProcessFailed(aKind: Integer) — browser process crashed.
- OnPermissionRequested(aURI: string; aKind: Integer; aIsUserInitiated: Boolean; var aState: Integer) — permission request.
- OnWindowCloseRequested — window.close() called.
- OnDownloadStarting(aURI, aResultFilePath: string; var aCancel, aHandled: Boolean; var aFilePath: string) — download started.
- OnDownloadProgress(aBytesReceived, aTotalBytes: Int64) — download progress.
- OnDownloadCompleted(aFilePath: string; aState: Integer) — download finished.
- OnContextMenuRequested(aMenuItems: string; aContextKind: Integer; aLocation: TPoint; var aHandled: Boolean) — right-click menu.
- OnFaviconChanged(aFaviconURI: string) — page icon changed.
- OnClientCertificateRequested(aHost: string; aPort: Integer; var aHandled: Boolean) — client cert needed.
- OnServerCertificateError(aRequestURI: string; aErrorStatus: Integer; var aAction: Integer) — cert error.
- OnStatusBarTextChanged(aText: string) — status bar text changed.
- OnDOMContentLoaded — DOM ready.
- OnBasicAuthRequested(aURI: string; var aUserName, aPassword: string; var aHandled: Boolean) — HTTP basic auth.
- OnCapturePreviewCompleted(aErrorCode: HRESULT) — screenshot finished.