从单声道的user32.dll等效拖动检测(IntPtr,Point)?

时间:2011-02-21 17:55:50

标签: c# mono pinvoke

有人知道上述p / invoke调用的托管代码是否与mono一起使用? 是否有一个linux lib提供与windows上的user32.dll类似的功能?

1 个答案:

答案 0 :(得分:1)

我不确定是否存在针对x11 \ GTK的DragDirect调用的直接等效项,但您可以使用Gdk.Pointer.GrabGdk.Pointer.Ungrab方法具有相同的功能。在需要时捕获鼠标指针,然后跟踪鼠标移动并在用户点击Escape时释放,或释放鼠标或鼠标超出拖动矩形。

以下是一个小例子:

public partial class MainWindow : Gtk.Window
{
    public MainWindow () : base(Gtk.WindowType.Toplevel)
    {
        Build ();
    }

    protected void OnDeleteEvent (object sender, DeleteEventArgs a)
    {
        Application.Quit ();
        a.RetVal = true;
    }

    protected virtual void OnWidgetEvent (object o, Gtk.WidgetEventArgs args)
    {   
        if (args.Event is Gdk.EventMotion && args.Event.Type==Gdk.EventType.MotionNotify)
        {
            Gdk.EventMotion eventMotion = (Gdk.EventMotion)args.Event; 
            Console.WriteLine("mouse move {0} {1}", eventMotion.X, eventMotion.Y);
        }
        else if (args.Event is Gdk.EventKey && args.Event.Type==Gdk.EventType.KeyPress)
        {
            Gdk.EventKey eventKey = (Gdk.EventKey)args.Event;
            if (eventKey.Key==Gdk.Key.Escape)
            {
                Console.WriteLine("mouse pointer ungrab");
                Gtk.Grab.Remove(this);
                Gdk.Pointer.Ungrab(Gtk.Global.CurrentEventTime);
            }
        }
    }

    protected virtual void OnButton1WidgetEvent (object o, Gtk.WidgetEventArgs args)
    {
        if (args.Event is Gdk.EventButton && args.Event.Type==Gdk.EventType.ButtonPress)
        {
            Console.WriteLine("mouse pointer grab");
            Gdk.Pointer.Grab(this.GdkWindow, true,
                Gdk.EventMask.ButtonPressMask | Gdk.EventMask.ButtonReleaseMask | Gdk.EventMask.PointerMotionMask | Gdk.EventMask.EnterNotifyMask | Gdk.EventMask.LeaveNotifyMask, 
                null, null, Gtk.Global.CurrentEventTime); 
            Gtk.Grab.Add (this);
        }
    }
}

MainWindow UI声明:

  <widget class="Gtk.Window" id="MainWindow" design-size="400 300">
    <property name="MemberName" />
    <property name="Title" translatable="yes">MainWindow</property>
    <property name="WindowPosition">CenterOnParent</property>
    <signal name="DeleteEvent" handler="OnDeleteEvent" />
    <signal name="WidgetEvent" handler="OnWidgetEvent" />
    <child>
      <widget class="Gtk.Fixed" id="fixed1">
        <property name="MemberName" />
        <property name="HasWindow">False</property>
        <child>
          <widget class="Gtk.Button" id="button1">
            <property name="MemberName" />
            <property name="CanFocus">True</property>
            <property name="Type">TextOnly</property>
            <property name="Label" translatable="yes">GtkButton</property>
            <property name="UseUnderline">True</property>
            <signal name="WidgetEvent" handler="OnButton1WidgetEvent" />
          </widget>
          <packing>
            <property name="X">165</property>
            <property name="Y">125</property>
          </packing>
        </child>
      </widget>
    </child>
  </widget>

GTK中鼠标指针处理的更多细节:The Mouse Pointer

希望这有帮助,尊重