TsgcWebView2 | Advanced Features

TsgcWebView2 provides access to advanced browser capabilities including printing, screenshots, audio control, certificate handling, and more.

Print Support

Use PrintToPdf to save the current page as a PDF file, or ShowPrintUI to display the native print dialog.


// Save the current page as PDF
sgcWebView21.PrintToPdf('C:\Output\page.pdf');


// Show the native print dialog
sgcWebView21.ShowPrintUI;

Screenshot Capture

Use CapturePreviewToFile to take a screenshot of the current page. The format parameter specifies the image type: 0 for PNG, 1 for JPEG. The OnCapturePreviewCompleted event fires when the capture finishes.


// Capture as PNG
sgcWebView21.CapturePreviewToFile('C:\Output\screenshot.png', 0);

// Capture as JPEG
sgcWebView21.CapturePreviewToFile('C:\Output\screenshot.jpg', 1);


procedure TFormMain.sgcWebView21CapturePreviewCompleted(
  Sender: TObject; aErrorCode: HRESULT);
begin
  if aErrorCode = S_OK then
    ShowMessage('Screenshot saved.')
  else
    ShowMessage('Screenshot failed: ' + IntToStr(aErrorCode));
end;

Audio / Mute Control

Use the IsMuted property to mute or unmute audio playback. Check IsDocumentPlayingAudio to detect whether the page is currently playing audio.


// Toggle mute
sgcWebView21.IsMuted := not sgcWebView21.IsMuted;

// Check if audio is playing
if sgcWebView21.IsDocumentPlayingAudio then
  LabelStatus.Caption := 'Audio is playing';

Certificate Handling

Use OnClientCertificateRequested to respond when a server requires a client certificate. Use OnServerCertificateError to handle TLS certificate errors (for example, self-signed certificates in development).


procedure TFormMain.sgcWebView21ClientCertificateRequested(
  Sender: TObject; const aHost: string; aPort: Integer;
  var aHandled: Boolean);
begin
  // Handle client certificate selection
  aHandled := True;
end;


procedure TFormMain.sgcWebView21ServerCertificateError(
  Sender: TObject; const aRequestURI: string;
  aErrorStatus: Integer; var aAction: Integer);
begin
  // Accept self-signed certificates in development
  // aAction: 0 = deny, 1 = allow
  aAction := 1;
end;

Virtual Host Mapping

Use SetVirtualHostNameToFolderMapping to map a hostname to a local folder. This lets web content reference local files using a virtual URL instead of file:// paths.


// Map "app.local" to a local folder
sgcWebView21.SetVirtualHostNameToFolderMapping(
  'app.local',
  'C:\MyApp\WebContent',
  0); // 0 = deny remote access

// Now navigate to local content using the virtual host
sgcWebView21.Navigate('https://app.local/index.html');


// Remove the mapping
sgcWebView21.ClearVirtualHostNameToFolderMapping('app.local');

Profile Management

Use GetProfileName to retrieve the current browser profile name. Use ClearBrowsingData or ClearAllBrowsingData to remove cached data, cookies, and other browsing artifacts.


// Get the profile name
ShowMessage('Profile: ' + sgcWebView21.GetProfileName);

// Clear all browsing data
sgcWebView21.ClearAllBrowsingData;

// Clear specific browsing data types
sgcWebView21.ClearBrowsingData($0001); // cache only

Basic Authentication

The OnBasicAuthRequested event fires when a server requests HTTP Basic authentication. Provide the credentials and set aHandled to True.


procedure TFormMain.sgcWebView21BasicAuthRequested(Sender: TObject;
  const aURI: string; var aUserName, aPassword: string;
  var aHandled: Boolean);
begin
  aUserName := 'admin';
  aPassword := 'secret';
  aHandled := True;
end;

Context Menu

The OnContextMenuRequested event fires when the user right-clicks in the browser. Set aHandled to True to suppress the default menu and show your own.


procedure TFormMain.sgcWebView21ContextMenuRequested(Sender: TObject;
  const aMenuItems: string; aContextKind: Integer;
  aLocation: TPoint; var aHandled: Boolean);
begin
  // Suppress the default context menu
  aHandled := True;

  // Show a custom popup menu at the click location
  PopupMenu1.Popup(aLocation.X, aLocation.Y);
end;

Favicon

The FaviconURI property returns the URI of the current page's favicon. The OnFaviconChanged event fires when the favicon changes.


procedure TFormMain.sgcWebView21FaviconChanged(Sender: TObject;
  const aFaviconURI: string);
begin
  LabelFavicon.Caption := 'Favicon: ' + aFaviconURI;
end;

Status Bar

The StatusBarText property returns the current status bar text (typically the URL of a hovered link). The OnStatusBarTextChanged event fires when the text changes.


procedure TFormMain.sgcWebView21StatusBarTextChanged(Sender: TObject;
  const aText: string);
begin
  StatusBar1.SimpleText := aText;
end;

Task Manager

Use OpenTaskManagerWindow to open the Edge browser task manager, which shows memory and CPU usage for each browser process.


sgcWebView21.OpenTaskManagerWindow;

Shared Buffer

Use PostSharedBufferToScript to share a memory buffer between your Delphi application and web content for high-performance data transfer.


// Share a buffer with web content
sgcWebView21.PostSharedBufferToScript(
  vSharedBuffer,  // IUnknown shared buffer object
  0,              // 0 = read-only, 1 = read-write
  '{"type":"image","width":640,"height":480}');

Direct COM Access

For advanced scenarios not covered by the component API, use the WebView, Controller, and Environment properties to access the underlying WebView2 COM interfaces directly.


var
  vWebView: ICoreWebView2;
begin
  vWebView := sgcWebView21.WebView;
  if Assigned(vWebView) then
  begin
    // Call any ICoreWebView2 method directly
  end;
end;