如何使Gtk窗口背景透明?

如何使Gtk窗口背景透明?,第1张

概述我想使Gtk窗口背景透明,以便只有窗口中的窗口小部件可见.我发现了几个教程: http://mikehearn.wordpress.com/2006/03/26/gtk-windows-with-alpha-channels/ http://macslow.thepimp.net/?p=26 但他们似乎都听“暴露”事件,然后委托开罗做渲染.这意味着添加到窗口的其他小部件不会被渲染(例如,我已经尝 我想使Gtk窗口的背景透明,以便只有窗口中的窗口小部件可见.我发现了几个教程:

http://mikehearn.wordpress.com/2006/03/26/gtk-windows-with-alpha-channels/@H_419_3@

http://macslow.thepimp.net/?p=26@H_419_3@

但他们似乎都听“暴露”事件,然后委托开罗做渲染.这意味着添加到窗口的其他小部件不会被渲染(例如,我已经尝试向窗口中添加一个按钮).@H_419_3@

我看到GtkWidget有一个方法modify-bg:http://library.gnome.org/devel/gtk/stable/GtkWidget.html#gtk-widget-modify-bg@H_419_3@

但是,Gdkcolor似乎没有接受透明度参数:http://library.gnome.org/devel/gdk/stable/gdk-Colormaps-and-Colors.html@H_419_3@

我也尝试过GtkWindow.set_opacity方法,但这也设置了窗口内容的不透明度,这不是我想要的.@H_419_3@

我会感谢任何人都可以提供的任何指导.@H_419_3@解决方法 我改变了 alphademo的例子来绘制一个按钮而不是红色的圆圈.

该应用程序在400×400透明窗口上绘制按钮.@H_419_3@

当您点击窗口时,应用程序将显示/隐藏标题栏.@H_419_3@

#include <gtk/gtk.h>#include <gdk/gdkscreen.h>#include <cairo.h>static voID screen_changed(GtkWidget *Widget,GdkScreen *old_screen,gpointer user_data);static gboolean expose(GtkWidget *Widget,GdkEventExpose *event,gpointer user_data);static voID clicked(GtkWindow *win,GdkEventbutton *event,gpointer user_data);int main(int argc,char **argv){    gtk_init(&argc,&argv);    GtkWidget *window = gtk_window_new(GTK_WINDOW_topLEVEL);    gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);    gtk_window_set_default_size(GTK_WINDOW(window),400,400);    gtk_window_set_Title(GTK_WINDOW(window),"Alpha Demo");    g_signal_connect(G_OBJECT(window),"delete-event",gtk_main_quit,NulL);    gtk_Widget_set_app_paintable(window,TRUE);    g_signal_connect(G_OBJECT(window),"expose-event",G_CALLBACK(expose),NulL);    g_signal_connect(G_OBJECT(window),"screen-changed",G_CALLBACK(screen_changed),NulL);    gtk_window_set_decorated(GTK_WINDOW(window),FALSE);    gtk_Widget_add_events(window,GDK_button_PRESS_MASK);    g_signal_connect(G_OBJECT(window),"button-press-event",G_CALLBACK(clicked),NulL);    GtkWidget* fixed_container = gtk_fixed_new();    gtk_container_add(GTK_CONTAINER(window),fixed_container);    GtkWidget* button = gtk_button_new_with_label("button1");    gtk_Widget_set_size_request(button,100,100);    gtk_container_add(GTK_CONTAINER(fixed_container),button);    screen_changed(window,NulL,NulL);    gtk_Widget_show_all(window);    gtk_main();    return 0;}gboolean supports_Alpha = FALSE;static voID screen_changed(GtkWidget *Widget,gpointer userdata){    /* To check if the display supports Alpha channels,get the colormap */    GdkScreen *screen = gtk_Widget_get_screen(Widget);    Gdkcolormap *colormap = gdk_screen_get_rgba_colormap(screen);    if (!colormap)    {        printf("Your screen does not support Alpha channels!\n");        colormap = gdk_screen_get_rgb_colormap(screen);        supports_Alpha = FALSE;    }    else    {        printf("Your screen supports Alpha channels!\n");        supports_Alpha = TRUE;    }    gtk_Widget_set_colormap(Widget,colormap);}static gboolean expose(GtkWidget *Widget,gpointer userdata){   cairo_t *cr = gdk_cairo_create(Widget->window);    if (supports_Alpha)        cairo_set_source_rgba (cr,1.0,0.0); /* transparent */    else        cairo_set_source_rgb (cr,1.0); /* opaque white */    /* draw the background */    cairo_set_operator (cr,CAIRO_OPERATOR_SOURCE);    cairo_paint (cr);    cairo_destroy(cr);    return FALSE;}static voID clicked(GtkWindow *win,gpointer user_data){    /* toggle window manager frames */    gtk_window_set_decorated(win,!gtk_window_get_decorated(win));}

编译在Ubuntu 10.04上:@H_419_3@

gcc Alpha.c -o Alpha -I/usr/include/gtk-2.0 -I/usr/lib/gtk-2.0/include -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/atk-1.0 -lgtk-x11-2.0
总结

以上是内存溢出为你收集整理的如何使Gtk窗口背景透明?全部内容,希望文章能够帮你解决如何使Gtk窗口背景透明?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1233636.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存