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

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

回答

要调用这些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.

你可以像这样使用它:

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;

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


以上是WebView2(TEdgeBrowser)更新了Delphi界面(例如ICoreWebView2Controller2)的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>