GTK开发之文本框Entry,按钮Button,容器hbox,vbox,fixed的使用

/*
 * main2.cc
 *
 *   Created on: 2010-2-5
 *  Description:
 *    Copyright: 2010 @ ICT Li Heyuan
 */

#include <gtk/gtk.h>

typedef struct {
	GtkWidget *user, *pass;
} pointer;

void closeApp(GtkWidget *window, gpointer data) {
	gtk_main_quit();
}

void button_login(GtkWidget *button, gpointer data) {

	if ((void*) data == NULL) {
		return;
	}

	GtkWidget * user = ((pointer*) data)->user;
	GtkWidget * pass = ((pointer*) data)->pass;

	const char* user_text = gtk_entry_get_text(GTK_ENTRY(user));
	const char* pass_text = gtk_entry_get_text(GTK_ENTRY(pass));

	printf("Username %s,Password %s.\n", user_text, pass_text);
}

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

	GtkWidget *window;
	GtkWidget *hbox1, *hbox2, *vbox;
	GtkWidget *fixed;
	GtkWidget *label1, *label2;
	GtkWidget *text1, *text2;
	GtkWidget *button;

	gtk_init(&argc, &argv);

	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title(GTK_WINDOW(window), "I'm title"); //设置窗体标题
	gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); //设置中心
	gtk_window_set_default_size(GTK_WINDOW(window), 300, 150);
	g_signal_connect(GTK_OBJECT(window), "destroy", GTK_SIGNAL_FUNC(closeApp),
			NULL);

	label1 = gtk_label_new("Username : "); //创建两个Label
	label2 = gtk_label_new("Password : ");

	text1 = gtk_entry_new(); //创建两个entry文本框
	text2 = gtk_entry_new();
	gtk_entry_set_visibility(GTK_ENTRY(text2), FALSE); //设置密码区域

	button = gtk_button_new_with_label("Login");//一个按钮
	pointer p = { text1, text2 };
	g_signal_connect(GTK_OBJECT(button), "clicked",
			GTK_SIGNAL_FUNC(button_login), &p);

	hbox1 = gtk_hbox_new(TRUE, 5); //第一行,用户名的Label和Entry
	gtk_box_pack_start(GTK_BOX(hbox1), label1, TRUE, FALSE, 5);
	gtk_box_pack_start(GTK_BOX(hbox1), text1, TRUE, FALSE, 5);

	hbox2 = gtk_hbox_new(TRUE, 5);//第二行,密码的Label和Entry
	gtk_box_pack_start(GTK_BOX(hbox2), label2, TRUE, FALSE, 5);
	gtk_box_pack_start(GTK_BOX(hbox2), text2, TRUE, FALSE, 5);

	fixed = gtk_fixed_new();//一个fixed的容器,放入button
	gtk_fixed_put(GTK_FIXED(fixed), button, 200, 0);

	vbox = gtk_vbox_new(FALSE, 20); //把vbox1和vbox2放入hbox
	gtk_box_pack_start(GTK_BOX(vbox), hbox1, FALSE, FALSE, 5);
	gtk_box_pack_start(GTK_BOX(vbox), hbox2, FALSE, FALSE, 5);
	gtk_box_pack_start(GTK_BOX(vbox), fixed, FALSE, FALSE, 5);

	gtk_container_add(GTK_CONTAINER(window), vbox);

	gtk_widget_show_all(window);
	gtk_main();

	return 0;
}

2 thoughts on “GTK开发之文本框Entry,按钮Button,容器hbox,vbox,fixed的使用

Leave a Reply

Your email address will not be published.