Last active
September 9, 2020 21:44
-
-
Save henkman/2c4753ac579238522fcc0b8849cee15c to your computer and use it in GitHub Desktop.
set wallpaper util
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define WIN32_LEAN_AND_MEAN | |
#include <windows.h> | |
typedef enum | |
{ | |
Tile, | |
Center, | |
Stretch, | |
Fit, | |
Fill | |
} WallpaperStyle; | |
void wallpaper_set(char *path, WallpaperStyle s) | |
{ | |
HKEY key = NULL; | |
if (!SUCCEEDED(HRESULT_FROM_WIN32(RegOpenKeyExA( | |
HKEY_CURRENT_USER, | |
"Control Panel\\Desktop", 0, KEY_READ | KEY_WRITE, &key)))) | |
return; | |
typedef struct | |
{ | |
char *v; | |
size_t len; | |
} str; | |
str style, tile; | |
switch (s) | |
{ | |
case Tile: | |
style.v = "0"; | |
style.len = sizeof("0"); | |
tile.v = "1"; | |
tile.len = sizeof("1"); | |
break; | |
case Center: | |
style.v = "0"; | |
style.len = sizeof("0"); | |
tile.v = "0"; | |
tile.len = sizeof("0"); | |
break; | |
case Stretch: | |
style.v = "2"; | |
style.len = sizeof("2"); | |
tile.v = "0"; | |
tile.len = sizeof("0"); | |
break; | |
case Fit: | |
style.v = "6"; | |
style.len = sizeof("6"); | |
tile.v = "0"; | |
tile.len = sizeof("0"); | |
break; | |
case Fill: | |
style.v = "10"; | |
style.len = sizeof("10"); | |
tile.v = "0"; | |
tile.len = sizeof("0"); | |
break; | |
} | |
RegSetValueExA(key, "WallpaperStyle", 0, REG_SZ, style.v, style.len); | |
RegSetValueExA(key, "TileWallpaper", 0, REG_SZ, tile.v, tile.len); | |
RegCloseKey(key); | |
SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, path, | |
SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE); | |
} | |
int main(int argc, char **argv) | |
{ | |
if (argc != 3) | |
{ | |
printf("Usage: wpset [image] [tile|center|stretch|fit|fill]\n"); | |
return 0; | |
} | |
typedef struct | |
{ | |
char *string; | |
WallpaperStyle style; | |
} StyleString; | |
static StyleString strings[] = { | |
{"tile", Tile}, | |
{"center", Center}, | |
{"stretch", Stretch}, | |
{"fit", Fit}, | |
{"fill", Fill}}; | |
int style = -1; | |
for (unsigned i = 0; i < sizeof(strings) / sizeof(strings[0]); i++) | |
{ | |
if (strcmp(argv[2], strings[i].string) == 0) | |
{ | |
style = strings[i].style; | |
break; | |
} | |
} | |
if (style == -1) | |
{ | |
printf("Usage: wpset [image] [tile|center|stretch|fit|fill]\n"); | |
return 0; | |
} | |
wallpaper_set(argv[1], style); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment