#include <gtk/gtk.h>
#include <stdio.h>
#define WIDTH 2002
#define HIGHT 1412
const gchar *ip;
const gchar *mask;
const gchar *gateway;
GtkWidget *create_image(const gchar *path)
{
GtkWidget *image = gtk_image_new_from_file(path);
return image;
}
void change_background(GtkWidget *widget, int w, int h, const gchar *path)
{
//1.允许窗口可以绘图
gtk_widget_set_app_paintable(widget, TRUE);
gtk_widget_realize(widget);
/* 更改背景图时,图片会重叠
* 这时要手动调用下面的函数,让窗口绘图区域失效,产生窗口重绘制事件(即 expose 事件)。
*/
gtk_widget_queue_draw(widget);
GdkPixbuf *src = gdk_pixbuf_new_from_file(path, NULL);
GdkPixbuf *dst = gdk_pixbuf_scale_simple(src,w,h,GDK_INTERP_BILINEAR);
/* 创建pixmap图像;
* NULL:不需要蒙版;
* 123: 0~255,透明到不透明
*/
GdkPixmap *pixmap = NULL;
gdk_pixbuf_render_pixmap_and_mask(dst,&pixmap,NULL,128);
// 通过pixmap给widget设置一张背景图,最后一个参数必须为: FASL
gdk_window_set_back_pixmap(widget->window,pixmap,FALSE);
g_object_unref(src);
g_object_unref(dst);
g_object_unref(pixmap);
return;
}
/--------------------------得到行编辑的内容---------------------/
void deal1(GtkButton *button,gpointer user_data)
{
ip=gtk_entry_get_text((GtkEntry *)user_data);
}
void deal2(GtkButton *button,gpointer user_data)
{
mask=gtk_entry_get_text((GtkEntry *)user_data);
}
void deal3(GtkButton *button,gpointer user_data)
{
gateway=gtk_entry_get_text((GtkEntry *)user_data);
}
void show_test_window(GtkWidget *widget,gpointer data)
{
printf("%s %s %s \n", ip, mask, gateway);
}
int main(int argc,char *argv[])
{
//1.gtk初始化
gtk_init(&argc,&argv);
//2.创建GtkBuilder对象,GtkBuilder在<gtk/gtk.h>声明
GtkBuilder *builder = gtk_builder_new();
//3.读取test.glade文件的信息,保存在builder中
if ( !gtk_builder_add_from_file(builder,"test.glade", NULL)) {
printf("connot load file!");
}
//4.获取窗口指针,注意"window1"要和glade里面的标签名词匹配
GtkWidget *window = GTK_WIDGET(gtk_builder_get_object(builder,"window2"));
GtkWidget *fixed = GTK_WIDGET(gtk_builder_get_object(builder,"holefixed")); /* 一层固定容器 */
GtkWidget *fixed1 = GTK_WIDGET(gtk_builder_get_object(builder,"fixed1")); /* 二层固定容器 */
GtkWidget *fixed2 = GTK_WIDGET(gtk_builder_get_object(builder,"staticFixed1")); /* 二层固定容器 */
GdkScreen* screen;
gint width, height;
screen = gdk_screen_get_default();
width = gdk_screen_get_width(screen);
height = gdk_screen_get_height(screen);
gtk_window_set_default_size(GTK_WINDOW(window), width, height);
change_background(window, width, height, "3.jpg");
/*给button添加照片
GtkWidget *image = gtk_image_new_from_file("5.jpg");
GtkButton *button = GTK_BUTTON(gtk_builder_get_object(builder, "button1"));
gtk_button_set_image(GTK_BUTTON(button), image);
gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
*/
/* 给固定容器添加照片时,照片大小和容器宽、高要一致
GtkWidget *image1 = create_image("./7.jpg");
gtk_fixed_put(GTK_FIXED(fixed1), image1, 0, 0); //添加窗体背景图片
*/
/*容器中添加按钮时,先调整好位置,记录坐标,然后再设置
GtkWidget *button10 = gtk_button_new_with_label("button1");
//将button1添加到固定容器fixed中
gtk_fixed_put(GTK_FIXED(fixed1), button10, 73,63);
*/
GtkButton *button7 = GTK_BUTTON(gtk_builder_get_object(builder, "button7"));/*静态IP确定按钮*/
GtkWidget *entry7 = GTK_WIDGET(gtk_builder_get_object(builder,"entry7"));
GtkWidget *entry8 = GTK_WIDGET(gtk_builder_get_object(builder,"entry8"));
GtkWidget *entry9 = GTK_WIDGET(gtk_builder_get_object(builder,"entry9"));
//确定按钮回调函数
g_signal_connect(button7,"clicked",G_CALLBACK(deal1),entry7);
g_signal_connect(button7,"clicked",G_CALLBACK(deal2),entry8);
g_signal_connect(button7,"clicked",G_CALLBACK(deal3),entry9);
g_signal_connect(button7,"clicked",G_CALLBACK(show_test_window), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}