该楼层疑似违规已被系统折叠隐藏此楼查看此楼
#include
#include
#define NAME "root"
#define PAWD "123456"
struct login_widget
{
GtkWidget *window;
GtkWidget *label_tips;
GtkWidget *button_login;
GtkWidget *button_exit;
GtkWidget *entry_username;
GtkWidget *entry_password;
GtkWidget *label_username;
GtkWidget *label_password;
GtkWidget *vbox;
GtkWidget *hbox_username;
GtkWidget *hbox_password;
GtkWidget *hbox_button;
}wgt;
void login(GtkWidget *widget,gpointer data)
{
gchar *name;
gchar *pawd;
struct login_widget *wgt;
wgt=(struct login_widget*)data;
name=gtk_entry_get_text(GTK_ENTRY(wgt->entry_username));
pawd=gtk_entry_get_text(GTK_ENTRY(wgt->entry_password));
if(!strcmp(name,NAME)&&!strcmp(pawd,PAWD))
gtk_label_set_text(GTK_LABEL(wgt->label_tips),
"Welcome to GTK!");
else
gtk_label_set_text(GTK_LABEL(wgt->label_tips),
"Failed to login!");
}
void init_login_widget()
{
wgt.window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
wgt.label_tips=gtk_label_new("Welcome,please login");
wgt.button_login=gtk_button_new_with_label("login");
wgt.button_exit=gtk_button_new_with_label("exit");
wgt.entry_username=gtk_entry_new_with_max_length(10);
wgt.entry_password=gtk_entry_new_with_max_length(10);
wgt.label_username=gtk_label_new("User name:");
wgt.label_password=gtk_label_new("Password:");
wgt.vbox=gtk_vbox_new(FALSE,20);
wgt.hbox_username=gtk_hbox_new(FALSE,20);
wgt.hbox_password=gtk_hbox_new(FALSE,20);
wgt.hbox_button=gtk_hbox_new(FALSE,20);
gtk_window_set_title(GTK_WINDOW(wgt.window),"Login");
gtk_window_set_resizable(GTK_WINDOW(wgt.window),FALSE);
gtk_box_pack_start(GTK_BOX(wgt.hbox_username),
wgt.label_username,TRUE,FALSE,10);
gtk_box_pack_start(GTK_BOX(wgt.hbox_username),
wgt.entry_username,TRUE,FALSE,10);
gtk_box_pack_start(GTK_BOX(wgt.hbox_password),
wgt.label_password,TRUE,FALSE,10);
gtk_box_pack_start(GTK_BOX(wgt.hbox_password),
wgt.entry_password,TRUE,FALSE,10);
gtk_box_pack_start(GTK_BOX(wgt.hbox_button),
wgt.button_login,TRUE,FALSE,10);
gtk_box_pack_start(GTK_BOX(wgt.hbox_button),
wgt.button_exit,TRUE,FALSE,10);
gtk_box_pack_start(GTK_BOX(wgt.vbox),wgt.label_tips,
TRUE,FALSE,10);
gtk_box_pack_start(GTK_BOX(wgt.vbox),wgt.hbox_username,
TRUE,FALSE,10);
gtk_box_pack_start(GTK_BOX(wgt.vbox),wgt.hbox_password,
TRUE,FALSE,10);
gtk_box_pack_start(GTK_BOX(wgt.vbox),wgt.hbox_button,
TRUE,FALSE,10);
gtk_entry_set_visibility(GTK_ENTRY(wgt.entry_password),
FALSE);
gtk_entry_set_invisible_char(GTK_ENTRY(wgt.entry_password),
'*');
g_signal_connect(GTK_OBJECT(wgt.button_login),"clicked",
GTK_SIGNAL_FUNC(login),&wgt);
g_signal_connect(GTK_OBJECT(wgt.window),"destroy",
G_CALLBACK(gtk_main_quit),NULL);
g_signal_connect(GTK_OBJECT(wgt.button_exit),"clicked",
G_CALLBACK(gtk_main_quit),NULL);
gtk_container_add(GTK_CONTAINER(wgt.window),wgt.vbox);
gtk_widget_show_all(wgt.window);
}
int main(int argc,char **argv)
{
gtk_init(&argc,&argv);
init_login_widget();
gtk_main();
return 0;
}