CEF4Delphi/demos/DOMVisitor/uDOMVisitor.pas
2017-08-23 09:50:38 +02:00

197 lines
6.3 KiB
ObjectPascal

// ************************************************************************
// ***************************** CEF4Delphi *******************************
// ************************************************************************
//
// CEF4Delphi is based on DCEF3 which uses CEF3 to embed a chromium-based
// browser in Delphi applications.
//
// The original license of DCEF3 still applies to CEF4Delphi.
//
// For more information about CEF4Delphi visit :
// https://www.briskbard.com/index.php?lang=en&pageid=cef
//
// Copyright © 2017 Salvador Díaz Fau. All rights reserved.
//
// ************************************************************************
// ************ vvvv Original license and comments below vvvv *************
// ************************************************************************
(*
* Delphi Chromium Embedded 3
*
* Usage allowed under the restrictions of the Lesser GNU General Public License
* or alternatively the restrictions of the Mozilla Public License 1.1
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* Unit owner : Henri Gourvest <hgourvest@gmail.com>
* Web site : http://www.progdigy.com
* Repository : http://code.google.com/p/delphichromiumembedded/
* Group : http://groups.google.com/group/delphichromiumembedded
*
* Embarcadero Technologies, Inc is not permitted to use or redistribute
* this source code without explicit permission.
*
*)
unit uDOMVisitor;
{$I cef.inc}
interface
uses
{$IFDEF DELPHI16_UP}
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Menus,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, System.Types, Vcl.ComCtrls, Vcl.ClipBrd,
System.UITypes,
{$ELSE}
Windows, Messages, SysUtils, Variants, Classes, Graphics, Menus,
Controls, Forms, Dialogs, StdCtrls, ExtCtrls, Types, ComCtrls, ClipBrd,
{$ENDIF}
uCEFChromium, uCEFWindowParent, uCEFInterfaces, uCEFApplication, uCEFTypes, uCEFConstants;
const
MINIBROWSER_CREATED = WM_APP + $100;
MINIBROWSER_VISITDOM = WM_APP + $101;
MINIBROWSER_CONTEXTMENU_VISITDOM = MENU_ID_USER_FIRST + 1;
DOMVISITOR_MSGNAME = 'domvisitor';
RETRIEVEDOM_MSGNAME = 'retrievedom';
type
TDOMVisitorFrm = class(TForm)
CEFWindowParent1: TCEFWindowParent;
Chromium1: TChromium;
AddressBarPnl: TPanel;
GoBtn: TButton;
AddressEdt: TEdit;
StatusBar1: TStatusBar;
procedure GoBtnClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure Chromium1AfterCreated(Sender: TObject;
const browser: ICefBrowser);
procedure Chromium1BeforeContextMenu(Sender: TObject;
const browser: ICefBrowser; const frame: ICefFrame;
const params: ICefContextMenuParams; const model: ICefMenuModel);
procedure Chromium1ContextMenuCommand(Sender: TObject;
const browser: ICefBrowser; const frame: ICefFrame;
const params: ICefContextMenuParams; commandId: Integer;
eventFlags: Cardinal; out Result: Boolean);
procedure Chromium1ProcessMessageReceived(Sender: TObject;
const browser: ICefBrowser; sourceProcess: TCefProcessId;
const message: ICefProcessMessage; out Result: Boolean);
private
{ Private declarations }
protected
procedure BrowserCreatedMsg(var aMessage : TMessage); message MINIBROWSER_CREATED;
procedure VisitDOMMsg(var aMessage : TMessage); message MINIBROWSER_VISITDOM;
procedure WMMove(var aMessage : TWMMove); message WM_MOVE;
procedure WMMoving(var aMessage : TMessage); message WM_MOVING;
procedure ShowStatusText(const aText : string);
public
{ Public declarations }
end;
var
DOMVisitorFrm: TDOMVisitorFrm;
implementation
{$R *.dfm}
uses
uCEFProcessMessage;
procedure TDOMVisitorFrm.Chromium1AfterCreated(Sender: TObject;
const browser: ICefBrowser);
begin
PostMessage(Handle, MINIBROWSER_CREATED, 0, 0);
end;
procedure TDOMVisitorFrm.Chromium1BeforeContextMenu(Sender: TObject;
const browser: ICefBrowser; const frame: ICefFrame;
const params: ICefContextMenuParams; const model: ICefMenuModel);
begin
model.AddItem(MINIBROWSER_CONTEXTMENU_VISITDOM, 'Visit DOM in CEF');
end;
procedure TDOMVisitorFrm.Chromium1ContextMenuCommand(Sender: TObject;
const browser: ICefBrowser; const frame: ICefFrame;
const params: ICefContextMenuParams; commandId: Integer;
eventFlags: Cardinal; out Result: Boolean);
begin
Result := False;
case commandId of
MINIBROWSER_CONTEXTMENU_VISITDOM :
PostMessage(Handle, MINIBROWSER_VISITDOM, 0, 0);
end;
end;
procedure TDOMVisitorFrm.Chromium1ProcessMessageReceived(Sender: TObject;
const browser: ICefBrowser; sourceProcess: TCefProcessId;
const message: ICefProcessMessage; out Result: Boolean);
begin
if (message = nil) or (message.ArgumentList = nil) then exit;
if (message.Name = DOMVISITOR_MSGNAME) then
begin
// Message received from the DOMVISITOR in CEF
ShowStatusText('DOM Visitor result text : ' + message.ArgumentList.GetString(0));
Result := True;
end
else
Result := False;
end;
procedure TDOMVisitorFrm.FormShow(Sender: TObject);
begin
Chromium1.CreateBrowser(CEFWindowParent1, '');
end;
procedure TDOMVisitorFrm.GoBtnClick(Sender: TObject);
begin
Chromium1.LoadURL(AddressEdt.Text);
end;
procedure TDOMVisitorFrm.BrowserCreatedMsg(var aMessage : TMessage);
begin
AddressBarPnl.Enabled := True;
GoBtn.Click;
end;
procedure TDOMVisitorFrm.VisitDOMMsg(var aMessage : TMessage);
var
TempMsg : ICefProcessMessage;
begin
// Only works using a TCefCustomRenderProcessHandler.
// Use the ArgumentList property if you need to pass some parameters.
TempMsg := TCefProcessMessageRef.New(RETRIEVEDOM_MSGNAME); // Same name than TCefCustomRenderProcessHandler.MessageName
Chromium1.SendProcessMessage(PID_RENDERER, TempMsg);
end;
procedure TDOMVisitorFrm.WMMove(var aMessage : TWMMove);
begin
inherited;
if (Chromium1 <> nil) then Chromium1.NotifyMoveOrResizeStarted;
end;
procedure TDOMVisitorFrm.WMMoving(var aMessage : TMessage);
begin
inherited;
if (Chromium1 <> nil) then Chromium1.NotifyMoveOrResizeStarted;
end;
procedure TDOMVisitorFrm.ShowStatusText(const aText : string);
begin
StatusBar1.Panels[0].Text := aText;
end;
end.