我尝试了不同的方法,例如将事件框的背景更改为透明等,但它们都非常复杂。
我找到的最简单的解决方案如下:
static GdkCursor *_normal_cursor = NULL;
static GdkCursor *_hand_cursor = NULL;
/* in main */
_normal_cursor = gdk_window_get_cursor(widgets_to_remember->window->window);
_hand_cursor = gdk_cursor_new(GDK_HAND2);
/* create the event box */
gtk_event_box_set_visible_window(GTK_EVENT_BOX(event_box), FALSE);
gtk_widget_set_events(event_box, GDK_BUTTON_PRESS_MASK | GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
_fix_event_box(event_box, window);
/* rest of main */
static gboolean _set_hand(GtkWidget *w, GdkEventCrossing *e, gpointer data)
{
gdk_window_set_cursor(w->window, _hand_cursor);
return TRUE;
}
static gboolean _set_normal(GtkWidget *w, GdkEventCrossing *e, gpointer data)
{
gdk_window_set_cursor(w->window, _normal_cursor);
return TRUE;
}
static void _fix_event_box(GtkWidget *eb, GtkWidget *window)
{
g_signal_connect_swapped(eb, "enter_notify_event", G_CALLBACK(_set_hand), window);
g_signal_connect_swapped(eb, "leave_notify_event", G_CALLBACK(_set_normal), window);
}
这基本上所做的是将事件框设置为不可见,然后设置其enter-notify-event
和leave-notify-event
信号处理程序以在鼠标进入或离开其窗口时更改窗口光标。