Delphi 12.3调用Chrome/edge内核实现DEMO源码

发布于:2025-05-15 ⋅ 阅读:(13) ⋅ 点赞:(0)

DELPHI使用调用Chrome/Edge内核浏览器,虽然旧的WebBrowser也还可以用,但大势所趋,新版的已经不需要使用第三方的组件了,算是全内置的开发了,不废话

Unit1 源码 + Form 源码

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Winapi.WebView2, Winapi.ActiveX,
  Vcl.Buttons, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Edge, Vcl.OleCtrls, SHDocVw;

type
  TForm1 = class(TForm)
    EdgeBrowser1: TEdgeBrowser;
    Panel1: TPanel;
    Edit1: TEdit;
    SpeedButton1: TSpeedButton;
    procedure Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure SpeedButton1Click(Sender: TObject);
    procedure EdgeBrowser1DocumentTitleChanged(Sender: TCustomEdgeBrowser;
      const ADocumentTitle: string);
    procedure EdgeBrowser1NavigationCompleted(Sender: TCustomEdgeBrowser;
      IsSuccess: Boolean; WebErrorStatus: COREWEBVIEW2_WEB_ERROR_STATUS);
    procedure EdgeBrowser1NavigationStarting(Sender: TCustomEdgeBrowser;
      Args: TNavigationStartingEventArgs);
  private
    procedure navigateto(url: String);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.navigateto(url: String);
begin
  if not url.Contains('://') then
    url := 'http://' + url;
  EdgeBrowser1.Navigate(url);
end;


procedure TForm1.EdgeBrowser1DocumentTitleChanged(Sender: TCustomEdgeBrowser;
  const ADocumentTitle: string);
begin
  Caption := 'Delphi 12.3 EdgeBrowser Demo (13316098767@qq.com)  ' + EdgeBrowser1.DocumentTitle ;
end;

procedure TForm1.EdgeBrowser1NavigationCompleted(Sender: TCustomEdgeBrowser;
  IsSuccess: Boolean; WebErrorStatus: COREWEBVIEW2_WEB_ERROR_STATUS);
begin

  edit1.text := EdgeBrowser1.LocationURL;
end;

procedure TForm1.EdgeBrowser1NavigationStarting(Sender: TCustomEdgeBrowser;
  Args: TNavigationStartingEventArgs);
var
  uri : PWideChar;
begin
  Args.ArgsInterface.Get_uri(uri);
  edit1.text := uri;
end;

procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key = vk_return then
  begin
    navigateto(edit1.Text);
  end;
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
    navigateto(edit1.Text);
end;

end.
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Delphi 12.3 EdgeBrowser Demo Test (13316098767@qq.com)'
  ClientHeight = 509
  ClientWidth = 899
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -12
  Font.Name = 'Segoe UI'
  Font.Style = []
  TextHeight = 15
  object EdgeBrowser1: TEdgeBrowser
    Left = 0
    Top = 44
    Width = 899
    Height = 465
    Align = alClient
    TabOrder = 0
    AllowSingleSignOnUsingOSPrimaryAccount = False
    TargetCompatibleBrowserVersion = '117.0.2045.28'
    UserDataFolder = '%LOCALAPPDATA%\bds.exe.WebView2'
    OnDocumentTitleChanged = EdgeBrowser1DocumentTitleChanged
    OnNavigationStarting = EdgeBrowser1NavigationStarting
    OnNavigationCompleted = EdgeBrowser1NavigationCompleted
  end
  object Panel1: TPanel
    Left = 0
    Top = 0
    Width = 899
    Height = 44
    Align = alTop
    BevelOuter = bvNone
    Caption = 'Panel1'
    TabOrder = 1
    object SpeedButton1: TSpeedButton
      Left = 855
      Top = 0
      Width = 44
      Height = 44
      Align = alRight
      Caption = '&G'
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -24
      Font.Name = 'Segoe UI'
      Font.Style = []
      ParentFont = False
      OnClick = SpeedButton1Click
      ExplicitHeight = 41
    end
    object Edit1: TEdit
      Left = 0
      Top = 0
      Width = 855
      Height = 44
      Align = alClient
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -20
      Font.Name = 'Segoe UI'
      Font.Style = []
      ParentFont = False
      TabOrder = 0
      Text = 'http://www.google.com'
      OnKeyDown = Edit1KeyDown
      ExplicitHeight = 36
    end
  end
end

发布是记得带上这个对应版本的DLL就好了

这个是csdn的效果

有疑问建议:+v 13316098767