GTK# 中的对话框
最后修改于 2023 年 10 月 18 日
在本 GTK# 编程教程中,我们将介绍对话框。
对话框窗口或对话框是大多数现代 GUI 应用程序不可或缺的一部分。 对话框被定义为两个人或更多人之间的对话。 在计算机应用程序中,对话框是用于与应用程序“对话”的窗口。 对话框用于输入数据、修改数据、更改应用程序设置等。 对话框是用户与计算机程序之间重要的沟通方式。
消息对话框
消息对话框是方便的对话框,用于向应用程序的用户提供消息。 消息由文本和图像数据组成。
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();
}
}
在我们的示例中,我们展示了四种消息对话框。 信息、警告、问题和错误消息对话框。
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();
};
如果我们单击信息按钮,将显示信息对话框。 MessageType.Info 指定对话框的类型。 ButtonsType.Close 指定要在对话框中显示的按钮。 最后一个参数是要显示的消息。 对话框使用 Run 方法显示。 程序员还必须调用 Destroy 或 Hide 方法。
AboutDialog
AboutDialog 显示有关应用程序的信息。 AboutDialog 可以显示徽标、应用程序名称、版本、版权、网站或许可证信息。 还可以向作者、文档编写者、翻译者和艺术家表示感谢。
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 及其一些功能。
AboutDialog about = new AboutDialog();
我们创建一个 AboutDialog。
about.ProgramName = "Battery"; about.Version = "0.1"; about.Copyright = "(c) Jan Bodnar";
通过设置对话框的属性,我们指定名称、版本和版权。
about.Logo = new Gdk.Pixbuf("battery.png");
此行创建一个徽标。
FontSelectionDialog
FontSelectionDialog 是一个用于选择字体的对话框。 它通常用于执行一些文本编辑或格式化的应用程序中。
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。
FontSelectionDialog fdia = new FontSelectionDialog("Select font name");
我们创建 FontSelectionDialog。
fdia.Response += delegate (object o, ResponseArgs resp) {
if (resp.ResponseId == ResponseType.Ok) {
Pango.FontDescription fontdesc = Pango.FontDescription.FromString(fdia.FontName);
label.ModifyFont(fontdesc);
}
};
如果我们单击确定按钮,标签小部件的字体将更改为我们在对话框中选择的字体。
ColorSelectionDialog
ColorSelectionDialog 是一个用于选择颜色的对话框。
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();
}
}
该示例与前一个示例非常相似。 这次我们更改标签的颜色。
ColorSelectionDialog cdia = new ColorSelectionDialog("Select color");
我们创建 ColorSelectionDialog。
cdia.Response += delegate (object o, ResponseArgs resp) {
if (resp.ResponseId == ResponseType.Ok) {
label.ModifyFg(StateType.Normal, cdia.ColorSelection.CurrentColor);
}
};
如果用户按下了确定,我们将获取颜色并修改标签的颜色。
在本 GTK# 教程中,我们讨论了对话框。