简体   繁体   English

在 Linux 中独立于桌面环境或窗口管理器以编程方式更改墙纸

[英]Changing programatically the wallpaper in Linux independently from the desktop environment or the window manager

Linux have got lots of desktop environments (GNOME, KDE, Xfce, Cinnamon...) and windowing systems (X11, Wayland, Mir...) and it seems that each one has got its own way to change the wallpaper. Linux 有很多桌面环境(GNOME、KDE、Xfce、Cinnamon...)和窗口系统(X11、Wayland、Mir...),而且似乎每一个都有自己的方式来改变壁纸。 Are there some high-level libraries, especially in C++ (and Qt 5), which enables the developer to change programatically the wallpaper in Linux, regardless of the window management or the desktop management?是否有一些高级库,尤其是在 C++(和 Qt 5)中,使开发人员能够在 Linux 中以编程方式更改墙纸,而不管窗口管理还是桌面管理? I am looking for something like that:我正在寻找类似的东西:

#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;
}

Try the solution of "Andrew Y" in the post : Changing wallpaper on Linux programmatically在帖子中尝试“Andrew Y”的解决方案: 以编程方式在Linux上更改壁纸

He claims that his solution is not dependant of the higher layer toolkits, so it's should work for any linux desktop environments.他声称他的解决方案不依赖于更高层的工具包,因此它应该适用于任何 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM