# GTK# 中的对话框 > 原文: [http://zetcode.com/gui/gtksharp/dialogs/](http://zetcode.com/gui/gtksharp/dialogs/) 在 GTK# 编程教程的这一部分中,我们将介绍对话框。 对话框窗口或对话框是大多数现代 GUI 应用必不可少的部分。 对话被定义为两个或更多人之间的对话。 在计算机应用中,对话框是一个窗口,用于与应用“对话”。 对话框用于输入数据,修改数据,更改应用设置等。对话框是用户与计算机程序之间进行通信的重要手段。 ## `MessageDialog` 消息对话框是方便的对话框,可向应用的用户提供消息。 该消息包含文本和图像数据。 `messages.cs` ```cs using Gtk; using System; class SharpApp : Window { public SharpApp() : base("Messages") { SetDefaultSize(250, 100); SetPosition(WindowPosition.Center); DeleteEvent += delegate { Application.Quit(); }; Table table = new Table(2, 2, true); Button info = new Button("Information"); Button warn = new Button("Warning"); Button ques = new Button("Question"); Button erro = new Button("Error"); info.Clicked += delegate { MessageDialog md = new MessageDialog(this, DialogFlags.DestroyWithParent, MessageType.Info, ButtonsType.Close, "Download completed"); md.Run(); md.Destroy(); }; warn.Clicked += delegate { MessageDialog md = new MessageDialog(this, DialogFlags.DestroyWithParent, MessageType.Warning, ButtonsType.Close, "Unallowed operation"); md.Run(); md.Destroy(); }; ques.Clicked += delegate { MessageDialog md = new MessageDialog(this, DialogFlags.DestroyWithParent, MessageType.Question, ButtonsType.Close, "Are you sure to quit?"); md.Run(); md.Destroy(); }; erro.Clicked += delegate { MessageDialog md = new MessageDialog (this, DialogFlags.DestroyWithParent, MessageType.Error, ButtonsType.Close, "Error loading file"); md.Run(); md.Destroy(); }; table.Attach(info, 0, 1, 0, 1); table.Attach(warn, 1, 2, 0, 1); table.Attach(ques, 0, 1, 1, 2); table.Attach(erro, 1, 2, 1, 2); Add(table); ShowAll(); } public static void Main() { Application.Init(); new SharpApp(); Application.Run(); } } ``` 在我们的示例中,我们将显示四种消息对话框。 信息,警告,问题和错误消息对话框。 ```cs Button info = new Button("Information"); Button warn = new Button("Warning"); Button ques = new Button("Question"); Button erro = new Button("Error"); ``` 我们有四个按钮。 这些按钮中的每个按钮都会显示不同类型的消息对话框。 ```cs info.Clicked += delegate { MessageDialog md = new MessageDialog(this, DialogFlags.DestroyWithParent, MessageType.Info, ButtonsType.Close, "Download completed"); md.Run(); md.Destroy(); }; ``` 如果单击信息按钮,将显示“信息”对话框。 `MessageType.Info`指定对话框的类型。 `ButtonsType.Close`指定要在对话框中显示的按钮。 最后一个参数是显示的消息。 该对话框使用`Run()`方法显示。 程序员还必须调用`Destroy()`或`Hide()`方法。 ![Information message dialog](img/ca313a9c2114c438adc8c5f2d12c3215.jpg) ![Warning message dialog](img/c7e72b3d4e85b9678fa1cd1a392464ad.jpg) ![Question message dialog](img/d385a0e54492d6da0ff12e65d8eed7c4.jpg) ![Error message dialog](img/624135cdcbf6c65a82de8105f0bb28b8.jpg) ## `AboutDialog` `AboutDialog`显示有关应用的信息。 `AboutDialog`可以显示徽标,应用名称,版本,版权,网站或许可证信息。 也有可能对作者,文档撰写者,翻译者和艺术家予以赞扬。 `aboutdialog.cs` ```cs using Gtk; using System; class SharpApp : Window { public SharpApp() : base("About") { SetDefaultSize(300, 270); SetPosition(WindowPosition.Center); DeleteEvent += delegate { Application.Quit(); } ; Button button = new Button("About"); button.Clicked += OnClicked; Fixed fix = new Fixed(); fix.Put(button, 20, 20); Add(fix); ShowAll(); } void OnClicked(object sender, EventArgs args) { AboutDialog about = new AboutDialog(); about.ProgramName = "Battery"; about.Version = "0.1"; about.Copyright = "(c) Jan Bodnar"; about.Comments = @"Battery is a simple tool for battery checking"; about.Website = "http://www.zetcode.com"; about.Logo = new Gdk.Pixbuf("battery.png"); about.Run(); about.Destroy(); } public static void Main() { Application.Init(); new SharpApp(); Application.Run(); } } ``` 该代码示例使用具有某些功能的`AboutDialog`。 ```cs AboutDialog about = new AboutDialog(); ``` 我们创建一个`AboutDialog`。 ```cs about.ProgramName = "Battery"; about.Version = "0.1"; about.Copyright = "(c) Jan Bodnar"; ``` 通过设置对话框的属性,我们指定名称,版本和版权。 ```cs about.Logo = new Gdk.Pixbuf("battery.png"); ``` 此行创建徽标。 ![AboutDialog](img/598a19d225e16a3bef76e447f5c0d046.jpg) 图:`AboutDialog` ## `FontSelectionDialog` `FontSelectionDialog`是用于选择字体的对话框。 它通常用于进行一些文本编辑或格式化的应用中。 `fontdialog.cs` ```cs using Gtk; using System; class SharpApp : Window { Label label; public SharpApp() : base("Font Selection Dialog") { SetDefaultSize(300, 220); SetPosition(WindowPosition.Center); DeleteEvent += delegate { Application.Quit(); } ; label = new Label("The only victory over love is flight."); Button button = new Button("Select font"); button.Clicked += OnClicked; Fixed fix = new Fixed(); fix.Put(button, 100, 30); fix.Put(label, 30, 90); Add(fix); ShowAll(); } void OnClicked(object sender, EventArgs args) { FontSelectionDialog fdia = new FontSelectionDialog("Select font name"); fdia.Response += delegate (object o, ResponseArgs resp) { if (resp.ResponseId == ResponseType.Ok) { Pango.FontDescription fontdesc = Pango.FontDescription.FromString(fdia.FontName); label.ModifyFont(fontdesc); } }; fdia.Run(); fdia.Destroy(); } public static void Main() { Application.Init(); new SharpApp(); Application.Run(); } } ``` 在代码示例中,我们有一个按钮和一个标签。 单击按钮显示`FontSelectionDialog`。 ```cs FontSelectionDialog fdia = new FontSelectionDialog("Select font name"); ``` 我们创建了`FontSelectionDialog.` ```cs fdia.Response += delegate (object o, ResponseArgs resp) { if (resp.ResponseId == ResponseType.Ok) { Pango.FontDescription fontdesc = Pango.FontDescription.FromString(fdia.FontName); label.ModifyFont(fontdesc); } }; ``` 如果单击“确定”按钮,则标签小部件的字体将更改为我们在对话框中选择的字体。 ![FontSelectionDialog](img/597ade8dd68b00ac413562671b89eb75.jpg) 图:`FontSelectionDialog` ## `ColorSelectionDialog` `ColorSelectionDialog`是用于选择颜色的对话框。 `colordialog.cs` ```cs using Gtk; using System; class SharpApp : Window { Label label; public SharpApp() : base("Color Dialog") { SetDefaultSize(300, 220); SetPosition(WindowPosition.Center); DeleteEvent += delegate { Application.Quit(); } ; label = new Label("The only victory over love is flight."); Button button = new Button("Select color"); button.Clicked += OnClicked; Fixed fix = new Fixed(); fix.Put(button, 100, 30); fix.Put(label, 30, 90); Add(fix); ShowAll(); } void OnClicked(object sender, EventArgs args) { ColorSelectionDialog cdia = new ColorSelectionDialog("Select color"); cdia.Response += delegate (object o, ResponseArgs resp) { if (resp.ResponseId == ResponseType.Ok) { label.ModifyFg(StateType.Normal, cdia.ColorSelection.CurrentColor); } }; cdia.Run(); cdia.Destroy(); } public static void Main() { Application.Init(); new SharpApp(); Application.Run(); } } ``` 该示例与上一个示例非常相似。 这次我们更改标签的颜色。 ```cs ColorSelectionDialog cdia = new ColorSelectionDialog("Select color"); ``` 我们创建`ColorSelectionDialog`。 ```cs cdia.Response += delegate (object o, ResponseArgs resp) { if (resp.ResponseId == ResponseType.Ok) { label.ModifyFg(StateType.Normal, cdia.ColorSelection.CurrentColor); } }; ``` 如果用户按下 OK,我们将获得颜色并修改标签的颜色。 ![ColorSelectionDialog](img/1acd3959cf7b03579ad481d88c057373.jpg) 图:颜色electionDialog 在 GTK# 教程的这一部分中,我们讨论了对话框。