#include <gtk/gtk.h>/* 这是一个显示图片函数。图片的路径作为参数传入
*/
static GtkWidget *drawing_area;
static GtkWidget *window;
static GtkWidget *aspect_frame;
int showPic( char *args)
{
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Aspect Frame");
g_signal_connect (G_OBJECT (window), "destroy",
G_CALLBACK (gtk_main_quit), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), 150);
/* 创建一个比例框架,将它添加到顶级窗口中 */
aspect_frame = gtk_aspect_frame_new ("image:", /* label */
0.5, /* center x */
0.1, /* center y */
1, /* xsize/ysize = 2 */
FALSE /* ignore child's aspect */);
gtk_container_add (GTK_CONTAINER (window), aspect_frame);
gtk_widget_show (aspect_frame);
/* 添加一个子构件到比例框架中 */
drawing_area = gtk_drawing_area_new ();
drawing_area = gtk_image_new_from_file(args);//********************????????????
/* 要求一个200×200的窗口,但是比例框架会给出一个200×100
* 的窗口,因为我们已经指定了2×1的比例值 */
gtk_widget_set_size_request (drawing_area, 800, 600);
gtk_container_add (GTK_CONTAINER (aspect_frame), drawing_area);
gtk_widget_show (drawing_area);
gtk_widget_show (window);
//gtk_main ();
return 0;
}
int main( int argc, char *argv[] )
{
/* 这个函数在所有的 GTK 程序都要调用。参数由命令行中解析出来并且送到该程序中 */
gtk_init (&argc, &argv);
/* 调用 showPic()函数,传入URL参数 */
showPic(argv[1]);
printf("drawing_area$$ %pn", drawing_area);
//drawing_area = gtk_image_new_from_file(argv[1]);//********************????????????????????
printf("drawing_area %pn", drawing_area);
/* 所有的 GTK 程序必须有一个 gtk_main() 函数。程序运行停在这里
* 等待事件 (如键盘事件或鼠标事件) 的发生。
*/
gtk_main ();
return 0;
}
以上代码可以实现显示一幅图片,图片名字作为参数传进去,但是我想做成灵活的,例如可以通过按钮按下显示下一幅,不知道怎么改了,想把
发布评论
评论(3)
控制显示哪幅图片,不应该从main函数里边实现,至少你得在drawin_are所在的window上加几个按钮和图片位置的输入框吧。
你这个只是显示一幅图片,不用drawing_area,用GtkImage就可以,显示下一幅图片之前,把当前的image free掉。
就是说我现在想在调用 showPic(argv[1]); 完成后再显示一幅图片怎么做呢? 求教高手给支招阿
gtk_image_set_from_file ((GtkImage *)old_draw_area, "./1.png"); 这样做好像不行阿
你应该先买本gtk的书看看,或者看看gtk自带的例子,帮你贴个button的例子吧
#include <stdlib.h>
#include <gtk/gtk.h>
/* Create a new hbox with an image and a label packed into it
* and return the box. */
static GtkWidget *xpm_label_box( gchar *xpm_filename,
gchar *label_text )
{
GtkWidget *box;
GtkWidget *label;
GtkWidget *image;
/* Create box for image and label */
box = gtk_hbox_new (FALSE, 0);
gtk_container_set_border_width (GTK_CONTAINER (box), 2);
/* Now on to the image stuff */
image = gtk_image_new_from_file (xpm_filename);
/* Create a label for the button */
label = gtk_label_new (label_text);
/* Pack the image and label into the box */
gtk_box_pack_start (GTK_BOX (box), image, FALSE, FALSE, 3);
gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 3);
gtk_widget_show (image);
gtk_widget_show (label);
return box;
}
/* Our usual callback function */
static void callback( GtkWidget *widget,
gpointer data )
{
g_print ("Hello again - %s was pressedn", (char *) data);
}
int main( int argc,
char *argv[] )
{
/* GtkWidget is the storage type for widgets */
GtkWidget *window;
GtkWidget *button;
GtkWidget *box;
gtk_init (&argc, &argv);
/* Create a new window */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Pixmap'd Buttons!");
/* It's a good idea to do this for all windows. */
g_signal_connect (G_OBJECT (window), "destroy",
G_CALLBACK (gtk_main_quit), NULL);
g_signal_connect (G_OBJECT (window), "delete_event",
G_CALLBACK (gtk_main_quit), NULL);
/* Sets the border width of the window. */
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
/* Create a new button */
button = gtk_button_new ();
/* Connect the "clicked" signal of the button to our callback */
g_signal_connect (G_OBJECT (button), "clicked",
G_CALLBACK (callback), (gpointer) "cool button");
/* This calls our box creating function */
box = xpm_label_box ("info.xpm", "cool button");
/* Pack and show all our widgets */
gtk_widget_show (box);
gtk_container_add (GTK_CONTAINER (button), box);
gtk_widget_show (button);
gtk_container_add (GTK_CONTAINER (window), button);
gtk_widget_show (window);
/* Rest in gtk_main and wait for the fun to begin! */
gtk_main ();
return 0;
}