最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

gtk - How to create a transparent window in GDK 3 that passes mouse events using C? - Stack Overflow

programmeradmin0浏览0评论

I need to create transparent window and when user clicks inside this window then mouse event to be passed to a window that is behind. For example, if there is a text editor behind my window then mouse event should be passed to that editor window. By other words I need the window to be transparent for mouse events.

This is my code. I create window but its content area is not transparent.

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

static void on_destroy(GtkWidget *widget, gpointer data) {
    gtk_main_quit();
}

static void on_realize(GtkWidget *widget, gpointer data) {
    GdkWindow *gdk_window;
    cairo_t *cr;
    cairo_surface_t *surface;
    cairo_region_t *region;

    gdk_window = gtk_widget_get_window(widget);

    if (gdk_window) {
        surface = gdk_window_create_similar_surface(gdk_window,  CAIRO_CONTENT_COLOR_ALPHA, 400, 300);
        cr = cairo_create(surface);

        cairo_set_source_rgb(cr, 0, 0, 0);
        cairo_rectangle(cr, 0, 0, 400, 300);
        cairo_fill(cr);

        region = cairo_region_create_rectangle(&((cairo_rectangle_int_t){0, 0, 200, 300}));
        gdk_window_input_shape_combine_region(gdk_window, region, 0, 0);

        cairo_destroy(cr);
        cairo_surface_destroy(surface);
        cairo_region_destroy(region);
    }
}

int main(int argc, char *argv[]) {
    GtkWidget *window;

    gtk_init(&argc, &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(window, "destroy", G_CALLBACK(on_destroy), NULL);
    g_signal_connect(window, "realize", G_CALLBACK(on_realize), NULL);
    gtk_widget_show_all(window);
    gtk_main();
    return 0;
}

Could anyone say how to do it?

I need to create transparent window and when user clicks inside this window then mouse event to be passed to a window that is behind. For example, if there is a text editor behind my window then mouse event should be passed to that editor window. By other words I need the window to be transparent for mouse events.

This is my code. I create window but its content area is not transparent.

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

static void on_destroy(GtkWidget *widget, gpointer data) {
    gtk_main_quit();
}

static void on_realize(GtkWidget *widget, gpointer data) {
    GdkWindow *gdk_window;
    cairo_t *cr;
    cairo_surface_t *surface;
    cairo_region_t *region;

    gdk_window = gtk_widget_get_window(widget);

    if (gdk_window) {
        surface = gdk_window_create_similar_surface(gdk_window,  CAIRO_CONTENT_COLOR_ALPHA, 400, 300);
        cr = cairo_create(surface);

        cairo_set_source_rgb(cr, 0, 0, 0);
        cairo_rectangle(cr, 0, 0, 400, 300);
        cairo_fill(cr);

        region = cairo_region_create_rectangle(&((cairo_rectangle_int_t){0, 0, 200, 300}));
        gdk_window_input_shape_combine_region(gdk_window, region, 0, 0);

        cairo_destroy(cr);
        cairo_surface_destroy(surface);
        cairo_region_destroy(region);
    }
}

int main(int argc, char *argv[]) {
    GtkWidget *window;

    gtk_init(&argc, &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(window, "destroy", G_CALLBACK(on_destroy), NULL);
    g_signal_connect(window, "realize", G_CALLBACK(on_realize), NULL);
    gtk_widget_show_all(window);
    gtk_main();
    return 0;
}

Could anyone say how to do it?

Share Improve this question edited Nov 20, 2024 at 14:11 SilverCube asked Nov 19, 2024 at 18:44 SilverCubeSilverCube 7342 silver badges12 bronze badges 5
  • Would this help? Link Found here – Mippy Commented Nov 19, 2024 at 18:50
  • Of topic but... does the user of your application know they're actually clicking on the window underneath? – G.M. Commented Nov 19, 2024 at 19:23
  • @G.M. Of course. My window will be just a "frame" and inside this frame the user will work and the user will set frame position and its sizes. – SilverCube Commented Nov 19, 2024 at 19:31
  • I don’t have an example, but you’ll need platform specific code, like using xlib on x11. – TingPing Commented Nov 20, 2024 at 18:25
  • You are already using gdk_window_input_shape_combine_region. That is about "click goes to the window behind". Do you know about gdk_window_shape_combine_region? This one seems to about "visibly transparent" or "see through". Alternatively, for "semi-transparent", something like stackoverflow/questions/3908565/… should be helpful. – Uli Schlachter Commented Nov 21, 2024 at 15:16
Add a comment  | 

1 Answer 1

Reset to default 0

You may explore about using gtk_overlay_set_overlay_pass_through() which internally uses gdk_window_set_pass_through() which you can also use directly if not wanting to use a GtkOverlay widget.

Description of GtkOverlay:pass-through property:

/**
   * GtkOverlay:pass-through:
   *
   * Whether to pass input through the overlay child to the main child.
   * (Of course, this has no effect when set on the main child itself.)
   *
   * Note that this is implemented by calling gdk_window_set_pass_through()
   * on the window that the overlay child is placed in. If the descendents
   * of the overlay child have their own windows, you need to manually call
   * that function on them to achieve the desired effect.
   *
   * Since: 3.18
   */
发布评论

评论列表(0)

  1. 暂无评论