我是靠谱客的博主 勤劳银耳汤,最近开发中收集的这篇文章主要介绍GtkDialog(4),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

GtkDialog(1)
GtkDialog(2)
GtkDialog(3)

这篇将学习记录以下相关函数。

GtkWidget *
gtk_dialog_new_with_buttons (const gchar *title,
                             GtkWindow *parent,
                             GtkDialogFlags flags,
                             const gchar *first_button_text,
                             ...);
void
gtk_dialog_add_action_widget (GtkDialog *dialog,
                              GtkWidget *child,
                              gint response_id);
void
gtk_dialog_set_default_response (GtkDialog *dialog,
                                 gint response_id);
void
gtk_dialog_set_response_sensitive (GtkDialog *dialog,
                                   gint response_id,
                                   gboolean setting);

例子

#include <gtk/gtk.h>

static GtkWidget *create_window(const gint, const gint);

void button_clicked_cb(GtkWindow *window)
{
	GtkWidget *dialog;
	GtkWidget *button;
	gint response_id;

	/// *** dialog
	GtkDialogFlags flags = GTK_DIALOG_MODAL;
	dialog = gtk_dialog_new_with_buttons("Dialog", window, flags, "OK", 1, "Cancel", 2, NULL);
	gtk_widget_set_size_request(dialog, 200, 300);
	/// *** button  按下Alt + c 可触发按钮
	button = gtk_button_new_with_mnemonic("_QUIT");
	/// *** 将button添加到dialog
	gtk_dialog_add_action_widget(GTK_DIALOG(dialog), button, 3);
	/// *** 设置默认选项按钮。按下Enter键即可触发
	gtk_dialog_set_default_response(GTK_DIALOG(dialog), 1);
	/// *** 将某个response_id对应的按钮设置为敏感/不敏感
	gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), 2, FALSE);

	gtk_widget_show_all(dialog);

	response_id = gtk_dialog_run(GTK_DIALOG(dialog));
	switch (response_id)
	{
	case 1:
		g_print("The OK button was clickedn");
		gtk_widget_destroy(dialog);
		break;
	case 2:
		g_print("The Cancel button was clickedn");
		break;
	case 3:
		g_print("The QUIT button was clickedn");
		gtk_widget_destroy(GTK_WIDGET(window));
		break;
	default:
		break;
	}
}

int main(int argc, char *argv[])
{
	gtk_init(&argc, &argv);

	GtkWidget *window;
	GtkWidget *button;

	/// *** Create a Window
	window = create_window(400, 500);
	/// *** Create a button
	button = gtk_button_new_with_mnemonic("_Open the dialog");
	g_object_set(button,
		     "margin", 50,
		     "parent", window,
		     NULL);
	g_signal_connect_swapped(button, "clicked", G_CALLBACK(button_clicked_cb), window);

	gtk_widget_show_all(window);
	gtk_main();
}

static GtkWidget *create_window(const gint w, const gint h)
{
	GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
	gtk_window_set_default_size(GTK_WINDOW(window), w, h);
	gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
	gtk_window_set_title(GTK_WINDOW(window), "Hello GTK3");
	gtk_container_set_border_width(GTK_CONTAINER(window), 50);

	return window;
}
  • 运行效果

在这里插入图片描述

最后

以上就是勤劳银耳汤为你收集整理的GtkDialog(4)的全部内容,希望文章能够帮你解决GtkDialog(4)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(32)

评论列表共有 0 条评论

立即
投稿
返回
顶部