要想摒弃控件默认的效果,实现自己想要的任意效果,就只能自己对控件进行重绘
做了gtkcheckbutton和gtkscale控件,顺便总结如下:
checkbutton的实现:
const char *sound_on = "./skin/res/sound_on.png";
const char *sound_off = "./skin/res/sound_off.png";
static gboolean sound_check_button_expose_event_cb(GtkWidget *widget,
GdkEventExpose *event,
gpointer user_data)
{
gboolean bActive = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
cairo_surface_t *surface = NULL;
cairo_t *cr = gdk_cairo_create(GDK_DRAWABLE(widget->window));
if(bActive)
{
surface = cairo_image_surface_create_from_png(sound_on);
}
else
{
surface = cairo_image_surface_create_from_png(sound_off);
}
cairo_set_source_surface(cr, surface, widget->allocation.x, widget->allocation.y);
cairo_paint(cr);
cairo_surface_destroy(surface);
cairo_destroy(cr);
return TRUE;
}
在此有点取巧:创建的checkbutton和对应的button效果图的大小是一样大的,由于是触摸设备,未实现鼠标hover效果
gtkscale的重绘制:
static gboolean sound_scale_expose_event_cb(GtkWidget *widget,
GdkEventExpose *event,
gpointer user_data)
{
GtkRange *pRange = (GtkRange*) widget;
cairo_surface_t *surface = NULL;
cairo_t *cr = gdk_cairo_create(GDK_DRAWABLE(widget->window));
//
surface = cairo_image_surface_create_from_png(sound_left);
cairo_set_source_surface(cr, surface, widget->allocation.x,
widget->allocation.y);
cairo_paint(cr);
cairo_surface_destroy(surface);
//
surface = cairo_image_surface_create_from_png(sound_btn);
cairo_set_source_surface(cr, surface,
widget->allocation.x + pRange->slider_start,
widget->allocation.y);
cairo_paint(cr);
cairo_surface_destroy(surface);
cairo_destroy(cr);
return TRUE;
}
阅读(2232) | 评论(0) | 转发(0) |