Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Cross-Platform-Entwicklung (https://www.delphipraxis.net/91-cross-platform-entwicklung/)
-   -   NSImage (https://www.delphipraxis.net/208902-nsimage.html)

Peter666 27. Sep 2021 13:58

NSImage
 
Hi,

hat schon mal jemand auf OSX/iOS ein NSImage über das bestehende Canvas gezeichnet? Der Hintergrund ist der: Ich wollte gerne über den Browser eine Art einblendbares Halbtransparentes OSD packen und das geht mit Firemonkey ja nicht wirklich.

Delphi-Quellcode:
unit UOSD;

interface

uses System.Types, System.Classes, FMX.Graphics, FMX.Controls;

type
  TOSD = class(TControl)
  protected
    FBitmap: TBitmap;
    FNativePaint: Boolean;

    procedure Paint; override;
    procedure SetBitmap(const ABitmap: TBitmap);
    procedure SetNativePaint(const ANativePaint: Boolean);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Bitmap: TBitmap read FBitmap write SetBitmap;
    property Opacity;
    property NativePaint: Boolean read FNativePaint write SetNativePaint
      default false;
  end;

implementation

constructor TOSD.Create(AOwner: TComponent);
begin
  inherited;
  Width := 100;
  Height := 100;
  FBitmap := TBitmap.Create;
end;

destructor TOSD.Destroy;
begin
  FBitmap.Free;
  inherited;
end;

procedure TOSD.SetNativePaint(const ANativePaint: Boolean);
begin
  if FNativePaint <> ANativePaint then
  begin
    FNativePaint := ANativePaint;
    Repaint;
  end;
end;

procedure TOSD.SetBitmap(const ABitmap: TBitmap);
begin
  FBitmap.Assign(ABitmap);
  Repaint;
end;

//WindowHandleToPlatform(FForm.Handle).View
procedure TOSD.Paint;
var
  DstRect: TRectF;
begin
{$IFDEF MACOS}
  if FNativePaint then
  begin
    exit;
  end;
{$ENDIF}
  DstRect := FBitmap.BoundsF;
  DstRect.Fit(BoundsRect);
  Canvas.DrawBitmap(FBitmap, FBitmap.BoundsF, DstRect, Opacity)
end;

end.
Peter

Rollo62 27. Sep 2021 16:09

AW: NSImage
 
Ich hab es nicht probiert, aber es sollten doch angeblich die Z-Orders mit den NativeComponents kompatibler geworden sein in Rx10.4/Rx11.
Hast Du mal probiert ein TLayout IN den WebBrowser zu werfen und dann mit Client/Content voll auszufüllen.
Dann sollten doch im TLayout weitere Komponenten wie TRectangle oder TImage den Webbrowser verdecken können,
wobei man die Opacity frei einstellen könnte.

Ich mache das an einigen Stellen so, OK aber ich habe auch aktuell keinen WebBrowser da drunterliegen.

Peter666 28. Sep 2021 10:38

AW: NSImage
 
Danke für die Rückmeldung. Leider geht das immer noch nicht und ich denke mal das wird auch in Zukunft nicht so gehen, aber eventuell ein TLayout Abkömmling der ein eigenes NSView oder UIView hält auf dem dann die restlichen Inhalte gezeichnet werden. Leider hab ich nicht wirklich viel Ahnung von MacOS und iOS.

CHackbart 29. Sep 2021 12:12

AW: NSImage
 
Auf MacOS kannst du NSImageView nutzen. Das ist relativ einfach und du hast keine Huddelei mit nativen Elementen. Auf IOS und Android bzw. unter Windows geht das denke ich ähnlich. Ich wüsste auch gerne, wie man das möglichst Elegant und großen Codeaufwand beheben kann.

Delphi-Quellcode:
unit UOSD;

interface

uses System.Types, System.Classes, FMX.Graphics, FMX.Controls
    {$IFDEF MACOS}
     , Macapi.AppKit
    {$ENDIF};

type
  TOSD = class(TControl)
  protected
    FBitmap: TBitmap;
    FNativePaint: Boolean;
    FNeedUpdate: Boolean;
{$IFDEF MACOS}
    FOverlayView: NSImageView;
{$ENDIF}
    procedure Paint; override;
    procedure Resize; override;
    procedure SetBitmap(const ABitmap: TBitmap);
    procedure SetNativePaint(const ANativePaint: Boolean);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Bitmap: TBitmap read FBitmap write SetBitmap;
    property Opacity;
    property NativePaint: Boolean read FNativePaint write SetNativePaint
      default false;
  end;

implementation

uses FMX.Types, FMX.Forms
{$IFDEF MACOS}, FMX.Platform.Mac, FMX.Helpers.Mac, Macapi.CocoaTypes, Macapi.CoreGraphics {$ENDIF};

function GetParentForm(Control: TFmxObject): TCommonCustomForm;
begin
  if (Control.Root <> nil) and (Control.Root.GetObject is TCommonCustomForm)
  then
    Result := TCommonCustomForm(Control.Root.GetObject)
  else
    Result := nil;
end;

constructor TOSD.Create(AOwner: TComponent);
begin
  inherited;
  Width := 100;
  Height := 100;
  FBitmap := TBitmap.Create;
end;

destructor TOSD.Destroy;
begin
  FBitmap.free;
  inherited;
end;

procedure TOSD.SetNativePaint(const ANativePaint: Boolean);
begin
  if FNativePaint <> ANativePaint then
  begin
    FNativePaint := ANativePaint;
{$IFDEF MACOS}
    if assigned(FOverlayView) then
      FOverlayView.release;
    FNeedUpdate := true;
{$ENDIF}
    Repaint;
  end;
end;

procedure TOSD.SetBitmap(const ABitmap: TBitmap);
begin
  FBitmap.Assign(ABitmap);
  FNeedUpdate := true;
  Repaint;
end;

procedure TOSD.Resize;
{$IFDEF MACOS}
var
  R: TRectF;
{$ENDIF}
begin
  inherited;
{$IFDEF MACOS}
  if assigned(FOverlayView) then
  begin
    R := GetAbsoluteRect;
    FOverlayView.setFrame(MakeNSRect(R.Left, R.Top, R.Width, R.Height));
  end;
{$ENDIF}
end;

procedure TOSD.Paint;
var
  DstRect: TRectF;
{$IFDEF MACOS}
  FormView: NSView;
  Form: TCommonCustomForm;
  R: TRectF;
{$ENDIF}
begin
{$IFDEF MACOS}
  if FNativePaint then
  begin
    if FNeedUpdate then
    begin
      if not assigned(FOverlayView) then
      begin
        R := BoundsRect;

        FOverlayView := TNSImageView.Wrap
          (TNSImageView.Alloc.initWithFrame(MakeNSRect(R.Left, R.Top, R.Width,
          R.Height)));
        FOverlayView.retain;

        Form := GetParentForm(self);

        if assigned(Form) then
        begin
          FormView := TNSView.Wrap(WindowHandleToPlatform(Form.Handle)
            .Wnd.contentView);
          FormView.addSubview(FOverlayView, NSWindowAbove, nil);
        end;
        FOverlayView.setWantsLayer(true);
        FOverlayView.setFrame(MakeNSRect(R.Left, R.Top, R.Width, R.Height));
      end;
      FOverlayView.setImage(BitmapToMacBitmap(FBitmap));
      FNeedUpdate := false;
    end;
    exit;
  end;
{$ENDIF}
  DstRect := FBitmap.BoundsF;
  DstRect.Fit(BoundsRect);
  Canvas.DrawBitmap(FBitmap, FBitmap.BoundsF, DstRect, Opacity)
end;

end.

Rollo62 29. Sep 2021 12:53

AW: NSImage
 
Hier gibt es auch weitere Ideen wie man NativeControl einsetzen kann.
https://github.com/DelphiWorlds/Kast...ativeImage.pas


Alle Zeitangaben in WEZ +1. Es ist jetzt 00:17 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz