如何将鼠标光标设置为在 GtkDrawingArea 上交叉?
How do I set the mouse cursor to cross hair over a GtkDrawingArea?
在 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
and 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; you need to create a cursor instance using 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);
在 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
and 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; you need to create a cursor instance using 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);