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)
  If Dragged = False Then
    '--- Change the line below to pick the folder
    '--- where you keep your wallpapers -----
    FolderName = "C:\wallpapers"
    wallpaper = System.FileOpenDialog("Select Wallpaper...", "", FolderName , "Wallpapers|*.jpg", 0)
    System.SetWallpaper wallpaper, 3
  End If
End Function

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. 

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
Sub Object_OnScriptEnter

End Sub

'Called when the script is terminated
Sub Object_OnScriptExit

End Sub

Function Object_OnLButtonUp(x, y, Dragged)
  If Dragged = False Then
    FolderName = "C:\wallpaper\"
    wallpaper = System.FileOpenDialog("Select Wallpaper...", "", FolderName , "Wallpapers|*.jpg", 0)
    System.SetWallpaper wallpaper, 3
  End If
End Function

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

'Called when the script is executed
Sub Object_OnScriptEnter

End Sub

'Called when the script is terminated
Sub Object_OnScriptExit

End Sub

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:
    D:\My data\Wallpapers\
Vbscript  Code
  Function Object_OnLButtonUp(x, y, Dragged)
  Dim filesys
  If Dragged = False Then
    FolderName = "C:\wallpaper\"
    wallpaper = System.FileOpenDialog("Select Wallpaper...", "", FolderName , "Wallpapers|*.jpg", 0)
    System.SetWallpaper wallpaper, 3
    Set filesys = CreateObject("Scripting.FileSystemObject")
    f = filesys.GetBaseName(wallpaper)
    t = instr(1,wallpaper,f)
    FolderName = left(wallpaper,t-1)
  End If
End Function

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()
  Set Sh = CreateObject("WScript.Shell")
  Sh.RegWrite "HKCU\SOFTWARE\desktopx\wallpaperpicker\folder", foldername
  Set Sh = Nothing
End Function

Function LoadSettings()
  foldername = Desktopx.ExecutableDirectory
  On Error Resume Next
  Set Sh = CreateObject("WScript.Shell")
  FolderName = Sh.RegRead("HKCU\SOFTWARE\desktopx\wallpaperpicker\folder")
  Set Sh = Nothing
  Err.Clear
End Function

STEP 4 - Putting it all together


Lets see what the entire script looks like now, with all the above included.

Vbscript  Code
  Dim foldername

'Called when the script is executed
Sub Object_OnScriptEnter

End Sub

'Called when the script is terminated
Sub Object_OnScriptExit

End Sub

Function Object_OnLButtonUp(x, y, Dragged)
  Dim filesys
  If Dragged = False Then
    Call LoadSettings()
    wallpaper = System.FileOpenDialog("Select Wallpaper...", "", FolderName , "Wallpapers|*.jpg", 0)
    System.SetWallpaper wallpaper, 3
    Set filesys = CreateObject("Scripting.FileSystemObject")
    f = filesys.GetBaseName(wallpaper)
    t = instr(1,wallpaper,f)
    foldername = left(wallpaper,t-1)
    Call SaveSettings()
  End If
End Function

Function SaveSettings()
  Set Sh = CreateObject("WScript.Shell")
  Sh.RegWrite "HKCU\SOFTWARE\desktopx\wallpaperpicker\folder", foldername
  Set Sh = Nothing
End Function

Function LoadSettings()
  foldername = Desktopx.ExecutableDirectory
  On Error Resume Next
  Set Sh = CreateObject("WScript.Shell")
  FolderName = Sh.RegRead("HKCU\SOFTWARE\desktopx\wallpaperpicker\folder")
  Set Sh = Nothing
  Err.Clear
End Function

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)
  If Not dragged Then
    Object_OnRButtonUpEx = True
    Set mainmenu = nothing
    Set mainmenu = DesktopX.CreatePopupMenu
    mainmenu.AppendMenu 0, 0, "Default Wallpaper"
    mainmenu.AppendMenu 0, 1, "Center Wallpaper"
    mainmenu.AppendMenu 0, 2, "Tile Wallpaper"
    mainmenu.AppendMenu 0, 3, "Stretch Wallpaper"
    result = mainmenu.TrackPopupMenu(0, System.CursorX, System.CursorY)
    Select Case result
      Case 0
        WPFormat = 3
        System.SetWallpaper "", 0
      Case 1
        WPFormat = 1
        System.SetWallpaper wallpaper, WPFormat
      Case 2
        WPFormat = 2
        System.SetWallpaper wallpaper, WPFormat
      Case 3
        WPFormat = 3
        System.SetWallpaper wallpaper, WPFormat
    End Select
    Set mainmenu = nothing
  End If
End Function

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
Dim Wallpaper
Dim WPFormat

'Called when the script is executed
Sub Object_OnScriptEnter
  Call LoadSettings()
End Sub

'Called when the script is terminated
Sub Object_OnScriptExit
  Call SaveSettings()
End Sub

Function Object_OnLButtonUp(x, y, Dragged)
  Dim filesys
  If Dragged = False Then
    Call LoadSettings()
    wallpaper = System.FileOpenDialog("Select Wallpaper...", "", FolderName , "Wallpapers|*.jpg", 0)
    If len(wallpaper) > 2 Then
      System.SetWallpaper wallpaper, WPFormat
      Set filesys = CreateObject("Scripting.FileSystemObject")
      f = filesys.GetBaseName(wallpaper)
      t = instr(1,wallpaper,f)
      foldername = left(wallpaper,t-1)
      Call SaveSettings()
    End If
  End If
End Function

Function SaveSettings()
  Set Sh = CreateObject("WScript.Shell")
  Sh.RegWrite "HKCU\SOFTWARE\desktopx\wallpaperpicker\folder", foldername
  Sh.RegWrite "HKCU\SOFTWARE\desktopx\wallpaperpicker\WPFormat", WPFormat
  Set Sh = Nothing
End Function

Function LoadSettings()
  foldername = Desktopx.ExecutableDirectory
  WPFormat= 3 'use stretch for default (change to whatever you want)
  On Error Resume Next
  Set Sh = CreateObject("WScript.Shell")
  FolderName = Sh.RegRead("HKCU\SOFTWARE\desktopx\wallpaperpicker\folder")
  WPFormat = Sh.RegRead("HKCU\SOFTWARE\desktopx\wallpaperpicker\WPFormat")
  Wallpaper = Sh.RegRead("HKEY_CURRENT_USER\Control Panel\Desktop\Wallpaper")
  Set Sh = Nothing
  Err.Clear
End Function

Function Object_OnRButtonUpEx(obj,x,y,dragged)
  If Not dragged Then
    Object_OnRButtonUpEx = True
    Set mainmenu = nothing
    Set mainmenu = DesktopX.CreatePopupMenu
    mainmenu.AppendMenu 0, 0, "Default Wallpaper"
    mainmenu.AppendMenu 0, 1, "Center Wallpaper"
    mainmenu.AppendMenu 0, 2, "Tile Wallpaper"
    mainmenu.AppendMenu 0, 3, "Stretch Wallpaper"
    result = mainmenu.TrackPopupMenu(0, System.CursorX, System.CursorY)
    Select Case result
    Case 0
      WPFormat = 0
      System.SetWallpaper "", WPFormat
    Case 1
      WPFormat = 1
      System.SetWallpaper wallpaper, WPFormat   
    Case 2
      WPFormat = 2
      System.SetWallpaper wallpaper, WPFormat   
    Case 3
      WPFormat = 3
      System.SetWallpaper wallpaper, WPFormat   
    End Select
    Set mainmenu = nothing
  End If
End Function

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
ZubaZ
Reply #1 Wednesday, May 2, 2007 10:17 AM
Great tutorial!  I like the way you mixed common DX functions and your own code from other tutorials to build this one.
Alternate Setting
Reply #2 Wednesday, May 2, 2007 11:01 AM
Tks for taking the time
jpkylegirl
Reply #3 Wednesday, May 2, 2007 12:17 PM
no images to download... YEAH!  I like this one best   LOL!!  I think I'm caught up now... yipee!!
RomanDA
Reply #4 Wednesday, May 2, 2007 12:47 PM
I like this one best


Im glad people are using these.. makes me feel good.
SirSmiley
Reply #5 Wednesday, May 2, 2007 3:15 PM
Looks like someone is liking the new code tags!

Hope it has helped save you some time. Nice work as usual.
RomanDA
Reply #6 Wednesday, May 2, 2007 8:17 PM
well i didnt use the new code stuff, didnt work. Thats a table.. LOL
Quentin94
Reply #7 Sunday, May 6, 2007 11:13 AM
D:\My data\Wallpapers\ZubazisUgly.jpg


just see that RomanDA you are really awesome
ZubaZ
Reply #8 Sunday, May 6, 2007 10:11 PM
D:\My data\Wallpapers\ZubazisUgly.jpg


just see that RomanDA you are really awesome


HEY!!!  I may be ugly, but I'm still better looking than Bichur, RomanDA, Island Dog, Po' Smedly, Skinhit, Zoomba, Starkers, vStyler COMBINED!


Quentin94
Reply #9 Monday, May 7, 2007 4:10 AM
HEY!!! I may be ugly, but I'm still better looking than Bichur, RomanDA, Island Dog, Po' Smedly, Skinhit, Zoomba, Starkers, vStyler COMBINED!




RomanDA
Reply #10 Monday, May 7, 2007 9:00 AM
omg.. all of us "combined" = Frankenstein!!

please remove that photo.. my head hurts bad enough today.. maybe replace it with your daughters pic.. HEHE

RomanDA runs away

PoSmedley
Reply #11 Thursday, November 1, 2007 7:56 PM
HEY!!! I may be ugly, but I'm still better looking than Bichur, RomanDA, Island Dog, Po' Smedly, Skinhit, Zoomba, Starkers, vStyler COMBINED!


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.
ZubaZ
Reply #12 Thursday, November 1, 2007 8:04 PM
Way to resurrect a dead thread for your own evil purposes.
PoSmedley
Reply #13 Thursday, November 1, 2007 9:18 PM
At least it's a good thread to resurrect. Speaking of which...it's getting close to Troll Thumping Day again.
ZubaZ
Reply #14 Thursday, November 1, 2007 9:48 PM
it's getting close to Troll Thumping Day
Is your son ready? 


[link]
PoSmedley
Reply #15 Friday, November 2, 2007 9:23 PM
I forgot about THAT thread. lol.

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!



web-wc01