TsgcWebView2 浏览器控件
在您的 Delphi VCL 应用程序中嵌入一个现代化、功能完备的 Microsoft Edge WebView2 (Chromium) 浏览器。将可视化组件拖放到窗体上,导航到任意 URL、运行 JavaScript、管理 Cookie 和下载,全部通过原生 Delphi 或 C++ Builder 代码完成。
在您的 Delphi VCL 应用程序中嵌入一个现代化、功能完备的 Microsoft Edge WebView2 (Chromium) 浏览器。将可视化组件拖放到窗体上,导航到任意 URL、运行 JavaScript、管理 Cookie 和下载,全部通过原生 Delphi 或 C++ Builder 代码完成。
一个封装 Microsoft Edge WebView2 (Chromium) 的可视化 VCL 组件。设置 Parent 和 Align,将 DefaultURL 指向某个页面,您便在应用中嵌入了一个真正的浏览器,并可在需要时完整访问 COM 接口。
TsgcWebView2
仅限 Windows
Standard / Professional / Enterprise
将 TsgcWebView2 拖放到窗体上,设置 Parent 和 Align,将 DefaultURL 指向某个页面,然后调用 Navigate,用 ExecuteScript 运行 JavaScript,并响应 OnNavigationCompleted。
uses
sgcWebView2;
var
WebView2: TsgcWebView2;
begin
WebView2 := TsgcWebView2.Create(Self);
WebView2.Parent := Self;
WebView2.Align := alClient;
WebView2.DefaultURL := 'https://www.example.com';
// navigate at runtime
WebView2.Navigate('https://www.esegece.com');
end;
// run JavaScript asynchronously (result in OnScriptExecuted)
procedure TForm1.Button1Click(Sender: TObject);
begin
WebView2.ExecuteScript('document.title');
end;
// react when the page has finished loading
procedure TForm1.WebView2NavigationCompleted(Sender: TObject;
aIsSuccess: Boolean; aWebErrorStatus: Integer);
begin
if aIsSuccess then
Caption := WebView2.DocumentTitle;
end;
// uses: sgcWebView2
TsgcWebView2 *WebView2 = new TsgcWebView2(this);
WebView2->Parent = this;
WebView2->Align = alClient;
WebView2->DefaultURL = "https://www.example.com";
// navigate at runtime
WebView2->Navigate("https://www.esegece.com");
// run JavaScript asynchronously (result in OnScriptExecuted)
WebView2->ExecuteScript("document.title");
// OnNavigationCompleted handler
void __fastcall TForm1::WebView2NavigationCompleted(TObject *Sender,
bool aIsSuccess, int aWebErrorStatus)
{
if (aIsSuccess)
Caption = WebView2->DocumentTitle;
}
// namespace: esegece.sgcWebSockets
var webView2 = new TsgcWebView2();
webView2.Parent = this;
webView2.Align = DockStyle.Fill;
webView2.DefaultURL = "https://www.example.com";
// navigate at runtime
webView2.Navigate("https://www.esegece.com");
// run JavaScript asynchronously (result in OnScriptExecuted)
webView2.ExecuteScript("document.title");
// OnNavigationCompleted handler
webView2.OnNavigationCompleted += (sender, e) =>
{
if (e.IsSuccess)
Text = webView2.DocumentTitle;
};
完整的 WebView2 功能面,以原生 Delphi 属性、方法和事件的形式暴露,并提供对 ICoreWebView2 的直接 COM 访问,用于任何尚未封装的功能。
Navigate 和 NavigateToString 加载 URL 或内存中的 HTML,GoBack / GoForward 浏览历史记录,Reload / Stop 控制加载,而 NavigateWithPostData 发起自定义方法、正文和标头。
使用 ExecuteScript(异步,结果在 OnScriptExecuted)或 ExecuteScriptSync 运行脚本以获取 JSON 返回值。AddInitScript 在每次页面加载时注入代码,而 PostWebMessageAsJson / PostWebMessageAsString 与页面通信,回复在 OnWebMessageReceived 中。
只读的 CookieManager 暴露 GetCookies、AddOrUpdateCookie、DeleteCookie 和 DeleteAllCookies,使您能够从 Delphi 代码读取、设置和清除任意主机的 Cookie。
通过 OnDownloadStarting、OnDownloadProgress 和 OnDownloadCompleted 端到端拦截和跟踪文件下载。取消、重定向目标路径,或在您自己的 UI 中报告进度。
通过 Settings(ScriptEnabled、DevToolsEnabled、ContextMenuEnabled)调整嵌入式浏览器,设置 ZoomFactor,用 UserDataFolder 隔离状态,并通过 BrowserExecutableFolder 固定一个固定版本的运行时。
用 PrintToPdf / ShowPrintUI 保存页面,用 CapturePreviewToFile 截图,通过 SetVirtualHostNameToFolderMapping 提供本地内容,用 OpenDevToolsWindow 打开 Edge DevTools,静音音频,并处理基本认证和服务器证书事件。
需要 Microsoft Edge WebView2 Runtime 以及放置在可执行文件旁的 WebView2Loader.dll。仅限 Windows,支持 Delphi 7 至 Delphi 13。
本组件所嵌入引擎的权威来源。