Delphi OS X subclass NSWindow borderless with mouse events

跟風遠走 提交于 2020-01-05 06:01:42

问题


How to create a derived class of NSWindow in delphi firemonkey? I've succeeded in creating a Cocoa window that contains only a webview. (using NSWindow.Wrap, setContentView, orderFront etc) I made it borderless, but the problem is that it does not accept mouse moved events, as described here: Why NSWindow without styleMask:NSTitledWindowMask can not be keyWindow?

Is it possible to subclass NSWindow in delphi and override canBecomeKeyWindow?

It's not working (compiles, but method not called):

type
 TMYNSWindow = class(TNSWindow)
   function canBecomeKeyWindow: Boolean; cdecl;
 end;
 function TMYNSWindow.canBecomeKeyWindow: Boolean;
 begin
   Result := true;
end;

`

This also ineffective:

TMYNSWindow = class(TOCGenericImport<NSWindowClass, NSWindow>) function canBecomeKeyWindow: Boolean; cdecl; end;

So, how can I subclass NSWindow and override one of its method?

EDIT

After using Sebastian's solution, to create the window actually, you can use something like this:

  constructor TMYNSWindow.Create( contentRect: NSRect; styleMask: NSUInteger; backing: NSBackingStoreType; defer: Boolean );
  var
    V : Pointer;
  begin
    inherited Create;
    V := NSWindow(Super).initWithContentRect( contentRect, styleMask, backing, defer );
    if GetObjectID <> V then UpdateObjectID(V);
  end;


var
  MyNSW : TMyNSWindow;
  NSW : NSWindow;

...

  MyNSW := TMyNSWindow.Create(
    MakeNSRect(0, 0, 600, 400),
    NSBorderlessWindowMask
    //NSClosableWindowMask or NSMiniaturizableWindowMask or NSResizableWindowMask
    ,NSBackingStoreBuffered, false );

  MyNSW.Super.QueryInterface( StringToGUID(GUID_NSWINDOW), NSW );   //GUID_NSWINDOW = '{8CDBAC20-6E46-4618-A33F-229394B70A6D}';
  NSW.setFrame( R, true, true );  // R is NSRect, fill it before...
  NSW.orderFront((TNSApplication.Wrap(TNSApplication.OCClass.sharedApplication) as ILocalObject).GetObjectID );

回答1:


Here's a quick example for deriving from a Cocoa class.

unit Unit2;

interface

uses
  MacApi.AppKit, Macapi.ObjectiveC, System.TypInfo;

type
  MyNSWindow = interface(NSWindow)
    // Press Ctrl+Shift+G to insert a unique guid here
    function canBecomeKeyWindow: Boolean; cdecl;
  end;

  TMyNSWindow = class(TOCLocal)
  protected
    function GetObjectiveCClass: PTypeInfo; override;
  public
    function canBecomeKeyWindow: Boolean; cdecl;
    constructor Create;
  end;

implementation

{ TMyNSWindow }

function TMyNSWindow.canBecomeKeyWindow: Boolean;
begin
  Result := True;
end;

constructor TMyNSWindow.Create;
var
  V: Pointer;
begin
  inherited Create;
  V := NSWindow(Super).init;
  if GetObjectID <> V then
    UpdateObjectID(V);
end;

function TMyNSWindow.GetObjectiveCClass: PTypeInfo;
begin
  Result := TypeInfo(MyNSWindow);
end;


end.


来源:https://stackoverflow.com/questions/26399749/delphi-os-x-subclass-nswindow-borderless-with-mouse-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!