c - gtk4透明绘图区
问题描述
我目前正在将一些东西移植到 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 中产生透明窗口?
解决方案
推荐阅读
- vue.js - Vue路由器可以通过动态参数区分确切的路由匹配和匹配吗?
- c - 如何使用 Ptrace_interrupt 命令
- java - 无法执行 android:onClick 的方法,原因是:java.lang.reflect.InvocationTargetException
- javascript - 如何使用jQuery从动态生成的元素中获取数据
- python-3.x - 使用 NLTK 的句子级别 Bleu 分数
- python - 如何读取 Pandas DataFrame 的前 10 行?
- android - Flutter Android Launcher,应用程序无法显示容器是否有轮播
- java - 如何将 .jar 文件转换为 Windows 服务
- objective-c - 如何从东到西,从南到北显示整个谷歌地图的经纬度?
- mongodb - 使用 MongoDB 代替 Amazon S3 作为文件存储有哪些优势?