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

在 GTK# 编程教程的这一部分中,我们将介绍一些 GTK# 小部件。

小部件是 GUI 应用的基本构建块。 多年来,几个小部件已成为所有 OS 平台上所有工具包中的标准。 例如,按钮,复选框或滚动条。 GTK# 工具箱的理念是将小部件的数量保持在最低水平。 将创建更多专门的小部件作为自定义 GTK# 小部件。

Label

Label小部件显示文本。

label.cs

  1. using Gtk;
  2. class SharpApp : Window {
  3. string text = @"Meet you downstairs in the bar and heard
  4. your rolled up sleeves and your skull t-shirt
  5. You say why did you do it with him today?
  6. and sniff me out like I was Tanqueray
  7. cause you're my fella, my guy
  8. hand me your stella and fly
  9. by the time I'm out the door
  10. you tear men down like Roger Moore
  11. I cheated myself
  12. like I knew I would
  13. I told ya, I was trouble
  14. you know that I'm no good";
  15. public SharpApp() : base("You know I'm No Good")
  16. {
  17. BorderWidth = 8;
  18. SetPosition(WindowPosition.Center);
  19. DeleteEvent += delegate { Application.Quit(); };
  20. Label lyrics = new Label(text);
  21. Add(lyrics);
  22. ShowAll();
  23. }
  24. public static void Main()
  25. {
  26. Application.Init();
  27. new SharpApp();
  28. Application.Run();
  29. }
  30. }

该代码示例在窗口上显示了一些歌词。

  1. string text = @"Meet you downstairs in the bar and heard
  2. your rolled up sleeves and your skull t-shirt
  3. ...

在 C# 编程语言中,多行字符串以@字符开头。

  1. BorderWidth = 8;

Label周围有一些空白。

  1. Label lyrics = new Label(text);
  2. Add(lyrics);

Label小部件已创建并添加到窗口。

GTK# 中的小部件 - 图1

图:Label小部件

CheckButton

CheckButton是具有两种状态的窗口小部件:打开和关闭。 开状态通过复选标记显示。 它用来表示一些布尔属性。

checkbutton.cs

  1. using Gtk;
  2. using System;
  3. class SharpApp : Window {
  4. public SharpApp() : base("CheckButton")
  5. {
  6. SetDefaultSize(250, 200);
  7. SetPosition(WindowPosition.Center);
  8. DeleteEvent += delegate { Application.Quit(); };
  9. CheckButton cb = new CheckButton("Show title");
  10. cb.Active = true;
  11. cb.Toggled += OnToggle;
  12. Fixed fix = new Fixed();
  13. fix.Put(cb, 50, 50);
  14. Add(fix);
  15. ShowAll();
  16. }
  17. void OnToggle(object sender, EventArgs args)
  18. {
  19. CheckButton cb = (CheckButton) sender;
  20. if (cb.Active) {
  21. Title = "CheckButton";
  22. } else {
  23. Title = " ";
  24. }
  25. }
  26. public static void Main()
  27. {
  28. Application.Init();
  29. new SharpApp();
  30. Application.Run();
  31. }
  32. }

根据CheckButton的状态,我们将在窗口的标题栏中显示标题。

  1. CheckButton cb = new CheckButton("Show title");

CheckButton小部件已创建。

  1. cb.Active = true;

默认情况下标题是可见的,因此我们默认情况下选中复选按钮。

  1. CheckButton cb = (CheckButton) sender;

在这里,我们将发送方对象转换为CheckButton类。

  1. if (cb.Active) {
  2. Title = "CheckButton";
  3. } else {
  4. Title = " ";
  5. }

根据CheckButtonActive属性,我们显示或隐藏窗口的标题。

GTK# 中的小部件 - 图2

图:CheckButton

ComboBox

ComboBox是一个小部件,允许用户从选项列表中进行选择。

combobox.cs

  1. using Gtk;
  2. using System;
  3. class SharpApp : Window {
  4. Label label;
  5. public SharpApp() : base("ComboBox")
  6. {
  7. string[] distros = new string[] {"Ubuntu",
  8. "Mandriva",
  9. "Red Hat",
  10. "Fedora",
  11. "Gentoo" };
  12. SetDefaultSize(250, 200);
  13. SetPosition(WindowPosition.Center);
  14. BorderWidth = 7;
  15. DeleteEvent += delegate { Application.Quit(); };
  16. Fixed fix = new Fixed();
  17. ComboBox cb = new ComboBox(distros);
  18. cb.Changed += OnChanged;
  19. label = new Label("-");
  20. fix.Put(cb, 50, 30);
  21. fix.Put(label, 50, 140);
  22. Add(fix);
  23. ShowAll();
  24. }
  25. void OnChanged(object sender, EventArgs args)
  26. {
  27. ComboBox cb = (ComboBox) sender;
  28. label.Text = cb.ActiveText;
  29. }
  30. public static void Main()
  31. {
  32. Application.Init();
  33. new SharpApp();
  34. Application.Run();
  35. }
  36. }

该示例显示了一个组合框和一个标签。 组合框具有六个选项的列表。 这些是 Linux 发行版的名称。 标签窗口小部件显示了从组合框中选择的选项。

  1. string[] distros = new string[] {"Ubuntu",
  2. "Mandriva",
  3. "Red Hat",
  4. "Fedora",
  5. "Gentoo" };

这是一个字符串数组,将显示在ComboBox小部件中。

  1. ComboBox cb = new ComboBox(distros);

ComboBox小部件已创建。 构造器将字符串数组作为参数。

  1. void OnChanged(object sender, EventArgs args)
  2. {
  3. ComboBox cb = (ComboBox) sender;
  4. label.Text = cb.ActiveText;
  5. }

OnChanged()方法内部,我们从组合框中获取选定的文本并将其设置为标签。

GTK# 中的小部件 - 图3

图:ComboBox

Image

下一个示例介绍Image小部件。 此小部件显示图片。

image.cs

  1. using Gtk;
  2. using System;
  3. class SharpApp : Window {
  4. Gdk.Pixbuf castle;
  5. public SharpApp() : base("Red Rock")
  6. {
  7. BorderWidth = 1;
  8. SetPosition(WindowPosition.Center);
  9. DeleteEvent += delegate { Application.Quit(); };
  10. try {
  11. castle = new Gdk.Pixbuf("redrock.png");
  12. } catch {
  13. Console.WriteLine("Image not found");
  14. Environment.Exit(1);
  15. }
  16. Image image = new Image(castle);
  17. Add(image);
  18. ShowAll();
  19. }
  20. public static void Main()
  21. {
  22. Application.Init();
  23. new SharpApp();
  24. Application.Run();
  25. }
  26. }

我们在窗口中显示红色岩石城堡。

  1. try {
  2. castle = new Gdk.Pixbuf("redrock.png");
  3. } catch {
  4. Console.WriteLine("Image not found");
  5. Environment.Exit(1);
  6. }

我们创建Gdk.Pixbuf小部件。 我们将构造器放在trycatch关键字之间,以处理可能的错误。

  1. Image image = new Image(castle);
  2. Add(image);

Image小部件已创建并添加到窗口。

GTK# 中的小部件 - 图4

图:图像

在本章中,我们展示了 GTK# 编程库的第一组基本小部件。