gpt4 book ai didi

c++ - 以编程方式在 Linux 上更改墙纸

转载 作者:IT王子 更新时间:2023-10-29 00:38:13 26 4
gpt4 key购买 nike

如何在 C/C++ 程序中更改 Linux 桌面(使用 GNOME)上的墙纸?是否有系统 API 可以做到这一点?

最佳答案

你可以使用 gconf图书馆来做。下面的示例是一个完整的改变背景的程序:

// bkgmanage.c
#include <glib.h>
#include <gconf/gconf-client.h>
#include <stdio.h>

typedef enum {
WALLPAPER_ALIGN_TILED = 0,
WALLPAPER_ALIGN_CENTERED = 1,
WALLPAPER_ALIGN_STRETCHED = 2,
WALLPAPER_ALIGN_SCALED = 3,
WALLPAPER_NONE = 4
} WallpaperAlign;

gboolean set_as_wallpaper( const gchar *image_path, WallpaperAlign align )
{
GConfClient *client;
char *options = "none";

client = gconf_client_get_default();

// TODO: check that image_path is a file
if ( image_path == NULL ) options = "none";
else {
gconf_client_set_string( client,
"/desktop/gnome/background/picture_filename",
image_path,
NULL );
switch ( align ) {
case WALLPAPER_ALIGN_TILED: options = "wallpaper"; break;
case WALLPAPER_ALIGN_CENTERED: options = "centered"; break;
case WALLPAPER_ALIGN_STRETCHED: options = "stretched"; break;
case WALLPAPER_ALIGN_SCALED: options = "scaled"; break;
case WALLPAPER_NONE: options = "none"; break;
}
}
gboolean result = gconf_client_set_string( client,
"/desktop/gnome/background/picture_options",
options,
NULL);
g_object_unref( G_OBJECT(client) );

return result;
}

int main(int argc, const char* argv[])
{
if ( argc > 1 ) {
printf( "Setting %s as wallpaper... ", argv[1] );
if ( set_as_wallpaper( argv[1], WALLPAPER_ALIGN_STRETCHED ) ) printf( "Ok\n" );
else printf( "Failed\n" );
} else printf( "Usage: ./bkgmanage <filename>\n" );

return 0;
}

以上来源基于gthumb项目。它可以用以下字符串编译:

gcc -Wall -g `pkg-config --libs --cflags glib-2.0 gconf-2.0` bkgmanage.c -o bkgmanage

关于c++ - 以编程方式在 Linux 上更改墙纸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1220826/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com