白浪 发表于 2013-9-19 15:35:30

glade3教程(五)--对话框

打开glade放置一个窗口,然后在窗口上放置一个按钮   然后放置一个信息对话框在对话框上面放置两个按钮保存为dialog.glade如图所示http://www.bailangcn.tk/content/uploadfile/201203/thum-8266e4bfeda1bd42d8f9794eb4ea0a1320120321153651.png


http://www.bailangcn.tk/content/uploadfile/201203/thum-2fc30b834379e7cde1af4a5107a50fe920120321153713.pnghttp://www.bailangcn.tk/content/uploadfile/201308/dialog1.png http://www.bailangcn.tk/content/uploadfile/201308/dialog2.png
打开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;
}运行后如下:http://www.bailangcn.tk/content/uploadfile/201203/thum-85920d0ea71eaeaaaa14b4f8b903c8e220120321154110.png

解释一下:在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;
本文始发于:http://www.bailangcn.tk/?post=20转载请注明

白浪 发表于 2013-9-19 15:39:34

本来我是一次性将控件从glade文件里面提取出来 ,然后运行的时候再显示,用完再隐藏起来,经测试发现第一次运行很正常,但是隐藏之后再显示出来就有些显示不出来了,不知道是什么原因,最后去国外的英文社区去看,发现蛮多人也是这样子的,有大神说在显示控件函数中再提取就好了,经测试发现的确是正常的,这样子做有什么不良的后果吗?有知道的人请赐教
页: [1]
查看完整版本: glade3教程(五)--对话框