Sign in to follow this  
Followers 0
dbzfanatic

Help Optimizing? (Background changer program)

10 posts in this topic

#1 ·  Posted (edited)

Hi, I don't really have a "problem" but my own skills are not quite up to snuff. The program works but I'm encouraging any improvements to the code or suggestions for "features". I'm not going to sell this but I think it would still be quite fun to continue developing it a bit further.

Current Specs:

  • Reads registry for background image.
  • Checks for proper image once per minute.
  • ~4400kb memory usage.
  • < 1% CPU usage.
  • Transfers background images to temp directory. (May stay, may not)
  • Image specification.

To-do List:

  • Add time specification. (Added)
  • Add picture specification.

Here is the code so far.

#NoTrayIcon
#RequireAdmin
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_outfile=change bg.exe
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_Res_Description=Changes the desktop background based on the time of day.
#AutoIt3Wrapper_Res_Language=1033
#AutoIt3Wrapper_Run_AU3Check=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Misc.au3>
#include <Date.au3>
#include <Array.au3>
#include <GUIConstants.au3>

#Region ### START Koda GUI section ### Form=C:\Users\Owner\Desktop\frmSetOptions.kxf
$frmSetOptions = GUICreate("Background Changer Options", 522, 224, 193, 125)
$Images = GUICtrlCreateGroup("Images", 24, 32, 465, 129)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$txtDay = GUICtrlCreateInput("", 178, 67, 289, 24)
$txtNight = GUICtrlCreateInput("", 178, 107, 289, 24)
$lblDay = GUICtrlCreateLabel("Image for morning/day: ", 34, 67, 144, 20)
$lblNight = GUICtrlCreateLabel("Image for evening/night: ", 32, 111, 149, 20)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$lblInfo = GUICtrlCreateLabel("Please enter the FULL path to the image file(s).", 72, 8, 326, 20)
GUICtrlSetFont(-1, 12, 400, 0, "MS UI Gothic")
$btnSend = GUICtrlCreateButton("Submit Changes", 168, 176, 177, 25, 0x50014001)
GUICtrlSetBkColor(-1, 0x0066CC)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

RegWrite("HCKU\Software\Michael Corp")

Global $d_temp
Global $n_temp

$c_image = RegRead("HKCU\Software\Michael Corp","CustomImage")
If $c_image = 0 Then
    FileInstall("C:\Users\Owner\Desktop\verdure1920.bmp",@TempDir & "/verdure1920.bmp")
    FileInstall("C:\Users\Owner\Desktop\verdure1920.bmp",@TempDir & "/summerwood1920.bmp")
    $d_temp = @TempDir & "\verdure1920.bmp"
    $n_temp = @TempDir & "\summerwood1920.bmp"
ElseIf $c_image = 1 Then
    $d_temp = RegRead("HKCU\Software\Michael Corp","DayImage")
    $n_temp = RegRead("HKCU\Software\Michael Corp","NightImage")
    GUICtrlSetData($txtDay,$d_temp)
    GUICtrlSetData($txtNight,$n_temp)
EndIf

