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

问题描述

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

解决方法

要调用 import kotlinx.android.synthetic.main.dialog_reward.* class RewardDialog: DialogFragment() { private var mView: View? = null override fun onCreateView( inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle? ): View? { return mView } override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { return activity?.let { mView = it.layoutInflater.inflate(R.layout.dialog_reward,null) AlertDialog.Builder(it).apply { setView(mView) }.create() } ?: throw IllegalStateException("Activity cannot be null") } override fun onViewCreated(view: View,savedInstanceState: Bundle?) { super.onViewCreated(view,savedInstanceState) //reference layout elements by name freely } override fun onDestroyView() { super.onDestroyView() mView = null } } 方法,您必须首先声明接口,然后在运行时使用 ICoreWebView2Controller2 获取对它的引用,最后调用该方法。

这里是我从 Microsoft 头文件开始创建的一个小单元:

QueryInterface

你可以像这样使用它:

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.

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