You've already forked CEF4Delphi
mirror of
https://github.com/salvadordf/CEF4Delphi.git
synced 2025-06-12 22:07:39 +02:00
Added ConsoleBrowser2 and WebpageSnapshot demos
- Split the Lazarus demos directory into "Lazarus_Windows" and "Lazarus_Linux". - Added OSRExternalPumpBrowser and TinyBrowser2 demos to "Lazarus_Linux". - Set TBufferPanel.GetScreenScale as virtual. - Added DevTools to the SchemeRegistrationBrowser demo. - Modified the SchemeRegistrationBrowser demo to receive XMLHttpRequest requests from JavaScript.
This commit is contained in:
217
demos/Lazarus_Windows/ToolBoxBrowser2/uMainForm.pas
Normal file
217
demos/Lazarus_Windows/ToolBoxBrowser2/uMainForm.pas
Normal file
@ -0,0 +1,217 @@
|
||||
unit uMainForm;
|
||||
|
||||
{$MODE Delphi}
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
{$IFDEF DELPHI16_UP}
|
||||
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
|
||||
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.ComCtrls,
|
||||
{$ELSE}
|
||||
LCLIntf, LCLType, LMessages, Messages, SysUtils, Variants, Classes, Graphics,
|
||||
Controls, Forms, Dialogs, StdCtrls, ExtCtrls, ComCtrls,
|
||||
{$ENDIF}
|
||||
uCEFInterfaces, uCEFTypes, uCEFConstants, uCEFViewComponent,
|
||||
uCEFPanelComponent, uCEFWindowComponent,
|
||||
uCEFBrowserViewComponent, uCEFChromiumCore, uCEFChromium;
|
||||
|
||||
const
|
||||
CEFBROWSER_INITIALIZED = WM_APP + $100;
|
||||
|
||||
DEFAULT_WINDOW_VIEW_WIDTH = 800;
|
||||
DEFAULT_WINDOW_VIEW_HEIGHT = 600;
|
||||
|
||||
type
|
||||
TMainForm = class(TForm)
|
||||
ButtonPnl: TPanel;
|
||||
Edit1: TEdit;
|
||||
Button1: TButton;
|
||||
CEFWindowComponent1: TCEFWindowComponent;
|
||||
CEFBrowserViewComponent1: TCEFBrowserViewComponent;
|
||||
Chromium1: TChromium;
|
||||
|
||||
procedure FormShow(Sender: TObject);
|
||||
procedure Button1Click(Sender: TObject);
|
||||
|
||||
procedure CEFWindowComponent1GetPreferredSize(const Sender: TObject; const view: ICefView; var aResult: TCefSize);
|
||||
procedure CEFWindowComponent1WindowCreated(const Sender: TObject; const window: ICefWindow);
|
||||
procedure CEFWindowComponent1WindowDestroyed(const Sender: TObject; const window: ICefWindow);
|
||||
procedure CEFWindowComponent1CanClose(const Sender: TObject; const window: ICefWindow; var aResult: Boolean);
|
||||
|
||||
procedure Chromium1TitleChange(Sender: TObject; const browser: ICefBrowser; const title: ustring);
|
||||
procedure Chromium1BeforePopup(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; const targetUrl, targetFrameName: ustring; targetDisposition: TCefWindowOpenDisposition; userGesture: Boolean; const popupFeatures: TCefPopupFeatures; var windowInfo: TCefWindowInfo; var client: ICefClient; var settings: TCefBrowserSettings; var extra_info: ICefDictionaryValue; var noJavascriptAccess, Result: Boolean);
|
||||
|
||||
protected
|
||||
procedure CEFInitializedMsg(var aMessage : TMessage); message CEFBROWSER_INITIALIZED;
|
||||
|
||||
procedure EnableInterface;
|
||||
end;
|
||||
|
||||
var
|
||||
MainForm: TMainForm;
|
||||
|
||||
procedure CreateGlobalCEFApp;
|
||||
|
||||
implementation
|
||||
|
||||
{$R *.lfm}
|
||||
|
||||
uses
|
||||
uCEFApplication;
|
||||
|
||||
// This demo uses the Views Framework in CEF to create a child window with a
|
||||
// web browser.
|
||||
|
||||
// CEF4Delphi has several components to facilitate the creation of several views
|
||||
// and this demo uses these :
|
||||
|
||||
// TCEFWindowComponent : Has a ICefWindow and a ICefWindowDelegate to call
|
||||
// the methods of a "CefWindow" and the events from the "CefWindowDelegate".
|
||||
// Read the linked documents bellow for more details about them.
|
||||
|
||||
// TCEFBrowserViewComponent : Has a ICefBrowserView and a ICefBrowserViewDelegate
|
||||
// to call the methods of a "CefBrowserView" and the events from the
|
||||
// "CefBrowserViewDelegate".
|
||||
|
||||
// TChromium : Is used to create the browser and to handle all its events.
|
||||
|
||||
// All the views have a hierarchy and they inherit methods or events from their
|
||||
// parents. See the diagram in the uCEFTypes.pas file (line 2900 aprox.)
|
||||
|
||||
// This demo creates a window when the user clicks on the "Open" button.
|
||||
// Most of the methods in the Views Framework must be used in the CEF UI thread
|
||||
// but most of the procedures used to create the CEF4Delphi components create a
|
||||
// task if they are called in a different thread. The rest of the methods *MUST*
|
||||
// be called in the CEF UI thread or they won't work.
|
||||
|
||||
// TCEFWindowComponent.CreateTopLevelWindow triggers the
|
||||
// TCEFWindowComponent.OnWindowCreated event which is executed in the CEF UI
|
||||
// thread and you can use it to create the child views in the window, in this
|
||||
// case a browser view.
|
||||
|
||||
// TCEFWindowComponent.OnGetPreferredSize will alse be triggered when you create
|
||||
// the window to get the window size in DIPs.
|
||||
|
||||
// REFERENCES :
|
||||
// ------------
|
||||
// https://bitbucket.org/chromiumembedded/cef/issues/1749
|
||||
// https://www.chromium.org/developers/design-documents/chromeviews
|
||||
// https://magpcss.org/ceforum/apidocs3/projects/(default)/CefView.html
|
||||
// https://magpcss.org/ceforum/apidocs3/projects/(default)/CefViewDelegate.html
|
||||
// https://magpcss.org/ceforum/apidocs3/projects/(default)/CefWindow.html
|
||||
// https://magpcss.org/ceforum/apidocs3/projects/(default)/CefWindowDelegate.html
|
||||
// https://magpcss.org/ceforum/apidocs3/projects/(default)/CefBrowserView.html
|
||||
// https://magpcss.org/ceforum/apidocs3/projects/(default)/CefBrowserViewDelegate.html
|
||||
|
||||
procedure GlobalCEFApp_OnContextInitialized;
|
||||
begin
|
||||
if (MainForm <> nil) and MainForm.HandleAllocated then
|
||||
PostMessage(MainForm.Handle, CEFBROWSER_INITIALIZED, 0, 0);
|
||||
end;
|
||||
|
||||
procedure CreateGlobalCEFApp;
|
||||
begin
|
||||
GlobalCEFApp := TCefApplication.Create;
|
||||
GlobalCEFApp.OnContextInitialized := GlobalCEFApp_OnContextInitialized;
|
||||
end;
|
||||
|
||||
procedure TMainForm.Button1Click(Sender: TObject);
|
||||
begin
|
||||
CEFWindowComponent1.CreateTopLevelWindow;
|
||||
ButtonPnl.Enabled := False;
|
||||
end;
|
||||
|
||||
procedure TMainForm.CEFInitializedMsg(var aMessage : TMessage);
|
||||
begin
|
||||
EnableInterface;
|
||||
end;
|
||||
|
||||
procedure TMainForm.CEFWindowComponent1CanClose(const Sender: TObject;
|
||||
const window: ICefWindow; var aResult: Boolean);
|
||||
begin
|
||||
aResult := Chromium1.TryCloseBrowser;
|
||||
end;
|
||||
|
||||
procedure TMainForm.CEFWindowComponent1GetPreferredSize(const Sender: TObject;
|
||||
const view: ICefView; var aResult: TCefSize);
|
||||
begin
|
||||
// This is the initial window size
|
||||
aResult.width := DEFAULT_WINDOW_VIEW_WIDTH;
|
||||
aResult.height := DEFAULT_WINDOW_VIEW_HEIGHT;
|
||||
end;
|
||||
|
||||
procedure TMainForm.CEFWindowComponent1WindowCreated(const Sender: TObject;
|
||||
const window: ICefWindow);
|
||||
var
|
||||
TempURL : ustring;
|
||||
TempWorkArea : TCefRect;
|
||||
TempPosition : TCefPoint;
|
||||
TempDisplay : ICefDisplay;
|
||||
begin
|
||||
TempURL := trim(Edit1.Text);
|
||||
if (length(TempURL) = 0) then TempURL := 'about:blank';
|
||||
|
||||
// This event is executed in the CEF UI thread and we can call all these other
|
||||
// functions on this thread. In fact, all of these functions only work when
|
||||
// you call them on this thread.
|
||||
|
||||
if Chromium1.CreateBrowser(TempURL, CEFBrowserViewComponent1) then
|
||||
begin
|
||||
CEFWindowComponent1.AddChildView(CEFBrowserViewComponent1.BrowserView);
|
||||
CEFWindowComponent1.Show;
|
||||
|
||||
// This centers the window on the screen
|
||||
TempDisplay := CEFWindowComponent1.Display;
|
||||
if (TempDisplay <> nil) then
|
||||
begin
|
||||
TempWorkArea := TempDisplay.WorkArea;
|
||||
TempPosition.x := ((TempWorkArea.width - DEFAULT_WINDOW_VIEW_WIDTH) div 2) + TempWorkArea.x;
|
||||
TempPosition.y := ((TempWorkArea.height - DEFAULT_WINDOW_VIEW_HEIGHT) div 2) + TempWorkArea.y;
|
||||
|
||||
CEFWindowComponent1.Position := TempPosition;
|
||||
end;
|
||||
|
||||
CEFBrowserViewComponent1.RequestFocus;
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TMainForm.CEFWindowComponent1WindowDestroyed(const Sender: TObject;
|
||||
const window: ICefWindow);
|
||||
begin
|
||||
ButtonPnl.Enabled := True;
|
||||
end;
|
||||
|
||||
procedure TMainForm.Chromium1BeforePopup(Sender: TObject;
|
||||
const browser: ICefBrowser; const frame: ICefFrame; const targetUrl,
|
||||
targetFrameName: ustring; targetDisposition: TCefWindowOpenDisposition;
|
||||
userGesture: Boolean; const popupFeatures: TCefPopupFeatures;
|
||||
var windowInfo: TCefWindowInfo; var client: ICefClient;
|
||||
var settings: TCefBrowserSettings; var extra_info: ICefDictionaryValue;
|
||||
var noJavascriptAccess, Result: Boolean);
|
||||
begin
|
||||
// For simplicity, this demo blocks all popup windows and new tabs
|
||||
Result := (targetDisposition in [WOD_NEW_FOREGROUND_TAB, WOD_NEW_BACKGROUND_TAB, WOD_NEW_POPUP, WOD_NEW_WINDOW]);
|
||||
end;
|
||||
|
||||
procedure TMainForm.Chromium1TitleChange(Sender: TObject;
|
||||
const browser: ICefBrowser; const title: ustring);
|
||||
begin
|
||||
CEFWindowComponent1.Title := title;
|
||||
end;
|
||||
|
||||
procedure TMainForm.FormShow(Sender: TObject);
|
||||
begin
|
||||
if (GlobalCEFApp <> nil) and GlobalCEFApp.GlobalContextInitialized then
|
||||
EnableInterface;
|
||||
end;
|
||||
|
||||
procedure TMainForm.EnableInterface;
|
||||
begin
|
||||
Caption := 'ToolBox Browser 2';
|
||||
ButtonPnl.Enabled := True;
|
||||
cursor := crDefault;
|
||||
end;
|
||||
|
||||
end.
|
||||
|
Reference in New Issue
Block a user