|
代码如下:
#include <gtk/gtk.h>
/* 功能: 设置背景图
* widget: 主窗口
* w, h: 图片的大小
* path: 图片路径
*/
/***********************************/
struct carry_widget{
GtkWidget *window; //主窗体
GtkWidget *button_start;
GtkWidget *button_back;
GtkWidget *label_tip1;
GtkWidget *label_tip2;
GtkWidget *spinButton_x;
GtkWidget *spinButton_y;
GtkWidget *labelx;
GtkWidget *labely;
GtkWidget *button_enter;
GtkObject *adjustment;
GtkWidget *table;
// GtkWidget *vbox; //两个布局盒子
// GtkWidget *hbox;
}carry;
void value_changed_callback(GtkSpinButton *spinButton, gpointer data) {
gdouble value = gtk_spin_button_get_value(spinButton);
GString *text = g_string_new("");
g_string_sprintf(text, "%d", value);
gtk_label_set_text(GTK_LABEL(data), text->str);
}
/**********************************/
void carry_test(GtkWidget *widget, int w, int h, const gchar *path)
{
carry.window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
carry.button_start=gtk_button_new_with_label("开始采集");
carry.button_back=gtk_button_new_with_label("返回");
carry.label_tip1=gtk_label_new("一个参考点采集完成。");
carry.label_tip2=gtk_label_new("请输入对应的坐标");
carry.adjustment = gtk_adjustment_new(0, 0, 10000, 1, 0.0, 0.0);
carry.spinButton_x = gtk_spin_button_new(GTK_ADJUSTMENT(carry.adjustment), 0.01, 0);
carry.spinButton_y = gtk_spin_button_new(GTK_ADJUSTMENT(carry.adjustment), 0.01, 0);
carry.labelx =gtk_label_new("0.00");
carry.labely =gtk_label_new("0.00");
carry.button_enter=gtk_button_new_with_label("确定");
carry.table =gtk_table_new(8,6,FALSE);
// carry.hbox = gtk_hbox_new(TRUE, 5);
// carry.vbox = gtk_hbox_new(TRUE, 5);
gtk_window_set_title(GTK_WINDOW(carry.window), "采集窗口");
gtk_widget_set_size_request(carry.window, 500, 450);
gtk_window_set_position(GTK_WINDOW(carry.window),GTK_WIN_POS_CENTER);
gtk_container_set_border_width(GTK_CONTAINER(carry.window),20);
g_signal_connect(G_OBJECT(carry.window),"destroy",G_CALLBACK
(gtk_main_quit),NULL);
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);
gtk_table_attach(GTK_TABLE(carry.table),carry.button_start,
1,3,1,2,GTK_FILL,GTK_FILL,0,0);
gtk_table_attach(GTK_TABLE(carry.table),carry.button_back,
4,6,1,2,GTK_FILL,GTK_FILL,0,0);
gtk_table_attach(GTK_TABLE(carry.table),carry.label_tip1,
1,7,2,3,GTK_FILL,GTK_FILL,0,0);
gtk_table_attach(GTK_TABLE(carry.table),carry.label_tip2,
1,4,3,4,GTK_FILL,GTK_FILL,0,0);
gtk_table_attach(GTK_TABLE(carry.table),carry.spinButton_x,
5,6,3,4,GTK_FILL,GTK_FILL,0,0);
gtk_table_attach(GTK_TABLE(carry.table),carry.spinButton_y,
6,7,3,4,GTK_FILL,GTK_FILL,0,0);
gtk_table_attach(GTK_TABLE(carry.table),carry.labelx,
5,6,4,5,GTK_FILL,GTK_FILL,0,0);
gtk_table_attach(GTK_TABLE(carry.table),carry.labely,
6,7,4,5,GTK_FILL,GTK_FILL,0,0);
gtk_table_attach(GTK_TABLE(carry.table),carry.button_enter,
3,6,5,6,GTK_FILL,GTK_FILL,0,0);
gtk_container_add(GTK_CONTAINER(carry.window), carry.table);
g_signal_connect(GTK_OBJECT(carry.spinButton_x), "value_changed",
G_CALLBACK(value_changed_callback), carry.labelx);
g_signal_connect(GTK_OBJECT(carry.spinButton_y), "value_changed",
G_CALLBACK(value_changed_callback), carry.labely);
g_signal_connect(GTK_OBJECT(carry.window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(carry.window); // 显示所有部件
}
int main( int argc, char *argv[])
{
gtk_init(&argc, &argv);
//主窗口操作
carry_test(carry.window, 500, 400, "image.jpg"); // 设置窗口背景图
gtk_main();
return 0;
}
结果在vnc上执行时出现如图所示的情形,求大神解释是怎么回事?是程序哪里错了吗
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?马上加入
x
|