Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C语言中捕获鼠标事件_C#_Xamarin_Mono_Nswindow_Monomac - Fatal编程技术网

C# 如何在C语言中捕获鼠标事件

C# 如何在C语言中捕获鼠标事件,c#,xamarin,mono,nswindow,monomac,C#,Xamarin,Mono,Nswindow,Monomac,首先,我的目标是实用地创建没有控制器之类的东西,只有一个类无边界的NSWindow。在这个窗口中,我想跟踪鼠标事件,如:鼠标按下,鼠标移动。就这么简单 我需要在Monomac项目中使用Xamarin Studio在C中完成这项工作。我的窗户非常简单 public class MyWindow : NSWindow { public MyWindow() :base(new RectangleF (0, 0, 100, 100), NSWindow

首先,我的目标是实用地创建没有控制器之类的东西,只有一个类无边界的NSWindow。在这个窗口中,我想跟踪鼠标事件,如:鼠标按下,鼠标移动。就这么简单

我需要在Monomac项目中使用Xamarin Studio在C中完成这项工作。我的窗户非常简单

public class MyWindow : NSWindow
{
    public MyWindow()
        :base(new RectangleF (0, 0, 100, 100), 
            NSWindowStyle.Closable | NSWindowStyle.Resizable | NSWindowStyle.Titled, NSBackingStore.Buffered, 
            false)
    {
        this.StyleMask = NSWindowStyle.Borderless;
        this.MakeKeyAndOrderFront( null );
    }
}
现在,据我所知,我应该使用声明的事件将NSView添加到这个NSWindow类中。所以我创建了NSView类:

public class MouseTracking : NSView
{
    public NSTrackingArea tracking;

    public override bool AcceptsFirstResponder ()
    {
        return true;
    }

    public override void DrawRect(RectangleF dirtyRect)
    {
        base.DrawRect(dirtyRect);
        var context = NSGraphicsContext.CurrentContext.GraphicsPort;
        var rectangle = new RectangleF (0,0, this.Frame.Width, this.Frame.Height);
        NSColor.Blue.Set ();
        context.FillRect (rectangle);
    }

    public override void UpdateTrackingAreas()
    {
        if (tracking != null)
        {
            this.RemoveTrackingArea(tracking);
            tracking.Release();
        }
        tracking = new NSTrackingArea(this.Frame,NSTrackingAreaOptions.MouseEnteredAndExited | NSTrackingAreaOptions.ActiveAlways ,this, null);
        this.AddTrackingArea(tracking);
    }

    public override void MouseMoved(NSEvent theEvent)
    {
        Console.WriteLine("MouseMoved");
    }

    public override void MouseExited(NSEvent theEvent)
    {
        Console.WriteLine("MouseExitedTest");
    }
}
我的问题是如何将MouseTracking nsview添加到MyWindow并使这些鼠标事件可用?我已尝试在MyWindow构造函数中执行以下操作:

        MouseTracking tracking = new MouseTracking ();
        this.ContentView.AddSubview (tracking);
但是,这是行不通的。。我只想在WinForms中执行这两行:

this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseDown);

private void panel1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { }

感谢您的帮助

您不需要DrawRect来捕获鼠标事件。你需要承包商:

    public MouseTracking(NSCoder coder)
        :base(coder)
    {
    }

    public MouseTracking(RectangleF frameRect)
        :base(frameRect)
    {
    }
您可能还需要(也许您还不知道)覆盖此标志:

    public override bool AcceptsFirstMouse (NSEvent theEvent)
    {
        return true;
    }
是的,你在MyWindow中添加了捕鼠器,就像你写的那样,所以:

    this.ContentView.AddSubview (tracking);