#include <gtk/gtk.h>
#include <stdio.h>
gint sx=0;
gint sy=0;
gint ex=1;
gint ey=1;
gint barx=250;
gint diameter=50;
GtkWidget *window;
GtkWidget *da;
gint single =0;
static void destroy(GtkWidget *widget,gpointer data)
{
gtk_main_quit();
}
void press(GtkWidget *widget,GdkEventKey *event)
{
g_print ("press: %x\n",event->keyval);
switch(event->keyval)
{
case 65361: //左键
//g_print ("Rigth\n"); //use debug
if (barx>0 )
barx = barx - 10;
else
barx = 0;
break ;
case 65363: //右键
//g_print ("Left\n"); //use debug
if (barx<500 )
barx = barx + 10;
else
barx = 500;
break ;
case 32: //空格键
single = 1;
//g_print ("Stop\n"); //use debug
break;
case 65293: //回车键
single =0;
//g_print ("Begin\n"); //use debug
break;
default:
break;
}
}
gboolean my_expose(GtkWidget *da,GdkEventExpose *event,gpointer data )
{
GdkGC *gc1,*gc2,*gc;
GdkColor color;
gc1 = gdk_gc_new(da->window);
color.red = 0;
color.green = 65535;
color.blue = 0;
gdk_gc_set_rgb_fg_color(gc1,&color);
gc2 = gdk_gc_new(da->window);
color.red = 65535;
color.green = 0;
color.blue = 0;
gdk_gc_set_rgb_fg_color(gc2,&color);
gc = gc1;
gdk_draw_rectangle(da->window,gc,TRUE,barx,480,100,20);
gc = gc2;
gdk_draw_arc(da->window,gc,TRUE,ex,ey,diameter,diameter,0,64*360);
g_object_unref(G_OBJECT(gc1));
g_object_unref(G_OBJECT(gc2));
return TRUE;
}
void leave_notify_event(GtkWidget *widget, GdkEventMotion *event)
{
gint x, y;
GdkModifierType state;
GdkRectangle rect;
if (event->is_hint)
gdk_window_get_pointer(event->window,&x,&y,&state);
else{
x = (gint)event->x;
y = (gint)event->y;
state = (GdkModifierType)event->state;
}
barx = x;
rect.x = 0;
rect.y = 470;
rect.width = 600;
rect.height = 30;
gdk_window_invalidate_rect(da->window,&rect,FALSE);
}
gboolean inBar()
{
if(ex >=barx && ex <= (barx+100))
return TRUE;
return FALSE;
}
gboolean chPosition()
{
gint tempx=ex;
gint tempy=ey;
if ( single == 1)
{
return TRUE;
}
if(((ex == 0) && (ey > sy)) ||( (ey == 0) && (ex>sx)))//daiwei+()
{
ex++;
ey++;
}//右下
else if(((ey == (480-diameter)) && (ex < sx) && (inBar())) || ((ex == (600-diameter)) && (ey < sy) ))//daiwei+()
{
ex--;
ey--;
}//左上
else if(((ey == 0) && (ex <sx)) || ((ex == (600-diameter)) && (ey > sy)))//daiwei+()
{
ex--;
ey++;
}//左下
else if(((ey == (480-diameter)) && (inBar()) && (ex > sx)) || ((ex == 0) && (ey < sy)))//daiwei+()
{
ex++;
ey--;
}//右上
else if(ey == (500-diameter) )
{
return FALSE;
}
else
{
ex = 2*ex - sx;
ey = 2*ey - sy;
}
sx = tempx;
sy = tempy;
gdk_window_invalidate_rect(da->window, NULL, FALSE);
return TRUE;
}
int main(int argc, char *argv[])
{
gtk_init(&argc,&argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_set_events ( GTK_WIDGET(window), GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK|GDK_BUTTON_PRESS_MASK );
g_signal_connect(G_OBJECT(window),"delete_event", G_CALLBACK(gtk_main_quit),NULL);
g_signal_connect(G_OBJECT(window),"key_press_event", G_CALLBACK(press),NULL);
gtk_widget_add_events( GTK_WIDGET(window), GDK_KEY_PRESS_MASK |GDK_KEY_RELEASE_MASK|GDK_BUTTON_PRESS_MASK);
g_signal_connect(G_OBJECT(window),"destroy",G_CALLBACK(destroy),NULL);
gtk_signal_connect(GTK_OBJECT(window),"leave_notify_event", GTK_SIGNAL_FUNC(leave_notify_event),NULL);
da = gtk_drawing_area_new();
gtk_widget_set_size_request(da,600,500);
gtk_container_add(GTK_CONTAINER(window),da);
g_signal_connect(da,"expose_event",G_CALLBACK(my_expose),NULL);
gtk_widget_show_all(window);
g_timeout_add(10,(GSourceFunc)chPosition,NULL);
gtk_main();
return 0;
}
评论0