我正在使用C和GTK3 +编写程序。我想使用键盘更改鼠标光标行为。目前,每当鼠标悬停绘图区域时,我的代码都会使鼠标光标从“默认”变为“十字准线”:
#include <cairo.h>
#include <gtk/gtk.h>
/* gcc example_mouse_cursor.c `pkg-config --cflags gtk+-3.0 --libs --libs cairo gtk+-3.0` -lm -o example && ./example */
static void do_drawing(cairo_t *);
struct {
int count;
double coordx[100];
double coordy[100];
} glob;
static gboolean on_draw_event(GtkWidget *widget, cairo_t *cr,
gpointer user_data)
{
do_drawing(cr);
return FALSE;
}
static void do_drawing(cairo_t *cr)
{
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_set_line_width(cr, 0.5);
int i, j;
for (i = 0; i <= glob.count - 1; i++ ) {
for (j = 0; j <= glob.count - 1; j++ ) {
cairo_move_to(cr, glob.coordx[i], glob.coordy[i]);
cairo_line_to(cr, glob.coordx[j], glob.coordy[j]);
}
}
glob.count = 0;
cairo_stroke(cr);
}
static gboolean on_crossing (GtkWidget *darea, GdkEvent *event)
{
GdkCursor *cursor;
GdkDisplay *display = gtk_widget_get_display (darea);
switch (gdk_event_get_event_type (event))
{
case GDK_ENTER_NOTIFY:
cursor = gdk_cursor_new_from_name (display, "crosshair");
break;
case GDK_LEAVE_NOTIFY:
cursor = gdk_cursor_new_from_name (display, "default");
break;
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);
return TRUE;
}
static gboolean clicked(GtkWidget *widget, GdkEventButton *event,
gpointer user_data)
{
if (event->button == 1) {
glob.coordx[glob.count] = event->x;
glob.coordy[glob.count++] = event->y;
}
if (event->button == 3) {
gtk_widget_queue_draw(widget);
}
return TRUE;
}
gboolean keyboard_press (GtkWidget *window, GdkEventKey *event, gpointer data){
/*Do something if 'b' key is pressed */
if (event->keyval == GDK_KEY_b)
{
printf("'b' key pressed!\n");
}
return TRUE;
};
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *darea;
glob.count = 0;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
darea = gtk_drawing_area_new();
gtk_container_add(GTK_CONTAINER(window), darea);
gtk_widget_set_has_window (GTK_WIDGET (darea), TRUE);
gtk_widget_add_events (GTK_WIDGET (darea), (GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK) );
gtk_widget_add_events(window, GDK_BUTTON_PRESS_MASK);
g_signal_connect (darea, "enter-notify-event", G_CALLBACK (on_crossing), NULL);
g_signal_connect (darea, "leave-notify-event", G_CALLBACK (on_crossing), NULL);
g_signal_connect (G_OBJECT(window), "key_press_event", G_CALLBACK (keyboard_press), NULL);
g_signal_connect(G_OBJECT(darea), "draw", G_CALLBACK(on_draw_event), NULL);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(window, "button-press-event",
G_CALLBACK(clicked), NULL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 400, 300);
gtk_window_set_title(GTK_WINDOW(window), "Lines");
gtk_widget_show_all(window);
gtk_main();
return 0;
}
我希望能够在按下'b'键时禁用鼠标悬停行为('让鼠标光标在任何地方保持'默认')。如果用户再次按下“b”键,则再次启用光标更改bahaviour。
我可以从键盘上获取信号,在函数 gboolean keyboard_press()中实现,但我不知道如何在此函数中更改鼠标光标行为。有没有办法解除信号:
g_signal_connect (darea, "enter-notify-event", G_CALLBACK (on_crossing), NULL);
g_signal_connect (darea, "leave-notify-event", G_CALLBACK (on_crossing), NULL);
在 gboolean keyboard_press()函数内?我找到g_signal_handler_disconnect(),但我不知道如何使用它。
你会怎么做?
由于