|
執行後,兩個視窗是分離的,可以讓button顯示在drawingarea上嗎?還有我設定的scrolledwindow,也沒顯示出來,感謝各位大大的幫忙
#include <gtk/gtk.h>
gint x=10;
gint y=50;
gint x1=5;
gint y1=2;
/*button移動的位置*/
void move_button( GtkWidget *button,GtkLayout *layout)
{
x = (x + 2) % 3000;
y = 500;
gtk_layout_move (GTK_LAYOUT (layout),button, x, y);
}
void move_button1( GtkWidget *button,GtkLayout *layout )
{
x1 = (x1 + 1) % 3000;
y1 = 50;
gtk_layout_move (GTK_LAYOUT (layout), button, x1, y1);
}
GtkWidget* buttonWithImage(gchar *filename, gchar *text) {
GtkWidget *box;
GtkWidget *label;
GtkWidget *image;
box = gtk_hbox_new(FALSE, 5);
gtk_container_set_border_width(GTK_CONTAINER(box), 2);
image = gtk_image_new_from_file(filename);
label = gtk_label_new(text);
gtk_box_pack_start(GTK_BOX(box), image, FALSE, FALSE, 5);
gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 5);
gtk_widget_show (image);
gtk_widget_show (label);
return box;
}
int main(int argc, char *argv[]) {
GtkWidget *window;
GtkWidget *scrolledWindow;
GtkWidget *drawing_area;
GdkColor color;
// gint i;
GtkWidget *layout;
GtkWidget *button_running;
GtkWidget *button1;
GtkWidget *box;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "GtkDrawingArea");
gtk_window_set_policy (GTK_WINDOW (window), TRUE, TRUE, TRUE);
scrolledWindow = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledWindow),GTK_POLICY_AUTOMATIC,
GTK_POLICY_AUTOMATIC);
gtk_container_add(GTK_CONTAINER(window), scrolledWindow);
drawing_area = gtk_drawing_area_new();
gtk_widget_set_size_request(drawing_area,2000, 2000);
gtk_container_add(GTK_CONTAINER(window), drawing_area);
g_signal_connect(GTK_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
layout = gtk_layout_new (NULL,NULL);
gtk_container_add(GTK_CONTAINER(window),layout);
gtk_widget_show(layout);
button_running = gtk_button_new();
box=buttonWithImage("car.jpg",NULL);
gtk_container_add(GTK_CONTAINER(window),layout);
gtk_container_add(GTK_CONTAINER(button_running),box);
gtk_container_add(GTK_CONTAINER(layout),button_running);
gtk_widget_set_size_request(button_running,100, 50); //button的長與高
gtk_layout_move (GTK_LAYOUT (layout), button_running,0,500); //button的位置
gtk_layout_put(GTK_LAYOUT(layout),drawing_area,150,0);
gtk_widget_realize(button_running);
g_signal_connect(GTK_OBJECT (button_running), "clicked",G_CALLBACK (move_button), layout);
gtk_container_add(GTK_CONTAINER(drawing_area),layout);
gtk_container_add(GTK_CONTAINER(drawing_area),button_running);
gtk_container_add(GTK_CONTAINER(drawing_area),box);
gtk_container_add(GTK_CONTAINER(scrolledWindow),layout);
gtk_container_add(GTK_CONTAINER(scrolledWindow),drawing_area);
gtk_container_add(GTK_CONTAINER(scrolledWindow),box);
gdk_color_parse("#000000", &color);
gtk_widget_modify_bg(GTK_WIDGET(drawing_area), GTK_STATE_NORMAL, &color);
gtk_scrolled_window_add_with_viewport(
GTK_SCROLLED_WINDOW(scrolledWindow),drawing_area);
gtk_widget_show(drawing_area);
gtk_widget_show_all(button_running);
gtk_widget_show_all(scrolledWindow);
gtk_widget_show(box);
gtk_widget_show_all(window);
gtk_main();
return 0;
} |
|