首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WebView2 (TEdgeBrowser)更新的Delphi接口(例如ICoreWebView2Controller2)

WebView2 (TEdgeBrowser)更新的Delphi接口(例如ICoreWebView2Controller2)
EN

Stack Overflow用户
提问于 2021-03-17 23:55:17
回答 1查看 285关注 0票数 2

默认的Delphi10.4.2 TEdgeBrowser接口目前只有原始版本的WebView2。然而,似乎要在非白色背景上加载无闪烁负载,我们需要使用ICoreWebView2Controller2设置背景颜色。如何从Delphi访问它(以向后兼容的方式)?我尝试过从微软更新的Delphi包中导入.tlb,但是WebView2给出了一个OLE错误,所以我找不到一种方法来生成一个带有新函数的Delphi WebView2界面。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-18 01:42:13

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

下面是我从微软头文件开始创建的一个小单元:

代码语言:javascript
复制
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.

例如,您可以像这样使用它:

代码语言:javascript
复制
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演示测试了我的代码。红色背景是可见的,所以我认为我的代码是正确的。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66676662

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档