Skip to content

Instantly share code, notes, and snippets.

@Darker
Created November 23, 2014 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Darker/ecf315591def7ae25c5d to your computer and use it in GitHub Desktop.
Save Darker/ecf315591def7ae25c5d to your computer and use it in GitHub Desktop.
Generate a background and set is as Windows wallpaper
// stripes.cpp : Defines the entry point for the console application.
//
//
// Magick++ demo to generate a simple text button
//
// Copyright Bob Friesenhahn, 1999, 2000, 2001, 2003
//
#define _CRT_SECURE_NO_WARNINGS
#include <Magick++.h>
#include <string>
#include <cstdlib>
#include <sstream>
#include <iostream>
#include <random>
#include "wtypes.h"
#include "windows.h"
#include "wininet.h"
#include "shlobj.h"
#include <sys/stat.h>
using namespace std;
using namespace Magick;
void SetWallpaper(const char*);
void GetDesktopResolution(int&, int&);
bool exists (const std::string& name);
bool exists (const char* name);
int main( int argc, char ** argv)
{
// Initialize ImageMagick install location for Windows
InitializeMagick(*argv);
const char * image_name = "button_out.gif";
try {
int width, height;
GetDesktopResolution(width, height);
Image image;
image.size(Geometry(width,height));
//Random numbers
typedef std::mt19937 rng_type;
std::uniform_int_distribution<rng_type::result_type> colordist(0, 255);
std::uniform_int_distribution<rng_type::result_type> stripewidth(5, 20);
rng_type::result_type const seedval = (int)time (NULL); // get this from somewhere
rng_type hue_r;
rng_type stripe_width;
hue_r.seed(seedval);
stripe_width.seed(seedval+rand());
bool blackSeparators = colordist(hue_r)%2 ||true;
//Start with random hue
float hue = (int)colordist(hue_r);
// Set draw options
//tak to te vudu muset sploknout...
//image.strokeColor("red");
Color c;
int lasti = 0;
int itr = 0;
for(int i=stripewidth(stripe_width); ; i+=stripewidth(stripe_width)) {
if(blackSeparators && itr%2==1) {
c = Color("#000000");
}
else {
stringstream s;
s<<"hsl("<<(((int)hue) % 255)<<", 240, 100)";
//Rotate the hue by golden angle (137.5/360)*255
hue += 97.39583;
//s<<"hsl(59, 255, "<<((int)rand() % 2)*130<<")";
c = Color(s.str().c_str());
}
image.fillColor(Color(c));
image.draw( DrawableRectangle(lasti,0, i,height) );
if(i>width)
break;
lasti = i;
itr++;
}
//image.fillColor(Color("green"));
//image.strokeWidth(0);
//image.draw( DrawableRectangle(200,200, 270,170) );
bool written = false;
try {
image.write(image_name);
written = true;
}
catch( exception &error_ )
{
cout << "Image::write exception: " << error_.what() << endl;
system("Pause");
return 1;
}
if(written) {
if(!exists(image_name)) {
wcout << "The file "<<image_name<<" does not exist!" << endl;
}
else {
SetWallpaper(image_name);
}
}
//SystemParametersInfo( SPI_SETDESKWALLPAPER, 0, (PVOID)"button_out.gif", SPIF_UPDATEINIFILE );
image.display();
// Display on screen
// button.display();
}
catch( exception &error_ )
{
cout << "Caught exception: " << error_.what() << endl;
system("Pause");
return 1;
}
/*
*/
system("Pause");
return 0;
}
bool exists (const std::string& name) {
struct stat buffer;
return (stat (name.c_str(), &buffer) == 0);
}
bool exists(const char* name) {
struct stat buffer;
return (stat (name, &buffer) == 0);
}
// Get the horizontal and vertical screen sizes in pixel
void GetDesktopResolution(int& horizontal, int& vertical)
{
RECT desktop;
// Get a handle to the desktop window
const HWND hDesktop = GetDesktopWindow();
// Get the size of screen to the variable desktop
GetWindowRect(hDesktop, &desktop);
// The top left corner will have coordinates (0,0)
// and the bottom right corner will have coordinates
// (horizontal, vertical)
horizontal = desktop.right;
vertical = desktop.bottom;
}
void SetWallpaper(const char* file){
wchar_t widename[MAX_PATH+1], widepath[MAX_PATH+1];
mbstowcs(widename, file, sizeof(widename) / sizeof(wchar_t));
GetFullPathName(widename, MAX_PATH+1,widepath, NULL);
CoInitializeEx(0,COINIT_APARTMENTTHREADED);
IActiveDesktop* desktop;
HRESULT status = CoCreateInstance(CLSID_ActiveDesktop,NULL,CLSCTX_INPROC_SERVER,IID_IActiveDesktop,(void**)&desktop);
WALLPAPEROPT wOption;
ZeroMemory(&wOption, sizeof(WALLPAPEROPT));
wOption.dwSize=sizeof(WALLPAPEROPT);
wOption.dwStyle = WPSTYLE_CENTER;
status = desktop->SetWallpaper(widepath,0);
//wcout <<"0x"<<hex<< (unsigned int)status << endl;
status = desktop->SetWallpaperOptions(&wOption,0);
//wcout <<"0x"<<hex<< (unsigned int) status << endl;
status = desktop->ApplyChanges(AD_APPLY_ALL);
//wcout <<"0x"<<hex<< (unsigned int) status << endl;
if(status==0x80070002) {
wcout <<"Not found: "<<widepath<< endl;
}
else if (status!=0) {
wcout <<"Error: "<<widepath<< endl;
}
desktop->Release();
CoUninitialize();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment