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/