打开glade 放置一个窗口,然后在窗口上放置一个按钮 然后放置一个信息对话框 在对话框上面放置两个按钮 保存为dialog.glade 如图所示
打开code::blocks 输入以下代码 代码都比较简单 /* 作者:bailangcn 时间:2012/3/21 作者主页:bailangcn.tk */ #include <gtk/gtk.h> void show_dialog(void)
{
GtkWidget *dialog;
GtkWidget *builder;
GtkResponseType response;
builder = gtk_builder_new();
gtk_builder_add_from_file(builder, "dialog.glade", NULL);
dialog = GTK_WIDGET(gtk_builder_get_object(builder,"messagedialog"));
response = gtk_dialog_run (GTK_DIALOG (dialog));
if(response==GTK_RESPONSE_OK)
{
g_printf("ok\n");
}
else if(response==GTK_RESPONSE_CANCEL)
{
g_printf("CANCEL\n");
}
gtk_widget_destroy(GTK_WIDGET(dialog));
g_object_unref(G_OBJECT(builder));
} int main(int argc, char *argv[])
{
GtkBuilder *builder; gtk_init(&argc, &argv);
GtkWidget *window; GtkWidget *button1;
builder = gtk_builder_new();
gtk_builder_add_from_file(builder, "dialog.glade", NULL); window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
button1 = GTK_WIDGET(gtk_builder_get_object(builder,"button1"));
g_signal_connect(G_OBJECT(window),"delete_event",G_CALLBACK(gtk_main_quit),NULL);
g_signal_connect(G_OBJECT(button1),"pressed",G_CALLBACK(show_dialog),NULL);
// gtk_builder_connect_signals(builder, NULL);//自动关联已在 UI 文件配置好的信号 如果你不在galde文件里面配置信号这行代码可以不用
g_object_unref(G_OBJECT(builder));
gtk_widget_show_all(window);
gtk_main();
return 0;
} 运行后如下:
解释一下:在gtkdialog.h里面有这样子的定义 typedef enum
{
/* GTK returns this if a response widget has no response_id,
* or if the dialog gets programmatically hidden or destroyed.
*/
GTK_RESPONSE_NONE = -1, /* GTK won't return these unless you pass them in
* as the response for an action widget. They are
* for your convenience.
*/
GTK_RESPONSE_REJECT = -2,
GTK_RESPONSE_ACCEPT = -3, /* If the dialog is deleted. */
GTK_RESPONSE_DELETE_EVENT = -4, /* These are returned from GTK dialogs, and you can also use them
* yourself if you like.
*/
GTK_RESPONSE_OK = -5,
GTK_RESPONSE_CANCEL = -6,
GTK_RESPONSE_CLOSE = -7,
GTK_RESPONSE_YES = -8,
GTK_RESPONSE_NO = -9,
GTK_RESPONSE_APPLY = -10,
GTK_RESPONSE_HELP = -11
} GtkResponseType;
|