gpt4 book ai didi

c - 如何在 GTK+3 中处理双击事件?

转载 作者:太空狗 更新时间:2023-10-29 15:08:40 25 4
gpt4 key购买 nike

我想知道如何产生双击?

采用以下通过单击绘制线条的代码 http://zetcode.com/gfx/cairo/basicdrawing/ :

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


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

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

如何让这个程序响应双击事件而不是单击事件?

我无法在此列表中找到它 https://lazka.github.io/pgi-docs/Gtk-3.0/classes/Button.html#Gtk.Button.signals.clicked .

最佳答案

来自 GdkEventButton :

For double-clicks the order of events will be:

  • GDK_BUTTON_PRESS

  • GDK_BUTTON_RELEASE

  • GDK_BUTTON_PRESS

  • GDK_2BUTTON_PRESS

  • GDK_BUTTON_RELEASE

Note that the first click is received just like a normal button press, while the second click results in a GDK_2BUTTON_PRESS being received just after the GDK_BUTTON_PRESS.

(...)

For a double click to occur, the second button press must occur within 1/4 of a second of the first.

每个GdkEvent有一个GdkEventType您可以检查 GDK_2BUTTON_PRESSGDK_DOUBLE_BUTTON_PRESS 的字段(别名在 3.6 中添加):

a mouse button has been double-clicked (clicked twice within a short period of time). Note that each click also generates a GDK_BUTTON_PRESS event.

button-press-event回调。

#include <gtk/gtk.h>

gboolean clicked(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
{
if(event->type == GDK_DOUBLE_BUTTON_PRESS)
printf("double\n");

return TRUE;
}
int main (int argc, char *argv[])
{
gtk_init (&argc, &argv);
GtkWidget *window;
GtkWidget *label;

gtk_init(&argc, &argv);

window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
label = gtk_label_new("label");
gtk_container_add(GTK_CONTAINER(window), label);

g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

gtk_widget_add_events(window, GDK_BUTTON_PRESS_MASK);
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_widget_show_all(window);
gtk_main ();
return 0;
}

关于c - 如何在 GTK+3 中处理双击事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54583727/

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