salvadordf / CEF4Delphi

CEF4Delphi is an open source project to embed Chromium-based browsers in applications made with Delphi or Lazarus/FPC for Windows, Linux and MacOS.
https://www.briskbard.com/forum/
Other
1.2k stars 365 forks source link

Create dynamically #468

Closed p3rf1 closed 1 year ago

p3rf1 commented 1 year ago

Hello. I hope you can help me. I need to create tabs in tabcontrol and create a copy of each tab FMXChromium. The problem with this code is that the window with the website is created not in tabs, but on top of the tabcontrol. How can I solve this problem? Thank you

procedure TForm1.addBrowser(Link: string);
var
  TempHandle: HWND;
  TempRect: System.Types.TRect;
  TempClientRect: TRectF;
  TempScale: Single;
  FMXChromium: TFMXChromium;
  TempBitmap: TBitmap;
  TabItem: TTabItem;
  Lay: TLayout;
begin
  FMXChromium := TFMXChromium.Create(Self);
  TabItem := TTabItem.Create(TabControl);

    TabItem.Parent := TabControl;
    TabItem.TagString := '';
    TabItem.Text := '123123';

    Lay := TLayout.Create(TabItem);
    Lay.Parent := TabItem;
    Lay.Align := TAlignLayout.Client;

    if (FMXWindowParent = nil) then
    begin
      FMXWindowParent := TFMXWindowParent.CreateNew(Lay);

      FMXWindowParent.Chromium := FMXChromium;

     FMXWindowParent.Reparent(Handle);
    //  FMXWindowParent.Parent := Lay;

      if (FMXWindowParent <> nil) then
     begin
        FMXWindowParent.SetBounds(Round(Lay.Position.x), Round(Lay.Position.y),
          Round(Lay.Width), Round(Lay.Height));
        FMXWindowParent.UpdateSize;
      end;
     FMXWindowParent.Show;
    end;

    if not(FMXChromium.Initialized) then
    begin
      TempHandle := FmxHandleToHWND(FMXWindowParent.Handle);
      TempClientRect := FMXWindowParent.ClientRect;
      TempScale := FMXChromium.ScreenScale;
      TempRect.Left := Round(TempClientRect.Left);
      TempRect.Top := Round(TempClientRect.Top);
      TempRect.Right := Round(TempClientRect.Right * TempScale);
      TempRect.Bottom := Round(TempClientRect.Bottom * TempScale);

    end;

    FMXChromium.DefaultUrl := Link;
    FMXChromium.CreateBrowser(TempHandle, TempRect);

    TabControl.AddObject(TabItem);

end;
salvadordf commented 1 year ago

Hi,

Chromium requires a Windows handle to create a browser in normal mode.

Firemonkey controls are very different from normal Windows controls internally and only a few FMX controls have a Windows handle.

This is the reason why we have to define TFMXWindowParent as a TCommonCustomForm class.

Additionally, Chromium creates a few child controls to render the web page.

This could cause some issues if the application tries to draw other FMX controls over the web browser.

If your application has some overlapping issues with the web browser then try using the FMXTabbedOSRBrowser demo.

FMXTabbedOSRBrowser uses the off-screen rendering mode and it draws the web contents on a normal FMX control that behaves like any other Firemonkey control.

Please, use our forum for more questions.