# patch spaper to write filename to registry (restore on reboot) # patch spaper to handle stretching, too. --- SPaper.orig.cpp Tue May 26 00:38:04 1998 +++ SPaper.cpp Tue Mar 16 13:52:37 2004 @@ -7,27 +7,24 @@ // Read the size of the bitmap from the file header and decide whether to tile or centre it // Centre if it's greater than a third of the desktop width and greater than a third of the desktop height -bool Calculate(const char *wallpaper) -{ +bool Calculate(const char *wallpaper) { bool tile(true); HANDLE fhnd = CreateFile (wallpaper,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); - if (fhnd != INVALID_HANDLE_VALUE) - { + if (fhnd != INVALID_HANDLE_VALUE) { BITMAPFILEHEADER fhdr; BITMAPINFOHEADER finfo; DWORD read; - if (ReadFile(fhnd,&fhdr,sizeof(BITMAPFILEHEADER),&read,,NULL)&&(read==sizeof(BITMAPFILEHEADER))) - { - if (ReadFile(fhnd,&finfo,sizeof(BITMAPINFOHEADER),&reaad,NULL)&&(read==sizeof(BITMAPINFOHEADER))) - { + if (ReadFile(fhnd,&fhdr,sizeof(BITMAPFILEHEADER),&read,NULL)&&(read==sizeof(BITMAPFILEHEADER))) { + if (ReadFile(fhnd,&finfo,sizeof(BITMAPINFOHEADER),&read,NULL)&&(read==sizeof(BITMAPINFOHEADER))) { int screenwidth = GetSystemMetrics(SM_CXSCREEN); int screenheight = GetSystemMetrics(SM_CYSCREEN); - if ((finfo.biWidth > (screenwidth/3))&&(finfo.biHeighht > (screenheight/3))) + if ((finfo.biWidth > (screenwidth/3))&&(finfo.biHeight > (screenheight/3))) { tile = FALSE; + } } } @@ -36,13 +33,43 @@ return tile; } +// Read the size of the bitmap from the file header and decide whether to stretch or not +// stretch if it is not terribly out of proportion with desktop shape +bool Calculate2(const char *wallpaper) { + bool stretch(true); + float bAspect, scrAspect; + + HANDLE fhnd = CreateFile (wallpaper,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); + + if (fhnd != INVALID_HANDLE_VALUE) { + BITMAPFILEHEADER fhdr; + BITMAPINFOHEADER finfo; + DWORD read; + + if (ReadFile(fhnd,&fhdr,sizeof(BITMAPFILEHEADER),&read,NULL)&&(read==sizeof(BITMAPFILEHEADER))) { + if (ReadFile(fhnd,&finfo,sizeof(BITMAPINFOHEADER),&read,NULL)&&(read==sizeof(BITMAPINFOHEADER))) { + int screenwidth = GetSystemMetrics(SM_CXSCREEN); + int screenheight = GetSystemMetrics(SM_CYSCREEN); + + bAspect = float(finfo.biWidth)/float(finfo.biHeight); + scrAspect = float(screenwidth)/float(screenheight); + + if (((bAspect/scrAspect>1.3))||((scrAspect/bAspect)>1.3)) { + stretch = FALSE; + } + } + } + + CloseHandle(fhnd); + } + return stretch; +} + // Write the tile parameter to the registry, the 'only' way to control tiling and centering -void SetTileParameter(bool tile) -{ +void SetTileParameter(bool tile) { HKEY key; - if (RegOpenKeyEx(HKEY_CURRENT_USER,"Control Panel\\Deskttop",0,KEY_SET_VALUE,&key)==ERROR_SUCCESS) - { + if (RegOpenKeyEx(HKEY_CURRENT_USER,"Control Panel\\Desktop",0,KEY_SET_VALUE,&key)==ERROR_SUCCESS) { char data[8]; itoa(tile,data,10); @@ -51,78 +78,99 @@ } } +// Write the stretch parameter to the registry, the 'only' way to control stretching +void SetStretchParameter(bool stretch) { + HKEY key; + + if (RegOpenKeyEx(HKEY_CURRENT_USER,"Control Panel\\Desktop",0,KEY_SET_VALUE,&key)==ERROR_SUCCESS) { + char data[8]; + + itoa((2*stretch),data,10); + RegSetValueEx(key,"WallpaperStyle",NULL,REG_SZ,(UCHAR*)data,8); + RegCloseKey(key); + } +} + // Actually set the wallpaper, checks it for a .bmp extension and that it exists -void SetWallpaper(const char *wallpaper, int tile) -{ - if (wallpaper) - { +void SetWallpaper(char *wallpaper, int tile) { + HKEY key; + + if (wallpaper) { char *ext = strrchr(wallpaper,'.'); - if (ext) - { - if (stricmp(ext,".bmp")==0) - { + if (ext) { + if (stricmp(ext,".bmp")==0) { struct stat status; - if (stat(wallpaper,&status)!= -1) - { - if (tile==2) - { + if (stat(wallpaper,&status)!= -1) { + if (tile==3) { + SetStretchParameter(Calculate2(wallpaper)); + } else if (tile==2) { + SetStretchParameter(true); + } else { + SetStretchParameter(false); + } + if (tile==3) { SetTileParameter(Calculate(wallpaper)); + } else if (tile==1) { + SetTileParameter(true); + } else { + SetTileParameter(false); } - else - { - SetTileParameter(bool(tile)); + + if (RegOpenKeyEx(HKEY_CURRENT_USER,"Control Panel\\Desktop",0,KEY_SET_VALUE,&key)==ERROR_SUCCESS) { + RegSetValueEx(key,"Wallpaper",NULL,REG_SZ,(UCHAR*)wallpaper,strlen(wallpaper)); + RegCloseKey(key); } SystemParametersInfo(SPI_SETDESKWALLPAPER,0,(void*)wallpaper,SPIF_SENDWININICHANGE); - } - else - { + } else { cout << "Unable to find file\n"; } - } - else - { + } else { cout << "File must have a .bmp extension\n"; } - } - else - { + } else { cout << "File must have a .bmp extension\n"; } } } -int Tile(const char *tile) -{ +int Tile(const char *tile) { int ret = 2; - if (tile) - { - if (tile[0]=='t') + if (tile) { + if (tile[0]=='t') { ret = 1; - else if (tile[0]=='c') + } else if (tile[0]=='c') { ret = 0; + } else if (tile[0]=='s') { + ret = 2; + } } return ret; } -void main(int argc, char *argv[]) -{ +int main(int argc, char *argv[]) { - switch (argc) - { + switch (argc) { case 1: - cout << "usage: spaper [ or ]\n""; - cout << "where 'wallpaper.bmp' is a valid bitmap, 't' is tile and 'c' is centre.\n"; - cout << "if you don't supply a 't' or 'c' the program will decide based on it's own algorithm.\n"; + cout << "Usage: " << argv[0] << " [t c or s]\n"; + cout << "\twhere 'wallpaper.bmp' is a valid bitmap,\n"; + cout << "\t't' is tile\n"; + cout << "\t'c' is centre\n"; + cout << "\t's' is stretch.\n\n"; + cout << "If you don't supply a 't,' 'c,' or 's,'\n"; + cout << "the program will decide based on it's own algorithm.\n"; + exit(1); break; - case 2: - SetWallpaper(argv[1],2); + SetWallpaper(argv[1],3); break; case 3: SetWallpaper(argv[1],Tile(argv[2])); break; } + exit(0); } + +