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

标签 c++ linux qt wallpaper desktop-wallpaper

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

最佳答案

尝试帖子中“Andrew Y”的解决方案:Changing wallpaper on Linux programmatically

他声称他的解决方案不依赖于更高层的工具包,因此它应该适用于任何 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;
}

关于c++ - 独立于桌面环境或窗口管理器以编程方式更改 Linux 中的墙纸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32684687/

相关文章:

c++ - 虚拟和非虚拟继承的混合

c++ - 二进制兼容性因虚函数而中断

sql - Linux 上的 Postgres 文字回车错误

c# - 如何在 Qt 中更快地读取数据?

qt - 将 QList<QVariant> 存储到 Qt 中的 PostgreSQL 数据库

c++ - 如何更改 CMFCToolBar 运行时

android - 为什么无法在Android上使用Qt在app目录中创建多个文件

python - Selenium 远程 WebDriver

python - 是python2不愿意释放内存吗?

linux - 如何在linux中打开系统日期、声音、显示属性?