默认的Delphi10.4.2 TEdgeBrowser接口目前只有原始版本的WebView2。然而,似乎要在非白色背景上加载无闪烁负载,我们需要使用ICoreWebView2Controller2设置背景颜色。如何从Delphi访问它(以向后兼容的方式)?我尝试过从微软更新的Delphi包中导入.tlb,但是WebView2给出了一个OLE错误,所以我找不到一种方法来生成一个带有新函数的Delphi WebView2界面。
发布于 2021-03-18 01:42:13
要调用ICoreWebView2Controller2方法,必须首先声明接口,然后在运行时使用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.例如,您可以像这样使用它:
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演示测试了我的代码。红色背景是可见的,所以我认为我的代码是正确的。
https://stackoverflow.com/questions/66676662
复制相似问题