2

Linux 有很多桌面环境(GNOME、KDE、Xfce、Cinnamon...)和窗口系统(X11、Wayland、Mir...),似乎每个都有自己的方式来更改壁纸。是否有一些高级库,特别是在 C++(和 Qt 5)中,使开发人员能够以编程方式更改 Linux 中的墙纸,而不管窗口管理或桌面管理?我正在寻找类似的东西:

#include <the_lib>
#include <cstdlib>

int main(int argc, char ** argv) {
    std::string theNewWallpaper = "path/to/my/wallpaper.jpg";
    // Or a file, an image, or something else representing the wallpaper.

    TheLib::changeWallpaper(theNewWallpaper);
    // or a more complicated piece of code which does the same.

    return EXIT_SUCCESS;
}
4

1 回答 1

2

在帖子中尝试“Andrew Y”的解决方案:以编程方式更改 Linux 上的壁纸

他声称他的解决方案不依赖于更高层的工具包,因此它应该适用于任何 linux 桌面环境。

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;
}
于 2015-09-21T07:20:41.113 回答