在 Gtk::DrawingArea 中添加和定位小部件

发布于 2024-10-17 05:15:06 字数 203 浏览 2 评论 0原文

大家好,我想知道是否有办法在 Gtk::DrawingArea 中添加和定位 Gtk::Widget。我这样做的目的是当单击图形时,在绘图区域中显示图形的一些详细信息。详细信息可以是丰富的格式。

我了解到 Gtk::DrawingArea 没有像 add(Widget) 这样的功能。所以我想知道是否有解决方法。

提前致谢&此致。

陈凯

Greetings to all, I would like to know whether there is a way to add and locate a Gtk::Widget in Gtk::DrawingArea. My purpose of doing that is to show some detailed information on the graphics in the drawing area when the graphics is clicked. The detailed information can be rich-formatted.

I understood that Gtk::DrawingArea does not have the function like add(Widget). So I wonder if a work-around is available.

Thanks in advance & best regards.

CHEN Kai

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦在深巷 2024-10-24 05:15:06

不确定它有多大帮助,但我通过搜索发现了这一点:

Old Nabble - Gtk+ - Python - 在 gtk.drawing 区域上覆盖 gtk 小部件

似乎有一个 gtk.Fixed()->put() 函数,但是我无法判断它是否应该在绘图循环中运行..(编辑:另请参阅 GTK v1.2 教程:容器小部件

干杯!

Not sure how helpful it is, but I found this by searching:

Old Nabble - Gtk+ - Python - overlaying gtk widget over a gtk.drawing area

seems there is a gtk.Fixed()->put() function, but I cannot tell if it should run in a drawing loop or not.. (edit: see also GTK v1.2 Tutorial: Container Widgets)

Cheers!

回梦 2024-10-24 05:15:06

是的,这是可能的。给你。欣赏一下C中的代码:

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

GtkWidget *window;
GtkWidget *layout;
GtkWidget *canvas;

GtkWidget   *image;
GtkWidget   *eventbox;

static gboolean on_window_draw (GtkWidget *da, GdkEventExpose *event, gpointer data)
{
    GdkWindow *window;
    GdkDrawingContext *drawingContext;
    cairo_region_t *cairoRegion;
    cairo_t *cr;

    cairoRegion = cairo_region_create();
    window = gtk_layout_get_bin_window(GTK_LAYOUT(layout)); 
    drawingContext = gdk_window_begin_draw_frame(window, cairoRegion);
    cr = gdk_drawing_context_get_cairo_context(drawingContext);

    cairo_set_line_width(cr, 9);  
    cairo_set_source_rgb(cr, 0.69, 0.19, 0);

    cairo_translate(cr, 300/2, 200/2);
    cairo_arc(cr, 0, 0, 50, 0, 2 * M_PI);
    cairo_stroke_preserve(cr);

    cairo_set_source_rgb(cr, 0.3, 0.4, 0.6); 
    cairo_fill(cr);

    gdk_window_end_draw_frame(window, drawingContext);
    cairo_region_destroy(cairoRegion);

    return FALSE;
}

int main ( int argc, char **argv) 
{   
    GtkWidget *button;

    gtk_init (&argc , &argv);
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_widget_set_size_request (window, 300, 200);
    g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit) , NULL);

    canvas = gtk_drawing_area_new ();
    layout = gtk_layout_new(NULL,NULL);
    gtk_container_add (GTK_CONTAINER (layout), canvas);
    gtk_container_add (GTK_CONTAINER (window), layout);

    button = gtk_button_new_with_label("I feel alone...");
    gtk_container_add(GTK_CONTAINER(layout), button);
    gtk_layout_move(GTK_LAYOUT(layout), button, 90, 50);
    g_signal_connect (canvas, "draw", (GCallback) on_window_draw, NULL);

    gtk_widget_show_all (window);
    gtk_main ();
    return 0;
}

Yep it's possible. Here you are. Enjoy the code in C:

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

GtkWidget *window;
GtkWidget *layout;
GtkWidget *canvas;

GtkWidget   *image;
GtkWidget   *eventbox;

static gboolean on_window_draw (GtkWidget *da, GdkEventExpose *event, gpointer data)
{
    GdkWindow *window;
    GdkDrawingContext *drawingContext;
    cairo_region_t *cairoRegion;
    cairo_t *cr;

    cairoRegion = cairo_region_create();
    window = gtk_layout_get_bin_window(GTK_LAYOUT(layout)); 
    drawingContext = gdk_window_begin_draw_frame(window, cairoRegion);
    cr = gdk_drawing_context_get_cairo_context(drawingContext);

    cairo_set_line_width(cr, 9);  
    cairo_set_source_rgb(cr, 0.69, 0.19, 0);

    cairo_translate(cr, 300/2, 200/2);
    cairo_arc(cr, 0, 0, 50, 0, 2 * M_PI);
    cairo_stroke_preserve(cr);

    cairo_set_source_rgb(cr, 0.3, 0.4, 0.6); 
    cairo_fill(cr);

    gdk_window_end_draw_frame(window, drawingContext);
    cairo_region_destroy(cairoRegion);

    return FALSE;
}

int main ( int argc, char **argv) 
{   
    GtkWidget *button;

    gtk_init (&argc , &argv);
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_widget_set_size_request (window, 300, 200);
    g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit) , NULL);

    canvas = gtk_drawing_area_new ();
    layout = gtk_layout_new(NULL,NULL);
    gtk_container_add (GTK_CONTAINER (layout), canvas);
    gtk_container_add (GTK_CONTAINER (window), layout);

    button = gtk_button_new_with_label("I feel alone...");
    gtk_container_add(GTK_CONTAINER(layout), button);
    gtk_layout_move(GTK_LAYOUT(layout), button, 90, 50);
    g_signal_connect (canvas, "draw", (GCallback) on_window_draw, NULL);

    gtk_widget_show_all (window);
    gtk_main ();
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文