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

在 GTK# 编程教程的这一部分中,我们将介绍对话框。

对话框窗口或对话框是大多数现代 GUI 应用必不可少的部分。 对话被定义为两个或更多人之间的对话。 在计算机应用中,对话框是一个窗口,用于与应用“对话”。 对话框用于输入数据,修改数据,更改应用设置等。对话框是用户与计算机程序之间进行通信的重要手段。

MessageDialog

消息对话框是方便的对话框,可向应用的用户提供消息。 该消息包含文本和图像数据。

messages.cs

  1. using Gtk;
  2. using System;
  3. class SharpApp : Window {
  4. public SharpApp() : base("Messages")
  5. {
  6. SetDefaultSize(250, 100);
  7. SetPosition(WindowPosition.Center);
  8. DeleteEvent += delegate { Application.Quit(); };
  9. Table table = new Table(2, 2, true);
  10. Button info = new Button("Information");
  11. Button warn = new Button("Warning");
  12. Button ques = new Button("Question");
  13. Button erro = new Button("Error");
  14. info.Clicked += delegate {
  15. MessageDialog md = new MessageDialog(this,
  16. DialogFlags.DestroyWithParent, MessageType.Info,
  17. ButtonsType.Close, "Download completed");
  18. md.Run();
  19. md.Destroy();
  20. };
  21. warn.Clicked += delegate {
  22. MessageDialog md = new MessageDialog(this,
  23. DialogFlags.DestroyWithParent, MessageType.Warning,
  24. ButtonsType.Close, "Unallowed operation");
  25. md.Run();
  26. md.Destroy();
  27. };
  28. ques.Clicked += delegate {
  29. MessageDialog md = new MessageDialog(this,
  30. DialogFlags.DestroyWithParent, MessageType.Question,
  31. ButtonsType.Close, "Are you sure to quit?");
  32. md.Run();
  33. md.Destroy();
  34. };
  35. erro.Clicked += delegate {
  36. MessageDialog md = new MessageDialog (this,
  37. DialogFlags.DestroyWithParent, MessageType.Error,
  38. ButtonsType.Close, "Error loading file");
  39. md.Run();
  40. md.Destroy();
  41. };
  42. table.Attach(info, 0, 1, 0, 1);
  43. table.Attach(warn, 1, 2, 0, 1);
  44. table.Attach(ques, 0, 1, 1, 2);
  45. table.Attach(erro, 1, 2, 1, 2);
  46. Add(table);
  47. ShowAll();
  48. }
  49. public static void Main()
  50. {
  51. Application.Init();
  52. new SharpApp();
  53. Application.Run();
  54. }
  55. }

在我们的示例中,我们将显示四种消息对话框。 信息,警告,问题和错误消息对话框。

  1. Button info = new Button("Information");
  2. Button warn = new Button("Warning");
  3. Button ques = new Button("Question");
  4. Button erro = new Button("Error");

我们有四个按钮。 这些按钮中的每个按钮都会显示不同类型的消息对话框。

  1. info.Clicked += delegate {
  2. MessageDialog md = new MessageDialog(this,
  3. DialogFlags.DestroyWithParent, MessageType.Info,
  4. ButtonsType.Close, "Download completed");
  5. md.Run();
  6. md.Destroy();
  7. };

如果单击信息按钮,将显示“信息”对话框。 MessageType.Info指定对话框的类型。 ButtonsType.Close指定要在对话框中显示的按钮。 最后一个参数是显示的消息。 该对话框使用Run()方法显示。 程序员还必须调用Destroy()Hide()方法。

GTK# 中的对话框 - 图1

GTK# 中的对话框 - 图2

GTK# 中的对话框 - 图3

GTK# 中的对话框 - 图4

AboutDialog

AboutDialog显示有关应用的信息。 AboutDialog可以显示徽标,应用名称,版本,版权,网站或许可证信息。 也有可能对作者,文档撰写者,翻译者和艺术家予以赞扬。

aboutdialog.cs

  1. using Gtk;
  2. using System;
  3. class SharpApp : Window {
  4. public SharpApp() : base("About")
  5. {
  6. SetDefaultSize(300, 270);
  7. SetPosition(WindowPosition.Center);
  8. DeleteEvent += delegate { Application.Quit(); } ;
  9. Button button = new Button("About");
  10. button.Clicked += OnClicked;
  11. Fixed fix = new Fixed();
  12. fix.Put(button, 20, 20);
  13. Add(fix);
  14. ShowAll();
  15. }
  16. void OnClicked(object sender, EventArgs args)
  17. {
  18. AboutDialog about = new AboutDialog();
  19. about.ProgramName = "Battery";
  20. about.Version = "0.1";
  21. about.Copyright = "(c) Jan Bodnar";
  22. about.Comments = @"Battery is a simple tool for
  23. battery checking";
  24. about.Website = "http://www.zetcode.com";
  25. about.Logo = new Gdk.Pixbuf("battery.png");
  26. about.Run();
  27. about.Destroy();
  28. }
  29. public static void Main()
  30. {
  31. Application.Init();
  32. new SharpApp();
  33. Application.Run();
  34. }
  35. }

该代码示例使用具有某些功能的AboutDialog

  1. AboutDialog about = new AboutDialog();

我们创建一个AboutDialog

  1. about.ProgramName = "Battery";
  2. about.Version = "0.1";
  3. about.Copyright = "(c) Jan Bodnar";

通过设置对话框的属性,我们指定名称,版本和版权。

  1. about.Logo = new Gdk.Pixbuf("battery.png");

此行创建徽标。

GTK# 中的对话框 - 图5

图:AboutDialog

FontSelectionDialog

FontSelectionDialog是用于选择字体的对话框。 它通常用于进行一些文本编辑或格式化的应用中。

fontdialog.cs

  1. using Gtk;
  2. using System;
  3. class SharpApp : Window {
  4. Label label;
  5. public SharpApp() : base("Font Selection Dialog")
  6. {
  7. SetDefaultSize(300, 220);
  8. SetPosition(WindowPosition.Center);
  9. DeleteEvent += delegate { Application.Quit(); } ;
  10. label = new Label("The only victory over love is flight.");
  11. Button button = new Button("Select font");
  12. button.Clicked += OnClicked;
  13. Fixed fix = new Fixed();
  14. fix.Put(button, 100, 30);
  15. fix.Put(label, 30, 90);
  16. Add(fix);
  17. ShowAll();
  18. }
  19. void OnClicked(object sender, EventArgs args)
  20. {
  21. FontSelectionDialog fdia = new FontSelectionDialog("Select font name");
  22. fdia.Response += delegate (object o, ResponseArgs resp) {
  23. if (resp.ResponseId == ResponseType.Ok) {
  24. Pango.FontDescription fontdesc =
  25. Pango.FontDescription.FromString(fdia.FontName);
  26. label.ModifyFont(fontdesc);
  27. }
  28. };
  29. fdia.Run();
  30. fdia.Destroy();
  31. }
  32. public static void Main()
  33. {
  34. Application.Init();
  35. new SharpApp();
  36. Application.Run();
  37. }
  38. }

在代码示例中,我们有一个按钮和一个标签。 单击按钮显示FontSelectionDialog

  1. FontSelectionDialog fdia = new FontSelectionDialog("Select font name");

我们创建了FontSelectionDialog.

  1. fdia.Response += delegate (object o, ResponseArgs resp) {
  2. if (resp.ResponseId == ResponseType.Ok) {
  3. Pango.FontDescription fontdesc = Pango.FontDescription.FromString(fdia.FontName);
  4. label.ModifyFont(fontdesc);
  5. }
  6. };

如果单击“确定”按钮,则标签小部件的字体将更改为我们在对话框中选择的字体。

GTK# 中的对话框 - 图6

图:FontSelectionDialog

ColorSelectionDialog

ColorSelectionDialog是用于选择颜色的对话框。

colordialog.cs

  1. using Gtk;
  2. using System;
  3. class SharpApp : Window {
  4. Label label;
  5. public SharpApp() : base("Color Dialog")
  6. {
  7. SetDefaultSize(300, 220);
  8. SetPosition(WindowPosition.Center);
  9. DeleteEvent += delegate { Application.Quit(); } ;
  10. label = new Label("The only victory over love is flight.");
  11. Button button = new Button("Select color");
  12. button.Clicked += OnClicked;
  13. Fixed fix = new Fixed();
  14. fix.Put(button, 100, 30);
  15. fix.Put(label, 30, 90);
  16. Add(fix);
  17. ShowAll();
  18. }
  19. void OnClicked(object sender, EventArgs args)
  20. {
  21. ColorSelectionDialog cdia = new ColorSelectionDialog("Select color");
  22. cdia.Response += delegate (object o, ResponseArgs resp) {
  23. if (resp.ResponseId == ResponseType.Ok) {
  24. label.ModifyFg(StateType.Normal, cdia.ColorSelection.CurrentColor);
  25. }
  26. };
  27. cdia.Run();
  28. cdia.Destroy();
  29. }
  30. public static void Main()
  31. {
  32. Application.Init();
  33. new SharpApp();
  34. Application.Run();
  35. }
  36. }

该示例与上一个示例非常相似。 这次我们更改标签的颜色。

  1. ColorSelectionDialog cdia = new ColorSelectionDialog("Select color");

我们创建ColorSelectionDialog

  1. cdia.Response += delegate (object o, ResponseArgs resp) {
  2. if (resp.ResponseId == ResponseType.Ok) {
  3. label.ModifyFg(StateType.Normal, cdia.ColorSelection.CurrentColor);
  4. }
  5. };

如果用户按下 OK,我们将获得颜色并修改标签的颜色。

GTK# 中的对话框 - 图7

图:颜色 electionDialog

在 GTK# 教程的这一部分中,我们讨论了对话框。