gpt4 book ai didi

c - 在 GTK+3 中,我如何获得绘图区域来响应鼠标事件?

转载 作者:行者123 更新时间:2023-12-04 02:05:05 25 4
gpt4 key购买 nike

在 GTK+3 中,我如何获得一个 drawing_area 来响应鼠标事件?

main() 函数中,我声明了我的drawing_area:

GtkWidget *drawing_area;

然后我将 drawing_area 与鼠标点击信号连接起来:

g_signal_connect(drawing_area, "button-press-event", G_CALLBACK(clicked), NULL);

函数“clicked”定义为:

static gboolean clicked(GtkWidget *widget, GdkEventButton *event, gpointer user_data)

printf("Clicked! \n");

return TRUE;
}

程序运行并显示 drawing_area 但是当我点击它时,没有任何反应,没有任何反应!为什么会这样?

最佳答案

GtkDrawingArea 好像默认接收不到鼠标事件

查看 documentation :

To receive mouse events on a drawing area, you will need to enable them with gtk_widget_add_events(). To receive keyboard events, you will need to set the “can-focus” property on the drawing area, and you should probably draw some user-visible indication that the drawing area is focused. Use gtk_widget_has_focus() in your expose event handler to decide whether to draw the focus indicator. See gtk_render_focus() for one way to draw focus.

或者尝试将事件 "button-press-event" 连接到窗口:

g_signal_connect(window, "button-press-event", G_CALLBACK(clicked), NULL);

代替

g_signal_connect(drawing_area, "button-press-event", G_CALLBACK(clicked), NULL);

如本例所示:

http://zetcode.com/gfx/cairo/basicdrawing/

关于c - 在 GTK+3 中,我如何获得绘图区域来响应鼠标事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44077526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com