Delphi OS X子类NSWindow与鼠标事件无关

时间:2014-10-16 08:36:13

标签: macos cocoa delphi firemonkey nswindow

如何在delphi firemonkey中创建NSWindow的派生类? 我成功创建了一个只包含webview的Cocoa窗口。 (使用NSWindow.Wrap,setContentView,orderFront等) 我使它无边框,但问题是它不接受鼠标移动事件,如下所述: Why NSWindow without styleMask:NSTitledWindowMask can not be keyWindow?

是否可以在delphi中继承NSWindow并覆盖canBecomeKeyWindow?

它不起作用(编译,但没有调用方法):

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

`

这也无效:

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

那么,我怎样才能将NSWindow子类化并覆盖其中一个方法?

修改

使用Sebastian的解决方案后, 实际创建窗口,你可以使用这样的东西:

  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 个答案:

答案 0 :(得分:2)

这是一个从Cocoa类派生的快速示例。

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.