|
发表于 2005-8-30 09:49:49
|
显示全部楼层
#include <gtk/gtk.h>
GtkWidget *window;
GtkWidget *drawarea;
GdkPixbuf *pixbuf;
GdkPixmap *pixmap=NULL;
gint width, height;
GdkPixbuf *pixbuf1;
gint width1, height1;
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);
gdk_draw_pixbuf (pixmap,drawarea->style->white_gc,pixbuf1,
0,0,0,0,width1,height1,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)
{
//if (event->button == 1 && pixmap != NULL)
g_print("mouse press on %f, %f",event->x, event->y);
draw_brush (widget, event->x, event->y);
return TRUE;
}
static gint motion_notify_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
{
g_printf("mouse move on %f,%f\n", 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 ("background.png",NULL);
if (pixbuf == NULL) {
g_print ("Can't load image \n !!");
}
width = gdk_pixbuf_get_width (pixbuf);
height = gdk_pixbuf_get_height (pixbuf);
pixbuf1 = gdk_pixbuf_new_from_file ("button_off.png",NULL);
if (pixbuf1 == NULL) {
g_print ("Can't load image \n !!");
}
width1 = gdk_pixbuf_get_width (pixbuf1);
height1 = gdk_pixbuf_get_height (pixbuf1);
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_signal_connect (GTK_OBJECT(drawarea), "motion_notify_event",
(GtkSignalFunc) motion_notify_event, NULL);
gtk_container_add (GTK_CONTAINER (window), drawarea);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
这段代码会载入一个图片,你在上面点鼠标会画一个方形上去的 |
|