gpt4 book ai didi

c# - 在 Xamarin Mac 中阻止鼠标气泡

转载 作者:行者123 更新时间:2023-11-30 16:45:17 24 4
gpt4 key购买 nike

我创建了一个自定义 NSView,我想将其放置在窗口内容的顶部,以在加载所有内容时阻止任何交互。我遇到的问题是我可以通过 NSView 单击到下面的控件,尽管现在已经修复了。新问题是,即使我无法单击控件,当我将鼠标移到文本控件上时,鼠标也会切换到 I Beam 图标。

如何让 NSView 完全阻止与其下方所有内容的所有交互?

我创建的 NSView 如下:

[Register("StupidView")]
public class StupidView : NSView
{


public StupidView()
{
// Init
Initialize();
}

public StupidView(IntPtr handle) : base (handle)
{
// Init
Initialize();
}

[Export("initWithFrame:")]
public StupidView(CGRect frameRect) : base(frameRect) {
// Init
Initialize();
}

private void Initialize()
{
this.AcceptsTouchEvents = true;
this.WantsLayer = true;
this.LayerContentsRedrawPolicy = NSViewLayerContentsRedrawPolicy.OnSetNeedsDisplay;
}

public override void DrawRect(CGRect dirtyRect)
{
var ctx = NSGraphicsContext.CurrentContext.GraphicsPort;
ctx.SetFillColor(new CGColor(128, 128, 128, 0.7f));
ctx.FillRect(dirtyRect);
}

public override void MouseDown(NSEvent theEvent)
{
if (Hidden)
{
base.MouseDown(theEvent);
}
}

public override void MouseDragged(NSEvent theEvent)
{

if (Hidden)
{
base.MouseDragged(theEvent);
}
}

public override bool AcceptsFirstResponder()
{
return !this.Hidden;
}

public override bool AcceptsFirstMouse(NSEvent theEvent)
{
return !this.Hidden;
}

public override NSView HitTest(CGPoint aPoint)
{
return Hidden ? null : this;
}
}

最佳答案

几周前我遇到了同样的问题,下面是我如何解决这个问题:

首先,为了防止用户在下面放置的 super View 上进行交互,我添加了一个透明按钮,该按钮仅用于捕捉鼠标点击,如果您不需要做任何事情,则什么都不做:

 private void Initialize()
{
this.AcceptsTouchEvents = true;
this.WantsLayer = true;
this.LayerContentsRedrawPolicy = NSViewLayerContentsRedrawPolicy.OnSetNeedsDisplay;

//Add button to prevent user interactions

NSButton buttonToPreventUserInteraction = new NSButton();
buttonToPreventUserInteraction.Bordered = false;
buttonToPreventUserInteraction.Transparent = true;
buttonToPreventUserInteraction.TranslatesAutoresizingMaskIntoConstraints = false;
AddSubview(buttonToPreventUserInteraction);

//If you want to add some constraints on the button, for it to resize and keep the same size of your subview

var dicoViews = new NSMutableDictionary();
dicoViews.Add((NSString)"buttonToPreventUserInteraction", buttonToPreventUserInteraction);
NSLayoutConstraint[] buttonToPreventUserInteractionHorizontalConstraints = NSLayoutConstraint.FromVisualFormat("H:|[buttonToPreventUserInteraction]|", NSLayoutFormatOptions.DirectionLeadingToTrailing, null, dicoViews);
NSLayoutConstraint[] buttonToPreventUserInteractionVerticalConstraints = NSLayoutConstraint.FromVisualFormat("V:|[buttonToPreventUserInteraction]|", NSLayoutFormatOptions.DirectionLeadingToTrailing, null, dicoViews);
AddConstraints(buttonToPreventUserInteractionHorizontalConstraints);
AddConstraints(buttonToPreventUserInteractionVerticalConstraints);

}

对于您的其他问题,即鼠标光标从放置在下方的 super View 中的内容发生变化,您可以在 subview 上添加一个 NSTrackingArea,并实现覆盖方法“MouseMoved”以更改光标。你可以这样做:

首先在您的 subview 上添加 NSTrackingArea(您可以将这段代码放在您的“Initialize”方法中)

NSTrackingAreaOptions opts = ((NSTrackingAreaOptions.MouseMoved | NSTrackingAreaOptions.ActiveInKeyWindow | NSTrackingAreaOptions.InVisibleRect));
var trackingArea = new NSTrackingArea(new CGRect(0, 0, FittingSize.Width, FittingSize.Height), opts, Self, null);

AddTrackingArea(trackingArea);

然后实现覆盖方法:

public override void MouseMoved(NSEvent theEvent)
{
//You can choose the type of cursor you want to use here
NSCursor.ArrowCursor.Set();
}

这是为我做的,希望它也适合你

关于c# - 在 Xamarin Mac 中阻止鼠标气泡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42482434/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com