gpt4 book ai didi

c - 如何使用 GTK3 捕捉鼠标滚轮向上/向下事件?

转载 作者:行者123 更新时间:2023-12-01 12:21:16 25 4
gpt4 key购买 nike

如何使用 C GTK3 实现鼠标滚轮向上/向下事件?

我已经改编了这个code为了处理鼠标 scoll 事件:

#include <cairo.h>
#include <gtk/gtk.h>

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 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;
}

static gboolean mouse_scroll (GtkWidget *widget,
GdkEvent *event,
gpointer user_data)
{

printf("scrolled up! \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_add_events(window, GDK_BUTTON_PRESS_MASK);
gtk_widget_add_events(window, GDK_SCROLL_MASK);


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);

g_signal_connect(window, "scroll-event",
G_CALLBACK(mouse_scroll), 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;
}

但是,这只会捕获一般的鼠标事件,而不管鼠标是向上滚动还是向下滚动。我想知道只有当鼠标滚轮向上滚动时,如何才能将其发送到 printf("scrolled up!\n") 。有任何想法吗?

最佳答案

mouse_scroll 中的*event 参数应该指向GdkEventScroll具有方向成员的结构:

GdkScrollDirection direction; the direction to scroll to (one of GDK_SCROLL_UP, GDK_SCROLL_DOWN, GDK_SCROLL_LEFT, GDK_SCROLL_RIGHT or GDK_SCROLL_SMOOTH).

关于c - 如何使用 GTK3 捕捉鼠标滚轮向上/向下事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43847450/

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