如何在 nsbox 和 nsimage 上更改光标类型
How to change cursor type on nsbox and nsimage
我正在使用 xamarin.mac 进行 cocoa 申请。我在视图控制器上有框,
我希望每当鼠标移到那个框上时,它就会变成手,就像在 nsbutton 中一样。
第一个:NSView 子类
创建 NSView
的子类并定义您在 MouseEntered
/ MouseExited
覆盖中发生的事情。在这种情况下,从游标堆栈中推入和弹出 NSCursor.ClosedHandCursor
。
public partial class MyCustomImageView : NSImageView
{
[Export("initWithCoder:")]
public MyCustomImageView(NSCoder coder) : base(coder) { }
public MyCustomImageView (IntPtr handle) : base (handle) { }
NSCursor cursor;
[Export("mouseEntered:")]
public override void MouseEntered(NSEvent theEvent)
{
cursor = NSCursor.ClosedHandCursor;
cursor.Push();
base.MouseEntered(theEvent);
}
[Export("mouseExisted:")]
public override void MouseExited(NSEvent theEvent)
{
base.MouseExited(theEvent);
cursor?.Pop();
}
}
第二名:NSTrackingArea
现在您可以定义一个 NSTrackingArea
来激活鼠标进入和退出的处理程序。假设 MyImageView
是您的 NSView 子类的 instance/outlet,然后将该跟踪区域添加到视图 (AddTrackingArea()
)。
var ta = new NSTrackingArea(MyImageView.Bounds, NSTrackingAreaOptions.ActiveAlways | NSTrackingAreaOptions.MouseEnteredAndExited, MyImageView, null);
MyImageView.AddTrackingArea(ta);
我正在使用 xamarin.mac 进行 cocoa 申请。我在视图控制器上有框, 我希望每当鼠标移到那个框上时,它就会变成手,就像在 nsbutton 中一样。
第一个:NSView 子类
创建 NSView
的子类并定义您在 MouseEntered
/ MouseExited
覆盖中发生的事情。在这种情况下,从游标堆栈中推入和弹出 NSCursor.ClosedHandCursor
。
public partial class MyCustomImageView : NSImageView
{
[Export("initWithCoder:")]
public MyCustomImageView(NSCoder coder) : base(coder) { }
public MyCustomImageView (IntPtr handle) : base (handle) { }
NSCursor cursor;
[Export("mouseEntered:")]
public override void MouseEntered(NSEvent theEvent)
{
cursor = NSCursor.ClosedHandCursor;
cursor.Push();
base.MouseEntered(theEvent);
}
[Export("mouseExisted:")]
public override void MouseExited(NSEvent theEvent)
{
base.MouseExited(theEvent);
cursor?.Pop();
}
}
第二名:NSTrackingArea
现在您可以定义一个 NSTrackingArea
来激活鼠标进入和退出的处理程序。假设 MyImageView
是您的 NSView 子类的 instance/outlet,然后将该跟踪区域添加到视图 (AddTrackingArea()
)。
var ta = new NSTrackingArea(MyImageView.Bounds, NSTrackingAreaOptions.ActiveAlways | NSTrackingAreaOptions.MouseEnteredAndExited, MyImageView, null);
MyImageView.AddTrackingArea(ta);