Wednesday, May 2, 2007 by RomanDA | Discussion: DesktopX Tutorials
#9 - Wallpaper Changer
A series by RomanDA
Listing of other DX Tutorials:
Today's Lesson: "Wallpaper Changer"
In this lesson we will
walk thru a few built-in commands in DX that allow you to create a
We will start simple and add to the script. It is amazing all the things that are built into DX.
We will use the file dialog box, the wallpaper changer, create a "right-click" menu and even a custom preference window.
For this and all the Step-By-Step DX Tutorials you will need to purchase DesktopX for $14.95 from Stardock.
Lets get started.
STEP 1 - Using the System.FileOpenDialog & System.SetWallpaper
Create a new object (look at the old tutorials for info on how to do this).
Once it is created (you can use any image you want, but for me I'm just going to use the standard round DX image.
Add the following script:
Looking at the above script we have some new things in here.
The FileOpenDialog is a built-in
DesktopX function. It allows you to select a file from a standard windows
looking open dialog box.
The format is very simple.
- "Select Wallpaper..." - Title for the window
- "" - default file (say you want it to come up with a file selected, this would be that file name)
- FolderName - The default folder to open
- "Wallpapers |*.jpg" - this is the file Extension to use, in this case JPG
- 0 - flags (to learn more about all these flags look over: http://www.stardock.com/products/desktopx/documentation/scripting/system_namespace.html )
The way this works, is you click on the object, it pops open the FileOpenDialog , then you select your wallpaper this file name gets saved into the Wallpaper var.
This is another built-in DesktopX command. It does exactly what it says "SetWallpaper". It gets passed 2 items:
- wallpaper - the file we picked from above, has to have the entire path (IE: c:\wallpaper\mywallpaper.jpg )
- 3 - There are 3 options for
this: (we will add a right-click menu later to select this)
- 0 = use default wallpaper
- 1 = Center wallpaper
- 2 = Tile wallpaper
- 3 = Stretch wallpaper
So with just the above script you have created a object/widget that allows you to click, pick a file, and have it set the wallpaper to that file.
This is good, but every time it loads it points to that same folder, what if you had multiple folders? What if you want it to STORE what folder you picked the last time?
STEP 2 - Pulling the Folder name from the Open Window Dialog box
Lets add a few lines of code to our script.
Our entire script looks like this now:
By Adding dim FolderName at the
very top of the script it will make that variable Global, meaning it will be
able to read/write to that var from any/all functions/subs.
If you don't add this it will not be able to pull the value of FolderName from the other functions.
We are going to add a way to pull
the Folder name from the one we selected in our Dialog box.
To do this we need to "pull" the file name, and then strip that out from the var.
We are going to assume that the
user clicked on another drive/folder/filename from the open dialog.
Let's say I picked: D:\My data\Wallpapers\ZubazisUgly.jpg
Breaking this apart:
Set filesys = CreateObject("Scripting.FileSystemObject")
Sets the var FILESYS to a FileSystemObject
f = filesys.GetBaseName(wallpaper)Pulls the File name from the entire path
In this case it pulls: ZubazisUgly.jpg
t = instr(1,wallpaper,f)This finds the position of the file name in the path, so that we can strip it out.
FolderName = left(wallpaper,t-1)
This pulls the LEFT T-1 characters from the string so we end up with just the path:
This is all well and good, but the next time we click the button it will still use the C:\wallpaper folder, so we want to add a little more code to save and restore this var.
STEP 3 - Adding the Save / Load registry functions
We are going to add (2) new functions to the script:
- The SaveSettings and LoadSettings are functions we talked about in Tutorial #6.
- We are storing the FolderName into the reg key "HKCU\SOFTWARE\desktopx\wallpaperpicker\folder"
- We use a DEFAULT of the
Current DesktopX Executable Directory so that it loads up using the folder
the EXE was run from.
You don't want to hard-code a folder here because not everyone's pc will have that folder location.
STEP 4 - Putting it all together
Lets see what the entire script looks like now, with all the above included.
This should allow you to click and open a dialog box, allowing you to pick a folder/file to set the wallpaper, then it sets the wallpaper, and stores the selected folder.
STEP 5 - Adding a Right-Click menu for options.
The next step is to add a menu so
we can pick from default/centered/tiled/stretched for the wallpaper.
I made a tutorial a while back on how to add a Right-click menu - CLICK HERE to view that, I'm not going to go thru this step by step, I'm just going to give you the working code. If you want a break down of how the menu works, please use the above link to the other Tutorial.
The code below will popup a menu
when you right-click. It gives you 4 options, from changing to the default
wallpaper, to making it tiled/stretched.
It will do this immediately so you can see what it does, it also stores the info in a Var (WPFormat) so that the next time you pick a wallpaper it will use this same setting. With 1 exception the 0. This is designed to set the wallpaper to the default, so we don't want to STORE that, we will default it back to 3 (stretched).
STEP 6 - Putting it all together
We are going to add a few more DIMs at the top:
- Dim Wallpaper - stores the selected Wallpaper name
- Dim WPFormat - Stores the Current Wallpaper "format" - stretched/centered/etc.
Here is the FULL script, if you just want to copy/paste this into your object, its your call.
I hope you took the time to enter the code (not just copy/pasted the entire thing) so you could work thru the tutorial step-by-step and see how things work. You will notice I added a small IF under the Open Dialog box so that it only does anything if you select a file, it will crash out without that code if you don't select a file.
I hope you have enjoyed this step into DX, and look forward to the next installment..
AKA: David A. Roman