⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex24_1.c

📁 linux 高级编程的例子源码,包括了本书上的所有代码
💻 C
字号:
/* filename: ex24_1.c */
# include <gtk/gtk.h>

/* close the dialog window */
void CloseDialog(GtkWidget *widget,gpointer data)
{
	gtk_widget_destroy(GTK_WIDGET(data));
}

/* transfer function MessageBox to create a dialog window with "确定"button */
void YesButton(void)
{
        MessageBox("Good","She loves you too!",1);
        return ;
 }

/* transfer function MessageBox to create a dialog window with "ok"button */
void NoButton(void)
 {
	 MessageBox("Terrible","She doesn't love you too!",0);
         return ;
 }

/* this function to create a dialog window */
void MessageBox(gchar *caption,
              gchar  *message,gint IsChinese)
{
  GtkWidget *dialog_window;
  GtkWidget *button;
  GtkWidget *label;
  
  g_return_if_fail (caption!= NULL);
  g_return_if_fail (message!= NULL);

  dialog_window=gtk_dialog_new();
  gtk_window_set_title(GTK_WINDOW(dialog_window),caption);
  gtk_signal_connect(GTK_OBJECT(dialog_window),"destroy",
                    GTK_SIGNAL_FUNC(CloseDialog),dialog_window);			
  gtk_container_border_width(GTK_CONTAINER(dialog_window),5);
   
  /* create "Ok" button */
  if (!IsChinese)
      button=gtk_button_new_with_label("Ok");
  else
      button=gtk_button_new_with_label("确定");

  /* let we can press Enter to instead of pressing button */
  GTK_WIDGET_SET_FLAGS(button,GTK_CAN_DEFAULT);
  /* add it to the dialog box's action area */
  gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog_window)->action_area),
	                 button,TRUE,TRUE,0);
  gtk_widget_show(button);
  /* make the "ok" button as a default button */
  gtk_widget_grab_default(button);
  gtk_signal_connect(GTK_OBJECT(button),"clicked",
                    GTK_SIGNAL_FUNC(CloseDialog),dialog_window);			
  /* creat a label */
	label=gtk_label_new(message);
	gtk_misc_set_padding(GTK_MISC(label),10,10);
  /* add it to the dialog box's vbox area */
	gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog_window)->vbox),
	                 label,TRUE,TRUE,0);
  gtk_widget_show(label);
  gtk_widget_show(dialog_window);
  /* only this dialog window can be done now */
  gtk_grab_add(dialog_window);
  return ;
				
}
  	
int main(int argc, char *argv[])
{
  GtkWidget *dialog_window;
  GtkWidget *button1; 
  GtkWidget *button2; 
  GtkWidget *label; 
  
  gtk_init(&argc, &argv);
  /* create a dialog window */
  dialog_window=gtk_dialog_new();
  gtk_window_set_title(GTK_WINDOW(dialog_window),"test dialog");
  gtk_signal_connect(GTK_OBJECT(dialog_window),"destroy",
                    GTK_SIGNAL_FUNC(gtk_main_quit),NULL);			
  gtk_container_border_width(GTK_CONTAINER(dialog_window),10);
  
  /* create "Yes" and "No" buttons */
  button1=gtk_button_new_with_label("Yes");
  button2=gtk_button_new_with_label("No");
 
  /* let we can press Enter to instead of pressing button */
  GTK_WIDGET_SET_FLAGS(button1,GTK_CAN_DEFAULT);
  GTK_WIDGET_SET_FLAGS(button2,GTK_CAN_DEFAULT);
  
  /* add the buttons to the dialog action_area */
  gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog_window)->action_area),
	                 button1,TRUE,TRUE,0);
  gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog_window)->action_area),
	                 button2,TRUE,TRUE,0);

  /* click "Yes" button to transfer function YesButton */
  gtk_signal_connect(GTK_OBJECT(button1),"clicked",
                    GTK_SIGNAL_FUNC(YesButton),NULL);		
  /* click "No" button to transfer function NoButton */
  gtk_signal_connect(GTK_OBJECT(button2),"clicked",
                    GTK_SIGNAL_FUNC(NoButton),NULL);			
 
  gtk_widget_show(button1);
  gtk_widget_show(button2);
  /* make button1 as a default button */
  gtk_widget_grab_default(button1);
  
  /* create a label with message */
  label=gtk_label_new("Do you love her?");
  gtk_misc_set_padding(GTK_MISC(label),10,10);
  /* add the label to the dialog box */
  gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog_window)->vbox),
	                 label,TRUE,TRUE,0);
  gtk_widget_show(label);
  gtk_widget_show(dialog_window);
  /* only this dialog window can be done now */
  gtk_grab_add(dialog_window);
 
  gtk_main();
  return(0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -