Learning DX Step-By-Step - #9
Wallpaper Changer
Wednesday, May 2, 2007 by RomanDA | Discussion: DesktopX Tutorials
Step-by-Step Tutorials |
#9 - Wallpaper Changer |
A series by RomanDA |
Listing of other DX Tutorials:
Click
here
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
Wallpaper Changer.
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:
Vbscript Code | |
Function Object_OnLButtonUp(x, y, Dragged)
|
Looking at the above script we have some new things in here.
Vbscript Code | |
System.FileOpenDialog("Select Wallpaper...", "", FolderName , "Wallpapers|*.jpg", 0) |
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: https://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.
Vbscript Code | |
System.SetWallpaper wallpaper, 3 |
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:
Vbscript Code | |
'Called when the script is executed |
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.
Vbscript Code | |
Dim foldername |
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 FileSystemObjectf = filesys.GetBaseName(wallpaper)
Pulls the File name from the entire path
In this case it pulls: ZubazisUgly.jpgt = 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:
D:\My data\Wallpapers\
Vbscript Code | |
Function Object_OnLButtonUp(x, y, Dragged) |
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.
Vbscript Code | |
Function SaveSettings() |
STEP 4 - Putting it all together
Lets see what the entire script looks like now, with all the above included.
Vbscript Code | |
Dim foldername |
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).
Vbscript Code | |
Function Object_OnRButtonUpEx(obj,x,y,dragged)
|
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.
Vbscript Code | |
Dim foldername
|
CONCLUSION
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..
Enjoy, RomanDA AKA: David A. Roman http://romanda.wincustomize.com |
Reply #3 Wednesday, May 2, 2007 12:17 PM
Reply #4 Wednesday, May 2, 2007 12:47 PM
Im glad people are using these.. makes me feel good.
Reply #5 Wednesday, May 2, 2007 3:15 PM
Hope it has helped save you some time. Nice work as usual.
Reply #6 Wednesday, May 2, 2007 8:17 PM
Reply #7 Sunday, May 6, 2007 11:13 AM
just see that RomanDA you are really awesome
Reply #8 Sunday, May 6, 2007 10:11 PM
Reply #9 Monday, May 7, 2007 4:10 AM
Reply #10 Monday, May 7, 2007 9:00 AM
please remove that photo.. my head hurts bad enough today.. maybe replace it with your daughters pic.. HEHE
RomanDA runs away
Reply #11 Thursday, November 1, 2007 7:56 PM
First of all, I was actually looking for a pic of vStyler to mess with and 'yours' showed up when I Googled his name. So, thanks for the additional ammo for me to pick on you.
Reply #12 Thursday, November 1, 2007 8:04 PM
Reply #13 Thursday, November 1, 2007 9:18 PM
Please login to comment and/or vote for this skin.
Welcome Guest! Please take the time to register with us.
There are many great features available to you once you register, including:
- Richer content, access to many features that are disabled for guests like commenting on the forums and downloading skins.
- Access to a great community, with a massive database of many, many areas of interest.
- Access to contests & subscription offers like exclusive emails.
- It's simple, and FREE!
Reply #1 Wednesday, May 2, 2007 10:17 AM