原文: http://zetcode.com/gui/gtksharp/events/

在 GTK# 编程教程的这一部分中,我们将讨论事件。

GTK# 库是一个事件驱动的系统。 所有 GUI 应用都会对事件做出反应。 应用启动一个主循环,该循环不断检查新生成的事件。 如果没有事件,则应用将等待并且不执行任何操作。 事件主要由应用的用户生成。 但是它们也可以通过其他方式生成,例如互联网连接,窗口管理器或计时器。

简单事件示例

下一个示例显示了我们如何应对两个基本事件。

quitbutton.cs

  1. using Gtk;
  2. using System;
  3. class SharpApp : Window {
  4. public SharpApp() : base ("Button")
  5. {
  6. SetDefaultSize(250, 200);
  7. SetPosition(WindowPosition.Center);
  8. DeleteEvent += delegate { Application.Quit(); };
  9. Fixed fix = new Fixed();
  10. Button quit = new Button("Quit");
  11. quit.Clicked += OnClick;
  12. quit.SetSizeRequest(80, 35);
  13. fix.Put(quit, 50, 50);
  14. Add(fix);
  15. ShowAll();
  16. }
  17. void OnClick(object sender, EventArgs args)
  18. {
  19. Application.Quit();
  20. }
  21. public static void Main()
  22. {
  23. Application.Init();
  24. new SharpApp();
  25. Application.Run();
  26. }
  27. }

在我们的代码示例中,我们对两个事件作出反应:Delete事件和Clicked事件。

当我们关闭窗口时,将触发删除事件。 默认情况下,当我们单击标题栏中的关闭按钮时,应用不会退出。

  1. DeleteEvent += delegate { Application.Quit(); };

当我们使用delegate关键字时,我们可以编写将对这个特定事件做出反应的行代码。

  1. quit.Clicked += OnClick;

在这里,我们指定使用OnClick()方法对Clicked事件做出反应。

  1. void OnClick(object sender, EventArgs args)
  2. {
  3. Application.Quit();
  4. }

这是OnClick()方法。 它有两个参数。 第一个参数是对象,它触发了此事件。 在我们的例子中,它是退出按钮。 第二个参数为我们提供了有关事件的各种其他信息。 事件参数始终取决于事件的类型。 每种方法的签名都可以在 GTK# 库的参考手册中找到。 http://www.go-mono.com/docs/

移动窗口

下一个示例显示了我们如何对移动窗口事件做出反应。 我们在标题栏中显示窗口左上角的当前位置。

move.cs

  1. using Gtk;
  2. using System;
  3. class SharpApp : Window {
  4. public SharpApp() : base("")
  5. {
  6. SetDefaultSize(250, 150);
  7. SetPosition(WindowPosition.Center);
  8. DeleteEvent += delegate { Application.Quit(); };
  9. Show();
  10. }
  11. protected override bool OnConfigureEvent(Gdk.EventConfigure args)
  12. {
  13. base.OnConfigureEvent(args);
  14. Title = args.X + ", " + args.Y;
  15. return true;
  16. }
  17. public static void Main()
  18. {
  19. Application.Init();
  20. new SharpApp();
  21. Application.Run();
  22. }
  23. }

在前面的示例中,我们已将委托或方法插入事件。 在 GTK# 中,许多事件已经具有处理器方法。 在这种情况下,我们可以覆盖该方法。 在我们的代码示例中就是这种情况。

  1. protected override bool OnConfigureEvent(Gdk.EventConfigure args)
  2. {
  3. base.OnConfigureEvent(args);
  4. Title = args.X + ", " + args.Y;
  5. return true;
  6. }

在这里,我们将覆盖预定义的OnConfigureEvent()方法。 当我们调整大小或移动窗口小部件时,触发Configure事件。 请注意,该方法的第一行调用默认方法。 没有此行,程序将无法正常运行。 下一行将窗口的 x,y 坐标设置为窗口的标题。

GTK# 中的事件 - 图1

图:移动事件

EnterNotifyEvent

当我们使用鼠标指针进入小部件的区域时,会发出EnterNotifyEvent

enter.cs

  1. using Gtk;
  2. using System;
  3. class SharpApp : Window {
  4. public SharpApp() : base("Enter")
  5. {
  6. SetDefaultSize(200, 150);
  7. SetPosition(WindowPosition.Center);
  8. DeleteEvent += delegate { Application.Quit(); };
  9. Button button = new Button("Button");
  10. button.EnterNotifyEvent += OnEnter;
  11. Fixed fix = new Fixed();
  12. fix.Put(button, 20, 20);
  13. Add(fix);
  14. ShowAll();
  15. }
  16. void OnEnter(object sender, EnterNotifyEventArgs args)
  17. {
  18. Button button = (Button) sender;
  19. button.ModifyBg(StateType.Prelight, new Gdk.Color(220, 220, 220));
  20. }
  21. public static void Main()
  22. {
  23. Application.Init();
  24. new SharpApp();
  25. Application.Run();
  26. }
  27. }

一旦将鼠标指针悬停在按钮小部件上,我们将更改其背景颜色。

  1. button.EnterNotifyEvent += OnEnter;

我们将OnEnter()方法插入EnterNotifyEvent

  1. Button button = (Button) sender;
  2. button.ModifyBg(StateType.Prelight, new Gdk.Color(220, 220, 220));

我们获取按钮小部件并修改其背景颜色。

断开事件处理器

我们可以从事件断开处理器方法。 下一个代码示例演示了这种情况。

disconnect.cs

  1. using Gtk;
  2. using System;
  3. class SharpApp : Window {
  4. Button button;
  5. public SharpApp() : base("Disconnect")
  6. {
  7. SetDefaultSize(250, 150);
  8. SetPosition(WindowPosition.Center);
  9. DeleteEvent += delegate { Application.Quit(); };
  10. button = new Button("Button");
  11. CheckButton cb = new CheckButton("connect");
  12. cb.Toggled += OnToggled;
  13. Fixed fix = new Fixed();
  14. fix.Put(button, 30, 50);
  15. fix.Put(cb, 130, 50);
  16. Add(fix);
  17. ShowAll();
  18. }
  19. void OnClick(object sender, EventArgs args)
  20. {
  21. Console.WriteLine("Click");
  22. }
  23. void OnToggled(object sender, EventArgs args)
  24. {
  25. CheckButton cb = (CheckButton) sender;
  26. if (cb.Active) {
  27. button.Clicked += OnClick;
  28. } else {
  29. button.Clicked -= OnClick;
  30. }
  31. }
  32. public static void Main()
  33. {
  34. Application.Init();
  35. new SharpApp();
  36. Application.Run();
  37. }
  38. }

在代码示例中,我们有一个按钮和一个复选框。 当我们单击按钮并且复选框处于活动状态时,我们在控制台中显示"Click"文本。 该复选框可将处理器方法与按钮Clicked事件连接或断开连接。

  1. CheckButton cb = new CheckButton("connect");
  2. cb.Toggled += OnToggled;

我们有一个复选框。 该小部件具有Toggled事件。 我们将OnToggled()方法插入此事件。

  1. CheckButton cb = (CheckButton) sender;
  2. if (cb.Active) {
  3. button.Clicked += OnClick;
  4. } else {
  5. button.Clicked -= OnClick;
  6. }

这些行根据复选框小部件的状态连接或断开事件处理器。

GTK# 中的事件 - 图2

图:断开连接

本章是关于 GTK# 中的事件的。