2016-08-18 60 views
0

我正在试验一下monodevelop/c#/ gdk,我能够创建一个窗口,并且正确地处理了这个expose事件的DrawingArea为什么不能获得`ButtonPressEvent`

然而,鼠标放下事件并没有调度,我不明白为什么。代表们已经成立了由GUI设计器自动生成代码:

this.da.ExposeEvent += new global::Gtk.ExposeEventHandler (this.OnDAExposeEvent); 
this.da.ButtonPressEvent += new global::Gtk.ButtonPressEventHandler (this.OnDAButtonPressEvent); 
this.da.MotionNotifyEvent += new global::Gtk.MotionNotifyEventHandler (this.OnDAMotionNotifyEvent); 

,这是我的初始化代码:

public MainWindow() : base (Gtk.WindowType.Toplevel) 
    { 
     Build(); 
     Gdk.Color col = new Gdk.Color(); 
     col.Red = col.Green = col.Blue = 0x8888; 
     da.ModifyBg(StateType.Normal, col); 
     var p = "wr/wn/wb/wq/wk/wp/br/bn/bb/bq/bk/bp".Split ('/'); 
     for (int i = 0; i < p.Length; i++) { 
      pieces[p[i]] = new ImageSurface("/home/agriffini/x/chessboard/i" + p [i] + ".png"); 
     } 
     da.Events |= (Gdk.EventMask.ButtonPressMask 
         | Gdk.EventMask.ButtonReleaseMask 
         | Gdk.EventMask.KeyPressMask 
         | Gdk.EventMask.PointerMotionMask); 
    } 

但是该处理函数OnDAButtonPressEvent不会被调用(通过放置一个断点检查那里)。

缺失的部分是什么?

回答

0

通过查看控制台输出发现问题。

问题是必须在实现小部件之前设置事件掩码。

这意味着当使用monodevelop和gui设计器时,您必须在widget-> properties-> events中使用gui来设置它们,因为自动生成的Build方法将在实现之前的适当时间设置事件掩码。

在调用Build之前设置代码中的事件掩码不起作用(小部件尚未创建);在呼叫之后设置掩码也不起作用(他们已经实现了)。

相关问题