2018-09-16 10:39:41 +02:00
|
|
|
unit uCEFWinControl;
|
|
|
|
|
2021-02-25 15:10:58 +01:00
|
|
|
{$I cef.inc}
|
|
|
|
|
2018-09-16 10:39:41 +02:00
|
|
|
{$IFDEF FPC}
|
|
|
|
{$MODE OBJFPC}{$H+}
|
2021-02-25 15:10:58 +01:00
|
|
|
{$IFDEF MACOSX}
|
|
|
|
{$ModeSwitch objectivec1}
|
|
|
|
{$ENDIF}
|
2018-09-16 10:39:41 +02:00
|
|
|
{$ENDIF}
|
|
|
|
|
2022-02-19 18:56:41 +01:00
|
|
|
{$IFNDEF TARGET_64BITS}{$ALIGN ON}{$ENDIF}
|
2018-11-22 15:02:21 +01:00
|
|
|
{$MINENUMSIZE 4}
|
2018-09-16 10:39:41 +02:00
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
{$IFDEF DELPHI16_UP}
|
|
|
|
{$IFDEF MSWINDOWS}WinApi.Windows, {$ENDIF} System.Classes, Vcl.Controls, Vcl.Graphics,
|
|
|
|
{$ELSE}
|
|
|
|
{$IFDEF MSWINDOWS}Windows,{$ENDIF} Classes, Forms, Controls, Graphics,
|
|
|
|
{$IFDEF FPC}
|
|
|
|
LCLProc, LCLType, LCLIntf, LResources, InterfaceBase,
|
|
|
|
{$ENDIF}
|
|
|
|
{$ENDIF}
|
2021-02-25 15:10:58 +01:00
|
|
|
{$IFDEF FPC}{$IFDEF MACOSX}
|
|
|
|
CocoaAll,
|
|
|
|
{$ENDIF}{$ENDIF}
|
2018-09-16 10:39:41 +02:00
|
|
|
uCEFTypes, uCEFInterfaces;
|
|
|
|
|
|
|
|
type
|
2023-09-24 11:21:05 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Custom TWinControl used by CEF browsers.
|
|
|
|
/// </summary>
|
2018-09-16 10:39:41 +02:00
|
|
|
TCEFWinControl = class(TWinControl)
|
|
|
|
protected
|
2021-02-21 18:49:04 +01:00
|
|
|
function GetChildWindowHandle : {$IFNDEF MSWINDOWS}{$IFDEF FPC}LclType.{$ENDIF}{$ENDIF}THandle; virtual;
|
2018-09-16 10:39:41 +02:00
|
|
|
procedure Resize; override;
|
|
|
|
|
|
|
|
public
|
2023-09-24 11:21:05 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Take a snapshot of the browser contents into aBitmap. This function only works in Windows without hardware acceleration.
|
|
|
|
/// </summary>
|
2019-03-15 17:17:14 +01:00
|
|
|
function TakeSnapshot(var aBitmap : TBitmap) : boolean;
|
2023-09-24 11:21:05 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Destroy the child windows created by the browser.
|
|
|
|
/// </summary>
|
2019-03-15 17:17:14 +01:00
|
|
|
function DestroyChildWindow : boolean;
|
2023-09-24 11:21:05 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Exposes the CreateHandle procedure to create the Handle at any time.
|
|
|
|
/// </summary>
|
2019-03-15 17:17:14 +01:00
|
|
|
procedure CreateHandle; override;
|
2023-09-24 11:21:05 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Invalidate the child windows created by the browser.
|
|
|
|
/// </summary>
|
2019-03-15 17:17:14 +01:00
|
|
|
procedure InvalidateChildren;
|
2023-09-24 11:21:05 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Updates the size of the child windows created by the browser.
|
|
|
|
/// </summary>
|
2020-12-30 19:40:07 +01:00
|
|
|
procedure UpdateSize; virtual;
|
2023-09-24 11:21:05 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Handle of the first child window created by the browser.
|
|
|
|
/// </summary>
|
2019-03-15 17:17:14 +01:00
|
|
|
property ChildWindowHandle : THandle read GetChildWindowHandle;
|
2018-09-16 10:39:41 +02:00
|
|
|
|
|
|
|
published
|
|
|
|
property Align;
|
|
|
|
property Anchors;
|
|
|
|
property Color;
|
|
|
|
property Constraints;
|
|
|
|
property TabStop;
|
|
|
|
property TabOrder;
|
|
|
|
property Visible;
|
|
|
|
property Enabled;
|
|
|
|
property ShowHint;
|
|
|
|
property Hint;
|
2021-01-16 14:35:01 +01:00
|
|
|
property DragKind;
|
|
|
|
property DragCursor;
|
|
|
|
property DragMode;
|
2020-12-30 19:40:07 +01:00
|
|
|
property OnResize;
|
|
|
|
property OnEnter;
|
|
|
|
property OnExit;
|
2021-01-16 14:35:01 +01:00
|
|
|
property OnDragDrop;
|
|
|
|
property OnDragOver;
|
|
|
|
property OnStartDrag;
|
|
|
|
property OnEndDrag;
|
2022-10-14 16:35:50 +02:00
|
|
|
{$IFNDEF FPC}
|
|
|
|
property OnCanResize;
|
|
|
|
{$ENDIF}
|
2020-02-01 19:14:01 +01:00
|
|
|
{$IFDEF DELPHI14_UP}
|
|
|
|
property Touch;
|
|
|
|
property OnGesture;
|
|
|
|
{$ENDIF}
|
2018-09-16 10:39:41 +02:00
|
|
|
property DoubleBuffered;
|
|
|
|
{$IFDEF DELPHI12_UP}
|
|
|
|
property ParentDoubleBuffered;
|
|
|
|
{$ENDIF}
|
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
|
|
|
uCEFMiscFunctions, uCEFClient, uCEFConstants;
|
|
|
|
|
2021-02-21 18:49:04 +01:00
|
|
|
function TCEFWinControl.GetChildWindowHandle : {$IFNDEF MSWINDOWS}{$IFDEF FPC}LclType.{$ENDIF}{$ENDIF}THandle;
|
2018-09-16 10:39:41 +02:00
|
|
|
begin
|
2019-01-08 19:15:25 +01:00
|
|
|
{$IFDEF MSWINDOWS}
|
2018-09-16 10:39:41 +02:00
|
|
|
if not(csDesigning in ComponentState) and HandleAllocated then
|
|
|
|
Result := GetWindow(Handle, GW_CHILD)
|
|
|
|
else
|
2019-01-08 19:15:25 +01:00
|
|
|
{$ENDIF}
|
2018-09-16 10:39:41 +02:00
|
|
|
Result := 0;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCEFWinControl.CreateHandle;
|
|
|
|
begin
|
|
|
|
inherited CreateHandle;
|
|
|
|
end;
|
|
|
|
|
2018-10-20 20:43:06 +02:00
|
|
|
procedure TCEFWinControl.InvalidateChildren;
|
|
|
|
begin
|
2021-11-09 15:00:17 +01:00
|
|
|
if HandleAllocated then
|
|
|
|
RedrawWindow(Handle, nil, 0, RDW_INVALIDATE or RDW_ALLCHILDREN);
|
2018-10-20 20:43:06 +02:00
|
|
|
end;
|
|
|
|
|
2018-09-16 10:39:41 +02:00
|
|
|
procedure TCEFWinControl.UpdateSize;
|
2020-12-30 19:40:07 +01:00
|
|
|
{$IFDEF MSWINDOWS}
|
2018-09-16 10:39:41 +02:00
|
|
|
var
|
|
|
|
TempRect : TRect;
|
|
|
|
TempHWND : THandle;
|
2020-12-30 19:40:07 +01:00
|
|
|
{$ENDIF}
|
2018-09-16 10:39:41 +02:00
|
|
|
begin
|
2020-12-30 19:40:07 +01:00
|
|
|
{$IFDEF MSWINDOWS}
|
2018-09-16 10:39:41 +02:00
|
|
|
TempHWND := ChildWindowHandle;
|
|
|
|
if (TempHWND = 0) then exit;
|
|
|
|
|
|
|
|
TempRect := GetClientRect;
|
|
|
|
|
|
|
|
SetWindowPos(TempHWND, 0,
|
|
|
|
0, 0, TempRect.right, TempRect.bottom,
|
|
|
|
SWP_NOZORDER);
|
2020-12-30 19:40:07 +01:00
|
|
|
{$ENDIF}
|
2018-09-16 10:39:41 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TCEFWinControl.TakeSnapshot(var aBitmap : TBitmap) : boolean;
|
2019-11-10 18:23:39 +01:00
|
|
|
{$IFDEF MSWINDOWS}
|
2018-09-16 10:39:41 +02:00
|
|
|
var
|
|
|
|
TempHWND : HWND;
|
|
|
|
TempDC : HDC;
|
|
|
|
TempRect : TRect;
|
|
|
|
TempWidth : Integer;
|
|
|
|
TempHeight : Integer;
|
2019-11-10 18:23:39 +01:00
|
|
|
{$ENDIF}
|
2018-09-16 10:39:41 +02:00
|
|
|
begin
|
|
|
|
Result := False;
|
2019-11-10 18:23:39 +01:00
|
|
|
{$IFDEF MSWINDOWS}
|
2018-09-16 10:39:41 +02:00
|
|
|
|
|
|
|
TempHWND := ChildWindowHandle;
|
|
|
|
if (TempHWND = 0) then exit;
|
|
|
|
{$IFDEF DELPHI16_UP}Winapi.{$ENDIF}Windows.GetClientRect(TempHWND, TempRect);
|
|
|
|
TempDC := GetDC(TempHWND);
|
|
|
|
TempWidth := TempRect.Right - TempRect.Left;
|
|
|
|
TempHeight := TempRect.Bottom - TempRect.Top;
|
|
|
|
|
2023-03-10 09:58:27 +01:00
|
|
|
if (aBitmap = nil) then
|
|
|
|
aBitmap := TBitmap.Create;
|
|
|
|
|
2018-09-16 10:39:41 +02:00
|
|
|
aBitmap.Height := TempHeight;
|
|
|
|
aBitmap.Width := TempWidth;
|
|
|
|
|
|
|
|
Result := BitBlt(aBitmap.Canvas.Handle, 0, 0, TempWidth, TempHeight,
|
|
|
|
TempDC, 0, 0, SRCCOPY);
|
|
|
|
|
|
|
|
ReleaseDC(TempHWND, TempDC);
|
2019-01-08 19:15:25 +01:00
|
|
|
{$ENDIF}
|
2018-09-16 10:39:41 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TCEFWinControl.DestroyChildWindow : boolean;
|
2019-11-10 18:23:39 +01:00
|
|
|
{$IFDEF MSWINDOWS}
|
2018-09-16 10:39:41 +02:00
|
|
|
var
|
|
|
|
TempHWND : HWND;
|
2019-11-10 18:23:39 +01:00
|
|
|
{$ENDIF}
|
2021-02-25 15:10:58 +01:00
|
|
|
{$IFDEF FPC}{$IFDEF MACOSX}
|
|
|
|
var
|
|
|
|
ViewObj: NSObject;
|
|
|
|
{$ENDIF}{$ENDIF}
|
2018-09-16 10:39:41 +02:00
|
|
|
begin
|
2019-01-08 19:15:25 +01:00
|
|
|
{$IFDEF MSWINDOWS}
|
2018-09-16 10:39:41 +02:00
|
|
|
TempHWND := ChildWindowHandle;
|
|
|
|
Result := (TempHWND <> 0) and DestroyWindow(TempHWND);
|
2019-11-10 18:23:39 +01:00
|
|
|
{$ELSE}
|
|
|
|
Result := False;
|
2021-02-25 15:10:58 +01:00
|
|
|
{$IFDEF FPC}{$IFDEF MACOSX}
|
|
|
|
ViewObj := NSObject(ChildWindowHandle);
|
|
|
|
if ViewObj <> nil then begin
|
|
|
|
if ViewObj.isKindOfClass_(nsview) then begin
|
|
|
|
NSView(ViewObj).removeFromSuperview;
|
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
{$ENDIF}{$ENDIF}
|
2019-01-08 19:15:25 +01:00
|
|
|
{$ENDIF}
|
2018-09-16 10:39:41 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCEFWinControl.Resize;
|
|
|
|
begin
|
|
|
|
inherited Resize;
|
|
|
|
|
|
|
|
UpdateSize;
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|