在GTK3中,当鼠标悬停在GtkWidget上时,如何将鼠标光标设置为十字线,在本例中是GtkDrawingArea?
最佳答案
首先,您必须告诉GtkDrawingArea
小部件use a backing window,以便接收事件:
gtk_widget_set_has_window (GTK_WIDGET (darea), TRUE);
然后必须告诉它which events you wish to subscribe to;在这种情况下,您需要交叉事件,以便接收指针进入和离开小部件的通知:
int crossing_mask = GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK;
gtk_widget_add_events (GTK_WIDGET (darea), crossing_mask);
此时,您可以连接到
GtkWidget::enter-notify-event
和GtkWidget::leave-notify-event
信号:g_signal_connect (darea, "enter-notify-event", G_CALLBACK (on_crossing), NULL);
g_signal_connect (darea, "leave-notify-event", G_CALLBACK (on_crossing), NULL);
如果需要的话,可以使用两个独立的信号处理程序,但是除非在它们中执行复杂的操作,否则代码将非常相同。
on_crossing()
处理程序将如下所示:static gboolean
on_crossing (GtkWidget *darea, GdkEventCrossing *event)
{
switch (gdk_event_get_event_type (event))
{
case GDK_ENTER_NOTIFY:
// Do something on enter
break;
case GDK_LEAVE_NOTIFY:
// Do something on leave
break;
}
}
现在,您可以根据事件类型指定要使用的游标。GTK+使用相同的光标名称as CSS does;您需要使用a cursor instance创建one of those names,然后将其与绘图区域小部件使用的
GdkWindow
关联:// Get the display server connection
GdkDisplay *display = gtk_widget_get_display (darea);
GdkCursor *cursor;
switch (gdk_event_get_event_type (event))
{
case GDK_ENTER_NOTIFY:
cursor = gdk_cursor_new_from_name (display, "crosshair");
break;
case GDK_ENTER_NOTIFY:
cursor = gdk_cursor_new_from_name (display, "default");
break;
}
// Assign the cursor to the window
gdk_window_set_cursor (gtk_widget_get_window (darea), cursor);
// Release the reference on the cursor
g_object_unref (cursor);