$d_array = StringSplit($d_temp,"\")
$n_array = StringSplit($n_temp,"\")
$d_image = $d_array[$d_array[0]]
$n_image = $n_array[$n_array[0]]

If $d_image = "" Then
    $d_image = @TempDir & "\verdure1920.bmp"
EndIf
If $n_image = "" Then
    $n_image = @TempDir & "\summerwood1920.bmp"
EndIf

AdlibEnable("Check",60000)

Func Updt($image)
    RegWrite("HKCU\Control Panel\Desktop", "Wallpaper", "REG_SZ", $image )
    Run("RunDll32.exe USER32.DLL,UpdatePerUserSystemParameters ,1 ,True")
EndFunc

Func Check()
        $time = @HOUR
        $pic = RegRead("HKCU\Control Panel\Desktop", "Wallpaper")
        If $time >= 6 And $time < 17 And $pic <> $d_image Or $pic <> $d_temp Then
            Updt(@TempDir & "\verdure1920.bmp")
        Elseif $time >= 17 And $time < 6 And $pic <> $n_image Or $pic <> $n_temp Then
            Updt(@TempDir & "\summerwood1920.bmp")
        EndIf
EndFunc


While 1
    $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                GUISetState(@SW_HIDE)
            Case $btnSend
                $day = GUICtrlRead($txtDay)
                $night = GUICtrlRead($txtNight)
                If $day <> "" Or $night <> "" Then
                    $custom = 1
                Else
                    $custom = 0
                EndIf
                RegWrite("HKCU\Software\Michael Corp","CustomImage","REG_DWORD",$custom)
                If $custom <> 0 Then
                    RegWrite("HKCU\Software\Michael Corp","DayImage","REG_SZ",$day)
                    RegWrite("HKCU\Software\Michael Corp","NightImage","REG_SZ",$night)
                EndIf
                GUISetState(@SW_HIDE)
        EndSwitch
WEnd

Here are the default images I used. (Taken from Digital Blasphemy's free gallery,not my own work, I do not take credit for them.)

Posted Image

Posted Image

(Edited: Pictures are now only 480x300)

Edited by dbzfanatic
posts merged and image links removed. they were 2.8Mb in size.

Share this post


Link to post
Share on other sites



Think you could thumbnail those images on their server? They load way to slow.


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

I scaled them down and I actually have a question now. I was working on the image specification part and the code seems right but when I try to submit my changes the program just hangs up. Here's the modified code I've been working on.

#NoTrayIcon
#RequireAdmin
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_outfile=change bg.exe
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_Res_Description=Changes the desktop background based on the time of day.
#AutoIt3Wrapper_Res_Language=1033
#AutoIt3Wrapper_Run_AU3Check=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Misc.au3>
#include <Date.au3>
#include <Array.au3>
#include <GUIConstants.au3>

#Region ### START Koda GUI section ### Form=C:\Users\Owner\Desktop\frmSetOptions.kxf
$frmSetOptions = GUICreate("Background Changer Options", 522, 224, 193, 125)
$Images = GUICtrlCreateGroup("Images", 24, 32, 465, 129)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$txtDay = GUICtrlCreateInput("", 178, 67, 289, 24)
$txtNight = GUICtrlCreateInput("", 178, 107, 289, 24)
$lblDay = GUICtrlCreateLabel("Image for morning/day: ", 34, 67, 144, 20)
$lblNight = GUICtrlCreateLabel("Image for evening/night: ", 32, 111, 149, 20)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$lblInfo = GUICtrlCreateLabel("Please enter the FULL path to the image file(s).", 72, 8, 326, 20)
GUICtrlSetFont(-1, 12, 400, 0, "MS UI Gothic")
$btnSend = GUICtrlCreateButton("Submit Changes", 168, 176, 177, 25, 0x50014001)
GUICtrlSetBkColor(-1, 0x0066CC)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

RegWrite("HCKU\Software\Michael Corp")

Global $d_temp
Global $n_temp

$c_image = RegRead("HKCU\Software\Michael Corp","CustomImage")
If $c_image = 0 And @error = 0 Then
    FileInstall("C:\Users\Owner\Desktop\verdure1920.jpg",@TempDir & "/verdure1920.jpg")
    FileInstall("C:\Users\Owner\Desktop\verdure1920.jpg",@TempDir & "/summerwood1920.jpg")
    $d_temp = @TempDir & "\verdure1920.jpg"
    $n_temp = @TempDir & "\summerwood1920.jpg"
ElseIf $c_image = 1 And @error = 0 Then
    $d_temp = RegRead("HKCU\Software\Michel Corp","DayImage")
    $n_temp = RegRead("HKCU\Software\Michael Corp","NightImage")
    GUICtrlSetData($txtDay,$d_temp)
    GUICtrlSetData($txtNight,$n_temp)
EndIf

Func Updt($image)
    RegWrite("HKCU\Control Panel\Desktop", "Wallpaper", "REG_SZ", $image )
    Run("RunDll32.exe USER32.DLL,UpdatePerUserSystemParameters ,1 ,True")
EndFunc

$d_array = StringSplit($d_temp,"\")
$n_array = StringSplit($n_temp,"\")

While 1
    $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                GUISetState(@SW_HIDE)
            Case $btnSend
                $day = GUICtrlRead($txtDay)
                $night = GUICtrlRead($txtNight)
                If $day <> "" Or $night <> "" Then
                    $custom = 1
                Else
                    $custom = 0
                EndIf
                RegWrite("HKCU\Software\Michael Corp","CustomImage","REG_DWORD",$custom)
                If $custom <> 0 Then
                    RegWrite("HKCU\Software\Michael Corp","DayImage","REG_SZ",$day)
                    RegWrite("HKCU\Software\Michael Corp","NightImage","REG_SZ",$night)
                EndIf
                GUISetState(@SW_HIDE)
        EndSwitch
    $time = @HOUR
    $pic = RegRead("HKCU\Control Panel\Desktop", "Wallpaper")
    If $time >= 6 And $time < 17 And $pic <> "verdure1920.jpg" Or $pic <> $d_temp Then
        Updt(@TempDir & "\verdure1920.jpg")
    Elseif $time >= 17 And $time < 6 And $pic <> "summerwood1920.jpg" Or $pic <> $n_temp Then
        Updt(@TempDir & "\summerwood1920.jpg")
    EndIf
    Sleep(60000)
WEnd

Hope you can find out what's caussing the hangups. I'm also aware that the code will try to continually change the desktop because of the

$pic <> "verdure1920.jpg"
portion and summerwood counterpart. I was going to fix that after the registry was properly written.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Sleep(60000)

Is a problem when using it in a GUI loop ;) .

Edit:

You are using GUIGetMsg(), so you really don't even need a sleep in that loop (unless you want to use it in a conditional statement.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Even if it's after the EndSwitch? I still want the program to only check for the background once per minute, I didn't think that would also make the GUI sleep. Oops Posted Image Ok I'll change that but is there a way (other than using timers, just a personal preference) to keep it checking only once every minute?

Edited by dbzfanatic

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Even if it's after the EndSwitch? I still want the program to only check for the background once per minute, I didn't think that would also make the GUI sleep. Oops Posted Image Ok I'll change that but is there a way (other than using timers, just a personal preference) to keep it checking only once every minute?

It's a loop, doesn't matter where it is, your getmsg won't fire until the sleep is over.
#NoTrayIcon
#RequireAdmin
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_outfile=change bg.exe
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_Res_Description=Changes the desktop background based on the time of day.
#AutoIt3Wrapper_Res_Language=1033
#AutoIt3Wrapper_Run_AU3Check=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Misc.au3>
#include <Date.au3>
#include <Array.au3>
#include <GUIConstants.au3>

#Region ### START Koda GUI section ### Form=C:\Users\Owner\Desktop\frmSetOptions.kxf
$frmSetOptions = GUICreate("Background Changer Options", 522, 224, 193, 125)
$Images = GUICtrlCreateGroup("Images", 24, 32, 465, 129)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$txtDay = GUICtrlCreateInput("", 178, 67, 289, 24)
$txtNight = GUICtrlCreateInput("", 178, 107, 289, 24)
$lblDay = GUICtrlCreateLabel("Image for morning/day: ", 34, 67, 144, 20)
$lblNight = GUICtrlCreateLabel("Image for evening/night: ", 32, 111, 149, 20)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$lblInfo = GUICtrlCreateLabel("Please enter the FULL path to the image file(s).", 72, 8, 326, 20)
GUICtrlSetFont(-1, 12, 400, 0, "MS UI Gothic")
$btnSend = GUICtrlCreateButton("Submit Changes", 168, 176, 177, 25, 0x50014001)
GUICtrlSetBkColor(-1, 0x0066CC)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

RegWrite("HCKU\Software\Michael Corp")

Global $d_temp, $i_timer
Global $n_temp

$c_image = RegRead("HKCU\Software\Michael Corp","CustomImage")
If $c_image = 0 And @error = 0 Then
    FileInstall("C:\Users\Owner\Desktop\verdure1920.jpg",@TempDir & "/verdure1920.jpg")
    FileInstall("C:\Users\Owner\Desktop\verdure1920.jpg",@TempDir & "/summerwood1920.jpg")
    $d_temp = @TempDir & "\verdure1920.jpg"
    $n_temp = @TempDir & "\summerwood1920.jpg"
ElseIf $c_image = 1 And @error = 0 Then
    $d_temp = RegRead("HKCU\Software\Michel Corp","DayImage")
    $n_temp = RegRead("HKCU\Software\Michael Corp","NightImage")
    GUICtrlSetData($txtDay,$d_temp)
    GUICtrlSetData($txtNight,$n_temp)
EndIf

Func Updt($image)
    RegWrite("HKCU\Control Panel\Desktop", "Wallpaper", "REG_SZ", $image )
    Run("RunDll32.exe USER32.DLL,UpdatePerUserSystemParameters ,1 ,True")
EndFunc

$d_array = StringSplit($d_temp,"\")
$n_array = StringSplit($n_temp,"\")

$i_timer = TimerInit()
While 1
    $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                GUISetState(@SW_HIDE)
            Case $btnSend
                $day = GUICtrlRead($txtDay)
                $night = GUICtrlRead($txtNight)
                If $day <> "" Or $night <> "" Then
                    $custom = 1
                Else
                    $custom = 0
                EndIf
                RegWrite("HKCU\Software\Michael Corp","CustomImage","REG_DWORD",$custom)
                If $custom <> 0 Then
                    RegWrite("HKCU\Software\Michael Corp","DayImage","REG_SZ",$day)
                    RegWrite("HKCU\Software\Michael Corp","NightImage","REG_SZ",$night)
                EndIf
                GUISetState(@SW_HIDE)
        EndSwitch
    If TimerDiff($i_timer)  >= 60000 Then
        $time = @HOUR
        $pic = RegRead("HKCU\Control Panel\Desktop", "Wallpaper")
        If $time >= 6 And $time < 17 And $pic <> "verdure1920.jpg" Or $pic <> $d_temp Then
            Updt(@TempDir & "\verdure1920.jpg")
        Elseif $time >= 17 And $time < 6 And $pic <> "summerwood1920.jpg" Or $pic <> $n_temp Then
            Updt(@TempDir & "\summerwood1920.jpg")
        EndIf
        $i_timer = TimerInit()
    EndIf
WEnd

Edit:

Forgot to reset the timer.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Edit 2: I found out what was wrong, it was a simple typo (I can't believe I misspelled my own name).

$d_temp = RegRead("HKCU\Software\Michel Corp","DayImage")
Sorry ... But ;) ... That's a first for me :D ...

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Project updated.

@SmOke_N: yeah *hangs head in shame* I know...but at least I found it lol.

For everyone looking, no SmOke_N didn't double post, I deleted the post before it just before it was submitted.

Edited by dbzfanatic

Share this post


Link to post
Share on other sites

That's very true... Good Job! ;) (still funny as hell though)


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Yeah yeah, laugh all you want get it out of your system. I have to agree it is pretty funny. Now, back on topic (sorta).

I need testers! Preferably people with WinXP SP2 or earlier but anyone who wants to assist is welcome to PM me. The only way to find bugs/flaws is to test on a range of different systems and architectures so that's my goal.

I am now currently working on implementing the time select feature. I will update the first post when it's finished. ;)

Edited by dbzfanatic

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0