代码之家  ›  专栏  ›  技术社区  ›  Coder12345

在Delphi/C++ Builder中使用WebVIEW(EdgeHTML)

  •  4
  • Coder12345  · 技术社区  · 6 年前

    我是否正确理解EdgeHTML现在在Windows10中可用于桌面(Win32/Win64应用程序)?根据这些博客文章:

    https://blogs.windows.com/msedgedev/2018/05/09/modern-webview-winforms-wpf-apps/ https://blogs.windows.com/msedgedev/2018/10/04/edgehtml-18-october-2018-update/ https://docs.microsoft.com/en-us/windows/communitytoolkit/controls/wpf-winforms/webview

    如果这是真的,那么在Delphi/C++ Builder中是否有可能使用这个方法呢?还是我们必须等待一个新的。 在RAD Studio的新更新中的控件?如果可能的话,有没有代码样本可以查看(C++ Builder或Delphi?)NET的要求是否意味着它不能在RAD Studio生产的常规Win32/Win64应用程序中使用?

    2 回复  |  直到 4 年前
        1
  •  3
  •   NineBerry    5 年前

    RAD Studio 10.4悉尼 对微软新推出的基于Chromium的Edge浏览器提供了增强的支持。

    有两个新控件 TEdgeBrowser 即可以直接使用边缘浏览器引擎以及允许经典的可能性 TWebBrowser TWebBrowser.SelectedEngine 财产。

    Embarcadero在博客中的详细解释:

        2
  •  15
  •   NineBerry    5 年前

    这个答案已经过时了,但是了解一下技术背景可能会很有趣。radstudio10.4sydney现在支持使用现成的Edge浏览器。看到了吗 my other answer .


    WebView控件是通过WinRT提供的,不依赖于.net。您可以从普通的Win32应用程序中使用它。

    WinRT(Windows运行时),现在在windows10中更名为UWP(通用Windows平台),有点像COM的继承者。

    与COM一样,它在很大程度上是基于接口的,可用的接口是在类型库中定义的。对于WinRT,类型库存储在Windows系统目录的*.WinMD文件中。包含嵌入边缘浏览器所需功能的类型库是 Windows.Web.winmd

    Delphi确实支持使用WinRT组件,它附带了一些类型库的翻译和一些附加的帮助函数和类来使用WinRT。

    如果安装Windows平台SDK,则可以找到类似于目录中的WINT类型库的IDL和C++翻译。 Drive:\Windows Kits\10\Include\10.0.17134.0\winrt

    我使用这些文件作为模板创建了一个非常基本的概念验证Delphi项目(用于delphi10.2),该项目使用嵌入式边缘浏览器。你可以找到下面的代码。为了测试这一点,只需创建一个新的VCL项目,粘贴代码并连接 FormCreate FormDestroy FormResize 窗体中的事件。

    unit Unit1;
    
    interface
    
    uses
      Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
      Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,
      System.Types,
      Winapi.Winrt,
      System.Win.WinRT,
      WinAPI.Foundation,
      WinAPI.Foundation.Types;
    
    const
      SWebViewControlProcess = 'Windows.Web.UI.Interop.WebViewControlProcess';
    
    type
      // Interface with functionality to interact with WebBrowser Control
      // https://docs.microsoft.com/en-us/uwp/api/windows.web.ui.iwebviewcontrol
      IWebViewControl = interface(IInspectable)
      ['{3F921316-BC70-4BDA-9136-C94370899FAB}']
        procedure Placeholder_SourceGet; safecall;
        procedure Placeholder_SourcePut; safecall;
        procedure Placeholder_DocumentTitle; safecall;
        procedure Placeholder_CanGoBack; safecall;
        procedure Placeholder_CanGoForward; safecall;
        procedure Placeholder_DefaultBackgroundColorPut; safecall;
        procedure Placeholder_DefaultBackgroundColorGet; safecall;
        procedure Placeholder_ContainsFullScreenElement; safecall;
        procedure Placeholder_Settings; safecall;
        procedure Placeholder_DeferredPermissionRequests; safecall;
        procedure Placeholder_GoForward; safecall;
        procedure Placeholder_GoBack; safecall;
        procedure Placeholder_Refresh; safecall;
        procedure Placeholder_Stop; safecall;
        procedure Navigate(source: IUriRuntimeClass); stdcall;
        procedure NavigateToString(text: HString); stdcall;
        // TODO: Declare further properties and functions of IWebViewControl
      end;
    
      IWebViewControlProcess = interface;
    
      // Declare  IWebViewControlSite
      IWebViewControlSite = interface(IInspectable)
      ['{133F47C6-12DC-4898-BD47-04967DE648BA}']
        function get_Process: IWebViewControlProcess; safecall;
        procedure put_Scale(value: Double); safecall;
        function get_Scale: Double; safecall;
        procedure put_Bounds(value: TRectF); safecall;
        function get_Bounds: TRectF; safecall;
        procedure put_IsVisible(value: Boolean); safecall;
        function get_IsVisible: Boolean; safecall;
    
        // TODO: Declare further properties and functions of IWebViewControlSite
    
        property Process: IWebViewControlProcess read get_Process;
        property Scale: Double read get_Scale write put_Scale;
        property Bounds: TRectF read get_Bounds write put_Bounds;
        property IsVisible: Boolean read get_IsVisible write put_IsVisible;
      end;
    
      // types for reacting to when the WebView has finished initialization
      IAsyncOperation_1__IWebViewControl = interface;
    
      IAsyncOperationCompletedHandler_1__IWebViewControl = interface(IUnknown)
      ['{d61963d6-806d-50a8-a81c-75d9356ad5d7}']
        procedure Invoke(asyncInfo: IAsyncOperation_1__IWebViewControl; asyncStatus: AsyncStatus); safecall;
      end;
    
      IAsyncOperation_1__IWebViewControl = interface(IInspectable)
      ['{ac3d28ac-8362-51c6-b2cc-16f3672758f1}']
        procedure put_Completed(handler: IAsyncOperationCompletedHandler_1__IWebViewControl); safecall;
        function get_Completed: IAsyncOperationCompletedHandler_1__IWebViewControl; safecall;
        function GetResults: IWebViewControl; safecall;
        property Completed: IAsyncOperationCompletedHandler_1__IWebViewControl read get_Completed write put_Completed;
      end;
    
      TWebViewControlCompleted = procedure(asyncInfo: IAsyncOperation_1__IWebViewControl; aasyncStatus: AsyncStatus) of object;
    
      TWebViewControlCompletedHandler = class(TInspectableObject,
          IAsyncOperationCompletedHandler_1__IWebViewControl
          )
      private
        FEvent: TWebViewControlCompleted;
      public
        procedure Invoke(asyncInfo: IAsyncOperation_1__IWebViewControl; aasyncStatus: AsyncStatus); safecall;
        constructor Create(AEvent: TWebViewControlCompleted);
      end;
    
       // The interface for interacting with the process hosting the web view control
       // https://docs.microsoft.com/en-us/uwp/api/windows.web.ui.interop.webviewcontrolprocess
      [WinRTClassNameAttribute(SWebViewControlProcess)]
      IWebViewControlProcess = interface(IInspectable)
      ['{02C723EC-98D6-424A-B63E-C6136C36A0F2}']
        function get_ProcessId: Cardinal; safecall;
        function get_EnterpriseId: HSTRING; safecall;
        function get_IsPrivateNetworkClientServerCapabilityEnabled: Boolean; safecall;
        function CreateWebViewControlAsync(hostWindowHandle: Int64; bounds: TRectF): IAsyncOperation_1__IWebViewControl; safecall;
        procedure Placeholder_GetWebViewControls; safecall;
        procedure Terminate; safecall;
    
        property ProcessId: Cardinal read get_ProcessId;
        property EnterpriseId: HSTRING read get_EnterpriseId;
        property IsPrivateNetworkClientServerCapabilityEnabled: Boolean read get_IsPrivateNetworkClientServerCapabilityEnabled;
    
        // TODO:
        //[eventadd] HRESULT ProcessExited([in] Windows.Foundation.TypedEventHandler<Windows.Web.UI.Interop.WebViewControlProcess*, IInspectable*>* handler, [out] [retval] EventRegistrationToken* token);
        //[eventremove] HRESULT ProcessExited([in] EventRegistrationToken token);
      end;
    
      // The CoClass to create an IWebViewControlProcess instance
      TWebViewControlProcess = class(TWinRTGenericImportI<IWebViewControlProcess>)
      end;
    
    type
    
      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
        procedure FormResize(Sender: TObject);
      private
        { Private declarations }
        FProcess: IWebViewControlProcess;
        FBrowser: IWebViewControl;
        FBrowserSite: IWebViewControlSite;
        procedure WebViewCompleted(asyncInfo: IAsyncOperation_1__IWebViewControl; aasyncStatus: AsyncStatus);
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    procedure TForm1.FormCreate(Sender: TObject);
    var
      Rect: TRectF;
      AsyncOperation: IAsyncOperation_1__IWebViewControl;
        CompletedHandler: IAsyncOperationCompletedHandler_1__IWebViewControl;
    begin
      CompletedHandler:=TWebViewControlCompletedHandler.Create(WebViewCompleted);
    
      // Size for browser
      Rect:= TRectF.Create(0, 0, ClientWidth, ClientHeight);
    
      // Create hosting process
      FProcess:= TWebViewControlProcess.Create();
    
      // Create WebView Control
      AsyncOperation:= FProcess.CreateWebViewControlAsync(self.Handle, Rect);
    
      // We will get notified when the control creation is finished
      AsyncOperation.Completed:= CompletedHandler;
    end;
    
    procedure TForm1.FormDestroy(Sender: TObject);
    begin
      // If there is a hosting process, then terminate it
      if Assigned(FProcess) then
      begin
        FProcess.Terminate;
      end;
    end;
    
    procedure TForm1.FormResize(Sender: TObject);
    begin
      if Assigned(FBrowserSite) then
      begin
        FBrowserSite.Bounds := TRectF.Create(0,0,ClientWidth, ClientHeight);
      end;
    end;
    
    procedure TForm1.WebViewCompleted(
      asyncInfo: IAsyncOperation_1__IWebViewControl;
      aasyncStatus: AsyncStatus);
    var
      WinS: TWindowsString;
      Uri: IUriRuntimeClass;
    begin
      // Initializing the WebView control was successful
    
      // Remember reference to control
      FBrowser:= asyncInfo.GetResults();
      FBrowserSite := FBrowser as IWebViewControlSite;
    
      // Load web page into control
      WinS:= TWindowsString.Create('http://www.whatismybrowser.com');
      Uri:= TUri.CreateUri(WinS);
      FBrowser.Navigate(Uri);
    end;
    
    { TWebViewControlCompletedHandler }
    
    constructor TWebViewControlCompletedHandler.Create(
      AEvent: TWebViewControlCompleted);
    begin
      FEvent := AEvent;
    end;
    
    procedure TWebViewControlCompletedHandler.Invoke(
      asyncInfo: IAsyncOperation_1__IWebViewControl;
      aasyncStatus: AsyncStatus);
    begin
      FEvent(asyncInfo, aasyncStatus);
    end;
    
    end.
    

    enter image description here

        3
  •  -1
  •   Zhi Lv    6 年前

    据我所知,我们现在无法从C++访问EdgeHtml,有人已经在USER语音网站上提交了一个建议。我建议你投赞成票。 Expose EdgeHTML C++ API