2019-04-04 10:26:44 +02:00
|
|
|
unit uCEFFMXWindowParent;
|
2018-04-18 10:25:38 +02:00
|
|
|
|
|
|
|
{$I cef.inc}
|
|
|
|
|
2022-02-19 18:56:41 +01:00
|
|
|
{$IFNDEF TARGET_64BITS}{$ALIGN ON}{$ENDIF}
|
|
|
|
{$MINENUMSIZE 4}
|
|
|
|
|
2018-04-18 10:25:38 +02:00
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
System.Classes, System.Types, System.UITypes,
|
|
|
|
{$IFDEF MSWINDOWS}
|
|
|
|
WinApi.Windows,
|
|
|
|
{$ENDIF}
|
2022-10-14 16:35:50 +02:00
|
|
|
FMX.Controls, FMX.Types, FMX.Forms,
|
2023-01-01 18:17:22 +01:00
|
|
|
uCEFConstants, uCEFFMXChromium;
|
2018-04-18 10:25:38 +02:00
|
|
|
|
|
|
|
type
|
2022-10-14 16:35:50 +02:00
|
|
|
{$IFNDEF FPC}{$IFDEF DELPHI16_UP}[ComponentPlatformsAttribute(pfidWindows or pfidOSX or pfidLinux)]{$ENDIF}{$ENDIF}
|
2018-04-18 10:25:38 +02:00
|
|
|
TFMXWindowParent = class(TCommonCustomForm)
|
|
|
|
protected
|
|
|
|
{$IFDEF MSWINDOWS}
|
2023-01-01 18:17:22 +01:00
|
|
|
FChromium : TFMXChromium;
|
2018-04-18 10:25:38 +02:00
|
|
|
function GetChildWindowHandle : HWND;
|
|
|
|
{$ENDIF}
|
2023-01-01 18:17:22 +01:00
|
|
|
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
|
2022-12-09 10:32:46 +01:00
|
|
|
{$IFDEF DELPHI17_UP}
|
2018-04-18 10:25:38 +02:00
|
|
|
procedure Resize; override;
|
2022-12-09 10:32:46 +01:00
|
|
|
{$ENDIF}
|
2018-04-18 10:25:38 +02:00
|
|
|
public
|
|
|
|
{$IFDEF MSWINDOWS}
|
2023-09-24 11:21:05 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Set a new parent for this control.
|
|
|
|
/// </summary>
|
2021-12-08 17:45:03 +01:00
|
|
|
procedure Reparent(const aNewParentHandle : {$IFDEF DELPHI18_UP}TWindowHandle{$ELSE}TFmxHandle{$ENDIF});
|
2023-09-24 11:21:05 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Updates the size of the child windows created by the browser.
|
|
|
|
/// </summary>
|
2023-01-01 18:17:22 +01:00
|
|
|
procedure UpdateSize;
|
2023-09-24 11:21:05 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Handle of the first child window created by the browser.
|
|
|
|
/// </summary>
|
2023-01-01 18:17:22 +01:00
|
|
|
property ChildWindowHandle : HWND read GetChildWindowHandle;
|
2023-09-24 11:21:05 +02:00
|
|
|
/// <summary>
|
|
|
|
/// TChromium instance used by this component.
|
|
|
|
/// </summary>
|
2023-01-01 18:17:22 +01:00
|
|
|
property Chromium : TFMXChromium read FChromium write FChromium;
|
2018-04-18 10:25:38 +02:00
|
|
|
{$ENDIF}
|
2022-12-11 17:15:20 +01:00
|
|
|
{$IFNDEF DELPHI17_UP}
|
|
|
|
procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer); override;
|
|
|
|
{$ENDIF}
|
2018-04-18 10:25:38 +02:00
|
|
|
|
2023-01-01 18:17:22 +01:00
|
|
|
|
2018-04-18 10:25:38 +02:00
|
|
|
published
|
|
|
|
property Visible;
|
|
|
|
property Height;
|
|
|
|
property Width;
|
2022-12-11 17:15:20 +01:00
|
|
|
{$IFDEF DELPHI17_UP}
|
2020-02-03 12:02:30 +01:00
|
|
|
property Touch;
|
|
|
|
property OnGesture;
|
2022-12-11 17:15:20 +01:00
|
|
|
{$ENDIF}
|
2018-04-18 10:25:38 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
// This class inherits from TCommonCustomForm because CEF needs a Windows handle
|
|
|
|
// to create the browser in normal mode.
|
|
|
|
|
|
|
|
// TFMXWindowParent has to be created and resized at runtime.
|
|
|
|
// It's also necessary to call "Reparent" to add this component as a child component to your form.
|
|
|
|
|
|
|
|
uses
|
2023-01-01 18:17:22 +01:00
|
|
|
System.SysUtils, FMX.Platform, FMX.Platform.Win,
|
|
|
|
uCEFApplicationCore;
|
2018-04-18 10:25:38 +02:00
|
|
|
|
2022-12-09 10:32:46 +01:00
|
|
|
{$IFDEF DELPHI17_UP}
|
2018-04-18 10:25:38 +02:00
|
|
|
procedure TFMXWindowParent.Resize;
|
|
|
|
begin
|
|
|
|
inherited Resize;
|
|
|
|
|
|
|
|
{$IFDEF MSWINDOWS}
|
|
|
|
UpdateSize;
|
|
|
|
{$ENDIF}
|
|
|
|
end;
|
2022-12-11 17:15:20 +01:00
|
|
|
{$ELSE}
|
|
|
|
procedure TFMXWindowParent.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
|
|
|
|
begin
|
|
|
|
inherited SetBounds(ALeft, ATop, AWidth, AHeight);
|
|
|
|
|
|
|
|
{$IFDEF MSWINDOWS}
|
|
|
|
UpdateSize;
|
|
|
|
{$ENDIF}
|
|
|
|
end;
|
2022-12-09 10:32:46 +01:00
|
|
|
{$ENDIF}
|
2018-04-18 10:25:38 +02:00
|
|
|
|
|
|
|
{$IFDEF MSWINDOWS}
|
|
|
|
function TFMXWindowParent.GetChildWindowHandle : HWND;
|
|
|
|
var
|
|
|
|
TempHWND : HWND;
|
|
|
|
begin
|
|
|
|
TempHWND := FmxHandleToHWND(Handle);
|
|
|
|
Result := WinApi.Windows.GetWindow(TempHWND, GW_CHILD);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TFMXWindowParent.UpdateSize;
|
|
|
|
var
|
|
|
|
TempHWND, TempChildHWND : HWND;
|
|
|
|
TempRect : System.Types.TRect;
|
|
|
|
TempClientRect : TRectF;
|
2023-01-01 18:17:22 +01:00
|
|
|
TempScale : single;
|
2018-04-18 10:25:38 +02:00
|
|
|
begin
|
|
|
|
TempChildHWND := ChildWindowHandle;
|
|
|
|
if (TempChildHWND = 0) then exit;
|
|
|
|
|
|
|
|
TempHWND := BeginDeferWindowPos(1);
|
|
|
|
|
|
|
|
try
|
2023-01-01 18:17:22 +01:00
|
|
|
if assigned(FChromium) then
|
|
|
|
TempScale := FChromium.ScreenScale
|
|
|
|
else
|
|
|
|
TempScale := GlobalCEFApp.DeviceScaleFactor;
|
|
|
|
|
2018-04-18 10:25:38 +02:00
|
|
|
TempClientRect := ClientRect;
|
|
|
|
TempRect.Left := round(TempClientRect.Left);
|
|
|
|
TempRect.Top := round(TempClientRect.Top);
|
2023-01-01 18:17:22 +01:00
|
|
|
TempRect.Right := round(TempClientRect.Right * TempScale);
|
|
|
|
TempRect.Bottom := round(TempClientRect.Bottom * TempScale);
|
2018-04-18 10:25:38 +02:00
|
|
|
|
|
|
|
TempHWND := DeferWindowPos(TempHWND, TempChildHWND, HWND_TOP,
|
|
|
|
TempRect.left, TempRect.top, TempRect.right - TempRect.left, TempRect.bottom - TempRect.top,
|
|
|
|
SWP_NOZORDER);
|
|
|
|
finally
|
|
|
|
EndDeferWindowPos(TempHWND);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2021-12-08 17:45:03 +01:00
|
|
|
procedure TFMXWindowParent.Reparent(const aNewParentHandle : {$IFDEF DELPHI18_UP}TWindowHandle{$ELSE}TFmxHandle{$ENDIF});
|
2018-04-18 10:25:38 +02:00
|
|
|
var
|
|
|
|
TempChildHandle, TempParentHandle : HWND;
|
|
|
|
begin
|
2021-12-08 17:45:03 +01:00
|
|
|
{$IFDEF DELPHI18_UP}
|
2018-04-18 10:25:38 +02:00
|
|
|
if (aNewParentHandle <> nil) then
|
2021-12-08 17:45:03 +01:00
|
|
|
{$ELSE}
|
|
|
|
if (aNewParentHandle <> 0) then
|
|
|
|
{$ENDIF}
|
2018-04-18 10:25:38 +02:00
|
|
|
begin
|
|
|
|
TempChildHandle := FmxHandleToHWND(Handle);
|
|
|
|
TempParentHandle := FmxHandleToHWND(aNewParentHandle);
|
|
|
|
|
|
|
|
if (TempChildHandle <> 0) and (TempParentHandle <> 0) then
|
|
|
|
begin
|
|
|
|
SetWindowLong(TempChildHandle, GWL_STYLE, WS_CHILDWINDOW);
|
|
|
|
WinApi.Windows.SetParent(TempChildHandle, TempParentHandle);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
{$ENDIF}
|
|
|
|
|
2023-01-01 18:17:22 +01:00
|
|
|
procedure TFMXWindowParent.Notification(AComponent: TComponent; Operation: TOperation);
|
|
|
|
begin
|
|
|
|
inherited Notification(AComponent, Operation);
|
|
|
|
|
|
|
|
if (Operation = opRemove) and (AComponent = FChromium) then FChromium := nil;
|
|
|
|
end;
|
|
|
|
|
2018-04-18 10:25:38 +02:00
|
|
|
|
|
|
|
end.
|