|
发表于 2005-8-10 08:24:17
|
显示全部楼层
下面的代码,通过PIXBUF显示一个test.jpeg,点击鼠标后,会在点击处面画一方块
#include <gtk/gtk.h>
GtkWidget *window;
GtkWidget *drawarea;
GdkPixbuf *pixbuf;
GdkPixmap *pixmap=NULL;
gint width, height;
static void
draw_brush (GtkWidget *widget, gdouble x, gdouble y)
{
GdkRectangle update_rect;
update_rect.x = x - 5;
update_rect.y = y - 5;
update_rect.width = 10;
update_rect.height = 10;
gdk_draw_rectangle (pixmap,
widget->style->black_gc,
TRUE,
update_rect.x, update_rect.y,
update_rect.width, update_rect.height);
gtk_widget_queue_draw_area (widget, update_rect.x,update_rect.y,
update_rect.width,update_rect.height);
}
static gboolean configure_event (GtkWidget *widget,GdkEventConfigure *event,gpointer data)
{
if (pixmap)
g_object_unref (pixmap);
pixmap = gdk_pixmap_new (widget->window,
widget->allocation.width,widget->allocation.height,-1);
gdk_draw_rectangle (pixmap,widget->style->white_gc,TRUE,0, 0,
widget->allocation.width,widget->allocation.height);
gdk_draw_pixbuf (pixmap,drawarea->style->white_gc,pixbuf,
0,0,0,0,width,height,0,0,0);
return TRUE;
}
static gboolean expose_event(GtkWidget *widget,GdkEventExpose *event,gpointer data)
{
gdk_draw_drawable (widget->window,
widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
pixmap,
event->area.x, event->area.y,
event->area.x, event->area.y,
event->area.width, event->area.height);
return TRUE;
}
static gint button_press_event(GtkWidget *widget,GdkEventButton *event,gpointer data)
{
g_print("mouse press on %f, %f",event->x, event->y);
draw_brush (widget, event->x, event->y);
return TRUE;
}
void destroy( GtkWidget *widget,
gpointer data )
{
gtk_main_quit ();
}
int main( int argc,
char *argv[] )
{
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect (G_OBJECT (window), "delete_event",
G_CALLBACK (destroy), NULL);
g_signal_connect (G_OBJECT (window), "destroy",
G_CALLBACK (destroy), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
pixbuf = gdk_pixbuf_new_from_file ("test.jpg",NULL);
if (pixbuf == NULL) {
g_print ("Can't load image \n !!");
}
width = gdk_pixbuf_get_width (pixbuf);
height = gdk_pixbuf_get_height (pixbuf);
drawarea = gtk_drawing_area_new ();
gtk_widget_set_size_request (drawarea,width,height);
g_signal_connect (drawarea,"configure_event",
G_CALLBACK (configure_event), NULL);
g_signal_connect (drawarea, "expose_event",
G_CALLBACK (expose_event), NULL);
gtk_widget_set_events (drawarea, GDK_EXPOSURE_MASK
| GDK_LEAVE_NOTIFY_MASK
| GDK_BUTTON_PRESS_MASK
| GDK_POINTER_MOTION_MASK
| GDK_POINTER_MOTION_HINT_MASK);
gtk_signal_connect (GTK_OBJECT(drawarea), "button_press_event",
(GtkSignalFunc) button_press_event, NULL);
gtk_container_add (GTK_CONTAINER (window), drawarea);
gtk_widget_show_all (window);
gtk_main ();
return 0;
} |
|