WebView2 (TEdgeBrowser) 更新了 Delphi 界面(例如 ICoreWebView2Controller2)

wor*_*rdy 2 delphi webview2

默认的 Delphi 10.4.2 TEdgeBrowser 界面目前只是原始发布的 WebView2。但是,似乎要在非白色背景上实现无闪烁负载,我们需要使用ICoreWebView2Controller2设置背景颜色。如何从 Delphi 访问它(以向后兼容的方式)?我曾尝试从 Microsoft 更新的 WebView2 nuget 包中导入 .tlb,但是 Delphi 给出了 OLE 错误,因此我找不到使用新功能生成 Delphi WebView2 界面的方法。

Mar*_*dor 6

要获取最新的接口而不是捆绑的 Delphi,请按照以下步骤操作:

  1. 下载最新的Microsoft.Web.WebView2包。
  2. Nuget 文件是一个 zip 存档,从中提取 WebView2.tlb
  3. Run "C:\Program Files (x86)\Embarcadero\Studio\{Ver}\bin\tlibimp.exe" -P WebView2.tlb
    Were {Ver} 是您的 RAD Studio 版本。
    它将创建一个具有可用接口的 WebView2_TLB.pas 文件
  4. 在您的项目中需要的地方链接 TLB 单元

示例代码使用:

var 
  WebView: ICoreWebView2_7;
  Controller: ICoreWebView2Controller3;
begin
  // May cast directly if you are sure WebView2 runtime supports it
  WebView := ICoreWebView2_7(Browser.DefaultInterface);
  // Or can check if the interface is available
  if Browser.DefaultInterface.QueryInterface(ICoreWebView2_7, WebView) = S_OK then
  // The same for WebView Controller:
  Controller := ICoreWebView2Controller3(Browser.ControllerInterface);
  // or using QueryInterface:
  if Browser.ControllerInterface.QueryInterface(ICoreWebView2Controller3, Controller) = S_OK then
  // Environment interface is accessile through
  // Browser.EnvironmentInterface
Run Code Online (Sandbox Code Playgroud)


fpi*_*tte 5

要调用这些ICoreWebView2Controller2方法,您必须首先声明接口,然后在运行时使用它QueryInterface来获取对它的引用,最后调用该方法。

在我从 Microsoft 头文件开始创建的一个小单元之后:

unit Ovb.WebView2;

interface

uses
    WebView2;

const
    IID_ICoreWebView2Controller2: TGUID = '{C979903E-D4CA-4228-92EB-47EE3FA96EAB}';

type
    COREWEBVIEW2_COLOR = packed record
        A : BYTE;
        R : BYTE;
        B : BYTE;
        G : BYTE;
    end;
    TCOREWEBVIEW2_COLOR = COREWEBVIEW2_COLOR;
    PCOREWEBVIEW2_COLOR = ^COREWEBVIEW2_COLOR;

  ICoreWebView2Controller2 = interface(ICoreWebView2Controller)
      ['{C979903E-D4CA-4228-92EB-47EE3FA96EAB}']
      function get_DefaultBackgroundColor(backgroundColor : PCOREWEBVIEW2_COLOR) : HRESULT; stdcall;
      function put_DefaultBackgroundColor(backgroundColor : TCOREWEBVIEW2_COLOR) : HRESULT; stdcall;
  end;


implementation

end.
Run Code Online (Sandbox Code Playgroud)

你可以像这样使用它:

procedure TEdgeViewForm.EdgeBrowser1CreateWebViewCompleted(
    Sender  : TCustomEdgeBrowser;
    AResult : HRESULT);
var
    Ctrl2     : ICoreWebView2Controller2;
    BackColor : TCOREWEBVIEW2_COLOR;
    HR        : HRESULT;
begin
    Sender.ControllerInterface.QueryInterface(IID_ICoreWebView2Controller2, Ctrl2);
    if not Assigned(Ctrl2) then
        raise Exception.Create('ICoreWebView2Controller2 not found');
    // Select red background
    BackColor.A := 255;
    BackColor.R := 255;
    BackColor.G := 0;
    BackColor.B := 0;
    HR := Ctrl2.put_DefaultBackgroundColor(BackColor);
    if not SUCCEEDED(HR) then
        raise Exception.Create('put_DefaultBackgroundColor failed');
end;
Run Code Online (Sandbox Code Playgroud)

我已经使用 Embarcadero EdgeView 演示测试了我的代码。红色背景是可见的,所以我认为我的代码是正确的。