LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1097|回复: 2

GTK 新手 画图疑问

[复制链接]
发表于 2004-1-2 03:35:45 | 显示全部楼层 |阅读模式
I wrote a very simple program,  which should draw a rectangle on the left part of the main window ( main window contains a hbox with 2 columns)



  1. //----------------------------------------
  2. #include<gtk/gtk.h>
  3. gint main(gint argc,gchar * argv[])
  4. {
  5.         GtkWidget * main;
  6.         GtkWidget * hbox;
  7.         GtkWidget * button;
  8.         GtkWidget * draw;
  9.         GdkDrawable * drawable;
  10.        
  11.         gtk_init(&argc,&argv);
  12.        
  13.         main = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  14.         gtk_window_set_default_size(GTK_WINDOW(main),500,400);
  15.         gtk_window_set_title(GTK_WINDOW(main),"New Window");
  16.         gtk_signal_connect(GTK_OBJECT(main),"destroy",GTK_SIGNAL_FUNC(gtk_main_quit),NULL);
  17.        
  18.         gtk_widget_show(main);  
  19.        
  20.         hbox = gtk_hbox_new(FALSE,10);
  21.         gtk_container_add(GTK_CONTAINER(main),hbox);
  22.         gtk_widget_show(hbox);
  23.        
  24.         draw = gtk_drawing_area_new();
  25.         gtk_widget_set_usize(draw,300,300);
  26.         gtk_box_pack_start(GTK_BOX(hbox),draw,TRUE,TRUE,5);
  27.         gtk_widget_show(draw);
  28.         drawable = draw->window;
  29.         g_assert(drawable != NULL);
  30.         gdk_draw_rectangle(drawable,draw->style->black_gc,TRUE,100,100,(draw->allocation.width)/2,(draw->allocation.height)/2);

  31.        
  32.         button = gtk_button_new_with_label("Click Me");
  33.         gtk_box_pack_end(GTK_BOX(hbox),button,FALSE,FALSE,0);
  34.        
  35.         gtk_widget_show_all(main);
  36.        
  37.         gtk_main();
  38.         return 0;
  39. }
  40. //-----------------------------------------------------
复制代码



It runs ok but no content on the left cell. Then I comment the line
gtk_widget_show(main);
drawable will be NULL and an assert will given in runtime.

I am a newbee to GTK programming, anyone can help me to figure out the error?

Thanks!
发表于 2004-1-2 16:39:58 | 显示全部楼层
画矩形要在某个控件上,控件名忘了。
看API 有函数的。
发表于 2004-1-2 17:16:22 | 显示全部楼层
例程在在附件里,画图需要一个GdkPixmap和GtkDrawingArea的expose_event和
configure_event连接,然后在pixmap上画.
具体请看gtk带的gtk-demo.

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表