如何将鼠标光标设置为在GtkDrawingArea上交叉?

时间:2017-05-01 00:16:11

标签: c mouseover gtk3

在GTK3中,当鼠标悬停在 GtkWidget 上时,如何将鼠标光标设置为交叉发,在这种情况下为 GtkDrawingArea

1 个答案:

答案 0 :(得分:2)

首先,您必须告诉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-eventGtkWidget::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);