使用GTK - 绘制按钮和消息框

  1. #include <gtk/gtk.h>
  2. gint count = 1;
  3. // 按下按钮时弹出响应框
  4. void on_button_clicked (GtkWidget *button, gpointer userdata)
  5. {
  6.      GtkWidget* dialog;
  7.      gchar buff[50];
  8.      sprintf(buff, "counter: %d", count);
  9.      dialog = gtk_message_dialog_new(
  10.             NULL,
  11.             GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
  12.             GTK_MESSAGE_INFO,
  13.             GTK_BUTTONS_OK,
  14.             buff); 
  15.      gtk_dialog_run(GTK_DIALOG(dialog));
  16.      gtk_widget_destroy(dialog);
  17.      // g_print在WINDOWS不起作用 
  18.      //g_print("Hello, this is a GTK test.");
  19.      //g_print("%d/n", (gint)userdata);
  20.      //g_print("%d/n", count);
  21.      count = count + 1;
  22. }
  23. int main(int argc,char *argv[])
  24. {
  25.     GtkWidget *window;
  26.     GtkWidget *button;
  27.     gtk_init(&argc,&argv);
  28.     window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
  29.     
  30.     g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), &window);
  31.     gtk_window_set_title(GTK_WINDOW(window), "Hello GTK");
  32.     gtk_window_set_default_size(GTK_WINDOW(window), 500, 100);
  33.     gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  34.     gtk_container_set_border_width(GTK_CONTAINER(window), 40);
  35.     button = gtk_button_new_with_label("Press to show a string on the terminal");
  36.     g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(on_button_clicked), (gpointer)count);
  37.     gtk_container_add(GTK_CONTAINER(window), button);
  38.     gtk_widget_show(button);
  39.     gtk_widget_show(window);
  40.     gtk_main();
  41.     return FALSE;
  42. }

你可能感兴趣的:(使用GTK - 绘制按钮和消息框)