0

我目前正在将一些东西移植到 GTK4 中,并且大部分已经完成。但是,我根本无法获得透明度。大多数事情发生在需要透明的绘图区域内(对桌面的其余部分)。

我试图让绘图区域示例使用透明度,但它不起作用。这是我当前的代码:

#include <gtk/gtk.h>

/* Surface to store current scribbles */
static cairo_surface_t *surface = NULL;

static void
clear_surface(void)
{
    cairo_t *cr;

    cr = cairo_create(surface);

    cairo_set_source_rgba(cr, 1, 1, 0.5, 0.5);
    cairo_paint_with_alpha(cr, 0.5);

    cairo_destroy(cr);
}

/* Create a new surface of the appropriate size to store our scribbles */
static void
resize_cb(GtkWidget *widget,
          int width,
          int height,
          gpointer data)
{
    if (surface)
    {
        cairo_surface_destroy(surface);
        surface = NULL;
    }

    if (gtk_native_get_surface(gtk_widget_get_native(widget)))
    {
        surface = gdk_surface_create_similar_surface(gtk_native_get_surface(gtk_widget_get_native(widget)),
                                                     CAIRO_CONTENT_COLOR_ALPHA,
                                                     gtk_widget_get_width(widget),
                                                     gtk_widget_get_height(widget));

        /* Initialize the surface to white */
        clear_surface();
    }
}

static void
draw_cb(GtkDrawingArea *drawing_area,
        cairo_t *cr,
        int width,
        int height,
        gpointer data)
{
    cairo_set_source_surface(cr, surface, 0, 0);
    cairo_paint(cr);
}

static void
activate(GtkApplication *app,
         gpointer user_data)
{
    GtkWidget *window;
    GtkWidget *frame;
    GtkWidget *drawing_area;

    window = gtk_application_window_new(app);
    gtk_window_set_title(GTK_WINDOW(window), "Drawing Area");
    frame = gtk_frame_new(NULL);
    gtk_window_set_child(GTK_WINDOW(window), frame);
    drawing_area = gtk_drawing_area_new();
    gtk_frame_set_child(GTK_FRAME(frame), drawing_area);
    gtk_drawing_area_set_draw_func(GTK_DRAWING_AREA(drawing_area), draw_cb, NULL, NULL);
    g_signal_connect_after(drawing_area, "resize", G_CALLBACK(resize_cb), NULL);
    gtk_widget_show(window);
}

int main(int argc,
         char **argv)
{
    GtkApplication *app;
    int status;

    app = gtk_application_new("org.gtk.example", G_APPLICATION_FLAGS_NONE);
    g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
    status = g_application_run(G_APPLICATION(app), argc, argv);
    g_object_unref(app);

    return status;
}

在 GTK3 中,您需要将视觉效果设置为 RGB,但这应该在 gtk4 中消失(https://developer.gnome.org/gtk4/stable/gtk-migrating-3-to-4.html#id-1.7.4.3 .10 )。

有谁知道为什么这段代码不会在 GTK4 中产生透明窗口?

4

0 回答 0