1: 概述
是搜集别人的源代码, 自己修改了部分 ,主要目的是 应付学校的培训
GTK+ 实现的带背景的简易计算器:
/*gcc -o calc.c `pkg-config --cflags --libs gtk+-2.0`*/ #include <gtk/gtk.h> #include <stdlib.h> // 全局输入框 static GtkWidget *entry; gint count = 0; gdouble nn = 0; // 操作数一 gdouble mm = 0;// 操作数 二 gint s = 0; // 1 ++> + 2>>- 3>>* 4>>/ gboolean first = TRUE; gboolean have_dot = FALSE; // 存在点 gboolean have_result = FALSE;// 存在结果 gchar number[100]; // 清除所有参数 void clear_all() { gint i; nn = 0; mm = 0; s = 0; count = 0; first = TRUE; have_dot = FALSE; have_result = FALSE; for (i = 0; i < sizeof(number); i++) number[i] = '\0'; gtk_entry_set_text(GTK_ENTRY(entry), ""); } void on_clear_clicked(GtkButton *button, gpointer data) { clear_all(); } // 按钮数据处理函数 void on_num_clicked(GtkButton *button, gpointer data) { const gchar *num; gint i; if (have_result) clear_all(); if (count == 6) return; //限制运算位数 count++; // 获取按钮数据 num = gtk_button_get_label(GTK_BUTTON(button)); // 连接函数 i = g_strlcat(number, num, 100); // first 切换参数一 二开关 if (first) { nn = g_ascii_strtod(number, NULL);// 将字符串数组转为gdouble 数据类型 } else mm = g_ascii_strtod(number, NULL);//将字符串数组转为gdouble 数据类型 gtk_entry_set_text(GTK_ENTRY(entry), number); } void on_suan_clicked(GtkButton *button, gpointer data) { gint i; s = GPOINTER_TO_INT(data); // 第二个 参数 first = FALSE; count = 0; have_dot = FALSE; gtk_entry_set_text(GTK_ENTRY(entry), ""); // 清空缓存 for (i = 0; i < sizeof(number); i++) number[i] = '\0'; } void on_dot_clicked(GtkButton *button, gpointer data) { gint i; if (have_result) clear_all();// 存在结果 if (have_dot == FALSE) { have_dot = TRUE; i = g_strlcat(number, ".", 100); gtk_entry_set_text(GTK_ENTRY(entry), number); } } void on_eq_clicked(GtkButton *button, gpointer data) { gdouble numb; gchar num[100];// 缓冲数组 gchar *result; switch(s) { case 1: numb = nn + mm; break; case 2: numb = nn - mm; break; case 3: numb = nn * mm; break; case 4: numb = nn / mm; break; } result = g_ascii_dtostr(num, 100, numb); have_result = TRUE; gtk_entry_set_text(GTK_ENTRY(entry), result); } /* 功能: 设置背景图 * widget: 主窗口 * w, h: 图片的大小 * path: 图片路径 */ void chang_background(GtkWidget *widget, int w, int h, const gchar *path){ gtk_widget_set_app_paintable(widget, TRUE); //允许窗口可以绘图 gtk_widget_realize(widget); /* 更改背景图时,图片会重叠 * 这时要手动调用下面的函数,让窗口绘图区域失效,产生窗口重绘制事件(即 expose 事件)。 */ gtk_widget_queue_draw(widget); GdkPixbuf *src_pixbuf = gdk_pixbuf_new_from_file(path, NULL); // 创建图片资源对象 // w, h是指定图片的宽度和高度 GdkPixbuf *dst_pixbuf = gdk_pixbuf_scale_simple(src_pixbuf, w, h, GDK_INTERP_BILINEAR); GdkPixmap *pixmap = NULL; /* 创建pixmap图像; * NULL:不需要蒙版; * 123: 0~255,透明到不透明 */ gdk_pixbuf_render_pixmap_and_mask(dst_pixbuf, &pixmap, NULL, 128); // 通过pixmap给widget设置一张背景图,最后一个参数必须为: FASLE gdk_window_set_back_pixmap(widget->window, pixmap, FALSE); // 释放资源 g_object_unref(src_pixbuf); g_object_unref(dst_pixbuf); g_object_unref(pixmap); } int main(int argc, char *argv[]) { GtkWidget *window; GtkWidget *vbox; GtkWidget *hbox, *hbox1, *hbox2, *hbox3, *hbox4; GtkWidget *button; gtk_init(&argc, &argv); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); // 保证窗口正常退出 g_signal_connect(G_OBJECT(window), "delete_event", G_CALLBACK(gtk_main_quit), NULL); // 设置背景图片 chang_background(window, 230, 260, "bj_image.png"); gtk_window_set_title(GTK_WINDOW(window), "计算器"); gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); //postion gtk_widget_set_usize (window, 230, 260); gtk_container_set_border_width(GTK_CONTAINER(window), 10); vbox = gtk_vbox_new(FALSE, 0); gtk_container_add(GTK_CONTAINER(window), vbox); // 输入框 entry = gtk_entry_new(); // 设置文本 方向 gtk_widget_set_direction(entry, GTK_TEXT_DIR_RTL); gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 5); hbox = gtk_hbox_new(FALSE, 0); gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 5); button = gtk_button_new_with_label("CE"); gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 5); g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(on_clear_clicked), NULL); hbox1 = gtk_hbox_new(FALSE, 0); gtk_box_pack_start(GTK_BOX(vbox), hbox1, FALSE, FALSE, 5); button = gtk_button_new_with_label("3"); gtk_box_pack_start(GTK_BOX(hbox1), button, TRUE, TRUE, 5); g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(on_num_clicked), NULL); button = gtk_button_new_with_label("2"); gtk_box_pack_start(GTK_BOX(hbox1), button, TRUE, TRUE, 5); g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(on_num_clicked), NULL); button = gtk_button_new_with_label("1"); gtk_box_pack_start(GTK_BOX(hbox1), button, TRUE, TRUE, 5); g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(on_num_clicked), NULL); button = gtk_button_new_with_label("+"); g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(on_suan_clicked), (gpointer)1); gtk_box_pack_start(GTK_BOX(hbox1), button, TRUE, TRUE, 5); hbox2 = gtk_hbox_new(FALSE, 0); gtk_box_pack_start(GTK_BOX(vbox), hbox2, FALSE, FALSE, 5); button = gtk_button_new_with_label("6"); g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(on_num_clicked), NULL); gtk_box_pack_start(GTK_BOX(hbox2), button, TRUE, TRUE, 5); button = gtk_button_new_with_label("5"); g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(on_num_clicked), NULL); gtk_box_pack_start(GTK_BOX(hbox2), button, TRUE, TRUE, 5); button = gtk_button_new_with_label("4"); g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(on_num_clicked), NULL); gtk_box_pack_start(GTK_BOX(hbox2), button, TRUE, TRUE, 5); button = gtk_button_new_with_label("-"); g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(on_suan_clicked), (gpointer)2); gtk_box_pack_start(GTK_BOX(hbox2), button, TRUE, TRUE, 5); hbox3 = gtk_hbox_new(FALSE, 0); gtk_box_pack_start(GTK_BOX(vbox), hbox3, FALSE, FALSE, 5); button = gtk_button_new_with_label("9"); g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(on_num_clicked), NULL); gtk_box_pack_start(GTK_BOX(hbox3), button, TRUE, TRUE, 5); button = gtk_button_new_with_label("8"); g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(on_num_clicked), NULL); gtk_box_pack_start(GTK_BOX(hbox3), button, TRUE, TRUE, 5); button = gtk_button_new_with_label("7"); g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(on_num_clicked), NULL); gtk_box_pack_start(GTK_BOX(hbox3), button, TRUE, TRUE, 5); button = gtk_button_new_with_label("*"); g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(on_suan_clicked), (gpointer)3); gtk_box_pack_start(GTK_BOX(hbox3), button, TRUE, TRUE, 5); hbox4 = gtk_hbox_new(FALSE, 0); gtk_box_pack_start(GTK_BOX(vbox), hbox4, FALSE, FALSE, 5); button = gtk_button_new_with_label("0"); g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(on_num_clicked), NULL); gtk_box_pack_start(GTK_BOX(hbox4), button, TRUE, TRUE, 5); button = gtk_button_new_with_label("."); g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(on_dot_clicked), NULL); gtk_box_pack_start(GTK_BOX(hbox4), button, TRUE, TRUE, 5); button = gtk_button_new_with_label("="); g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(on_eq_clicked), NULL); gtk_box_pack_start(GTK_BOX(hbox4), button, TRUE, TRUE, 5); button = gtk_button_new_with_label("/"); g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(on_suan_clicked), (gpointer)4); gtk_box_pack_start(GTK_BOX(hbox4), button, TRUE, TRUE, 5); // 显示 gtk_widget_show_all(window); // 主事件循环 gtk_main(); return TRUE; }