在Linux上以编程方式改变壁纸 [英] Changing wallpaper on Linux programmatically

查看:220
本文介绍了在Linux上以编程方式改变壁纸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C / C ++程序中更改Linux桌面上的壁纸(使用GNOME)?有没有系统API来做这件事?

How would I change the wallpaper on a Linux desktop (using GNOME) within a C/C++ program? Is there a system API to do it?

推荐答案

虽然问题是gnome特定的,还有一种方法来处理在高层工具包上不是depepndant的壁纸。你应该能够通过研究 xsetroot.c ,我最喜欢的部分在这里复制:

Though the question was gnome-specific, there's also a way to deal with the wallpaper that is not depepndant on the higher layer toolkits. You should be able to deal with the root window (which the wallpaper is, in fact) by studying the source of xsetroot.c, the most interesting part of which I copypaste here:

static void
SetBackgroundToBitmap(Pixmap bitmap, unsigned int width, unsigned int height)
{
    Pixmap pix;
    GC gc;
    XGCValues gc_init;

    gc_init.foreground = NameToPixel(fore_color, BlackPixel(dpy, screen));
    gc_init.background = NameToPixel(back_color, WhitePixel(dpy, screen));
    if (reverse) {
        unsigned long temp=gc_init.foreground;
        gc_init.foreground=gc_init.background;
        gc_init.background=temp;
    }
    gc = XCreateGC(dpy, root, GCForeground|GCBackground, &gc_init);
    pix = XCreatePixmap(dpy, root, width, height,
                        (unsigned int)DefaultDepth(dpy, screen));
    XCopyPlane(dpy, bitmap, pix, gc, 0, 0, width, height, 0, 0, (unsigned long)1);
    XSetWindowBackgroundPixmap(dpy, root, pix);
    XFreeGC(dpy, gc);
    XFreePixmap(dpy, bitmap);
    if (save_colors)
        save_pixmap = pix;
    else
        XFreePixmap(dpy, pix);
    XClearWindow(dpy, root);
    unsave_past = 1;
}

这篇关于在Linux上以编程方式改变壁纸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