Jump to content

Moving a gui buttons desktop position with a Koda GUI


Recommended Posts

This program will create a dummy hovering button in the top right corner of the screen. It will also create another window that has buttons intended to make the first hovering button change its position on the screen. I have gotten the hovering button to move the button down the screen using the move up button (I know) but it only moves once by 10 pixels no matter how many times I press the move up button. Is this a problem with the speed of autoit or have I got a setting wrong somewhere? I am sure the problem is in the code I marked. Please someone look at this and tell me if there is a cleaner way to do this.

;Program to interface with the MAC.ini file to modify position settings

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Mac Button Move", 401, 372, 207, 114)
$MovePosUp = GUICtrlCreateButton("Move Up", 150, 10, 100, 100)
$MovePosLeft = GUICtrlCreateButton("Move Left", 40, 120, 100, 100)
$MovePosDown = GUICtrlCreateButton("Move Down", 150, 120, 100, 100)
$MovePosRight = GUICtrlCreateButton("Move Right", 260, 120, 100, 100)
$ResetButtonPos = GUICtrlCreateButton("Reset Button Position", 40, 248, 130, 41)
$SaveNewPos = GUICtrlCreateButton("Save New Position", 230, 248, 130, 41)
$ExitButtonPos = GUICtrlCreateButton("Close window", 230, 312, 130, 41)
$Instructions = GUICtrlCreateButton("Instructions", 40, 312, 129, 41)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

$inifile = FileOpen("Mac.ini", 0); the ini file holds the hovering button position values
$height = 75
$width = 80
$hPosition = IniRead("Mac.ini", "Desktop Position", "hPosition", "NotFound")
$vPosition = IniRead("Mac.ini", "Desktop Position", "vPosition", "NotFound")

If $inifile = -1 Then
    MsgBox(0, "Error", "Unable to open Mac.ini")
    Exit
EndIf

    GUICreate("MAC Button Move", $width, $height, @DesktopWidth - $hPosition, $vPosition, $WS_POPUP, $WS_EX_TOPMOST) ; Creates a window
    GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit"); Closes the program if the tray icon is closed
    $FloatingPic = GUICtrlCreatePic("MacMove.jpg", -1, -1)
    GUICtrlSetState(-1, $GUI_DISABLE)
    Opt("GUICoordMode", 2)
    GUISetState()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $MovePosUp ;If Up Arrow is clicked ;<---------------------------------------Look here
            GUIDelete("MAC Button Move")
             $NewVPosition = $vPosition+10
                GUICreate("MAC Button Move", $width, $height, @DesktopWidth - $hPosition, $NewVPosition, $WS_POPUP, $WS_EX_TOPMOST)
                GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
                $FloatingPic = GUICtrlCreatePic("MacMove.jpg", -1, -1)
                GUICtrlSetState(-1, $GUI_DISABLE)
                Opt("GUICoordMode", 2)
                GUISetState()
            Sleep(1000) ;<---------------------------------------------------------- To Here, and leave Sleep alone - trust me
        Case $nMsg = $MovePosLeft ;If Left Arrow is clicked
        Case $nMsg = $MovePosDown ;If Down Arrow is clicked
        Case $nMsg = $MovePosRight ;If Right arrow is clicked
        Case $nMsg = $ResetButtonPos ;If Reset Button Position Is clicked
        Case $nMsg = $SaveNewPos ;If Reset Button Position Is clicked
        Case $nMsg = $Instructions ;If Reset Button Position Is clicked
        Case $nMsg = $ExitButtonPos ;If Reset Button Position Is clicked
            Exit
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func _Exit()
    Exit
EndFunc   ;==>_Exit

The ini file has:

[Desktop Position]
hPosition = 80
vPosition = 1

Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Link to comment
Share on other sites

  • Moderators

computergroove,

Where to start? :x

- 1. Do not mix OnEvent and MessageLoop mode in the same script. It can be done, but there have to be very good reasons and you have to take very great care - in a simple script like this you should never mix modes

- 2. Why set a GUICoord mode - you do not need to. :P

- 3. No requirement to open an ini file before use - the Ini* functions do that for you automatically.

- 4. Use FileExists to check for the existence of the ini file. And why not set default values into your IniRead lines so that you can continue even if the ini file is missing? :shifty:

- 5. Save the returned handle of the GUIs you creat - they come in handy as you will see in a moment.

- 6. Use the proper format in a Switch structure - no need for the $msg = part in each Case.

- 7. Just move the GUI with WinMOve instead of destoying and recreating it. :(

- 8. Use WinGetPos to get the current position and then adjust using the those values. See how to use the GUI handle to identify it. :D

- 9. I understand that you must wait for the key to be released, but use a proper function rather than "trusting" to a Sleep.

So we end up with something like this: :nuke:

;Program to interface with the MAC.ini file to modify position settings

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Misc.au3>

$dll = DllOpen("user32.dll")

$Form1 = GUICreate("Mac Button Move", 400, 380, 200, 100)
$MovePosUp = GUICtrlCreateButton("Move Up", 150, 10, 100, 100)
$MovePosLeft = GUICtrlCreateButton("Move Left", 40, 120, 100, 100)
$MovePosDown = GUICtrlCreateButton("Move Down", 150, 120, 100, 100)
$MovePosRight = GUICtrlCreateButton("Move Right", 260, 120, 100, 100)
$ResetButtonPos = GUICtrlCreateButton("Reset Button Position", 40, 248, 130, 41)
$SaveNewPos = GUICtrlCreateButton("Save New Position", 230, 248, 130, 41)
$ExitButtonPos = GUICtrlCreateButton("Close window", 230, 312, 130, 41)
$Instructions = GUICtrlCreateButton("Instructions", 40, 312, 129, 41)
GUISetState(@SW_SHOW)

;If $inifile = -1 Then ; Use FileExists as below <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
If Not FileExists("Mac.ini")
    MsgBox(0, "Error", "Unable to open Mac.ini")
    Exit
EndIf

;$inifile = FileOpen("Mac.ini", 0); Not needed <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$height = 75
$width = 80
$hPosition = IniRead("Mac.ini", "Desktop Position", "hPosition", "NotFound") ; Wy not valid defaults? <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$vPosition = IniRead("Mac.ini", "Desktop Position", "vPosition", "NotFound")

    $hGUI = GUICreate("MAC Button", $width, $height, @DesktopWidth - $hPosition, $vPosition, $WS_POPUP, $WS_EX_TOPMOST) ; Creates a window
    ;GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit"); Not in OnEvent mode <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    $FloatingPic = GUICtrlCreatePic("MacMove.jpg", -1, -1)
    GUICtrlSetState(-1, $GUI_DISABLE)
    ;Opt("GUICoordMode", 2) ; Why? <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    GUISetState()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE ; Use correct format for the Case <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            DllClose($dll)
            Exit
        Case $MovePosUp ;If Up Arrow is clicked
            $aPos = WinGetPos($hGUI) ; Get current position <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            WinMove($hGUI, "", $aPos[0], $aPos[1] - 10) ; Use WinMove to change it <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            Do
                Sleep(10)
            Until Not _IsPressed("26", $dll) ; Wait until key is released <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        Case $MovePosLeft ;If Left Arrow is clicked
            $aPos = WinGetPos($hGUI)
            WinMove($hGUI, "", $aPos[0] - 10, $aPos[1])
            Do
                Sleep(10)
            Until Not _IsPressed("25", $dll)
        Case $MovePosDown ;If Down Arrow is clicked
            $aPos = WinGetPos($hGUI)
            WinMove($hGUI, "", $aPos[0], $aPos[1] + 10)
            Do
                Sleep(10)
            Until Not _IsPressed("28", $dll)
        Case $MovePosRight ;If Right arrow is clicked
            $aPos = WinGetPos($hGUI)
            WinMove($hGUI, "", $aPos[0] + 10, $aPos[1])
            Do
                Sleep(10)
            Until Not _IsPressed("27", $dll)
        Case $ResetButtonPos ;If Reset Button Position Is clicked
        Case $SaveNewPos ;If Reset Button Position Is clicked
        Case $Instructions ;If Reset Button Position Is clicked
        Case $ExitButtonPos ;If Reset Button Position Is clicked
            Exit
    EndSwitch
WEnd

Please take all this as constructive criticism and do ask if you have any questions on any of the points I made above. :lol:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thank you for the reply. This is working very well so far with the modifications you offered. I absolutely appreciate constructive criticism. I made this from parts of my other programs and I am still in learning mode with some of this stuff which is why you are seeing some unnecessary items in my code. 1 question so far, I have never heard of DllOpen. Why am I opening user32.dll?

Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Link to comment
Share on other sites

  • Moderators

computergroove,

Why am I opening user32.dll?

From the Help file for _IsPressed: :x

"If calling this function repeatidly [sic], should open 'user32.dll' and pass in handle. Make sure to close at end of script"

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I just changed:

$hGUI = GUICreate("MAC Button", $width, $height, $hPosition, $vPosition, $WS_POPUP, $WS_EX_TOPMOST) ; Creates a window

to remove the @Desktopwidth and added:

Case $SaveNewPos ;If Reset Button Position Is clicked
IniWrite("Mac.ini", "Desktop Position", "hPosition", $aPos[1])
IniWrite("Mac.ini", "Desktop Position", "vPosition", $aPos[0])

I am getting mixed results with the correct saving of the data to the ini file. I am testing this by clicking random move up right left and down and then clicking save New Position in the gui. When I reopen it I will get the hovering button showing up at a different location then it was at when I clicked save new position - sometimes. Other times it is starting at the right location. What am I missing here?

Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Link to comment
Share on other sites

  • Moderators

computergroove,

What am I missing here?

That you have the coordinates reversed: :P

IniWrite("Mac.ini", "Desktop Position", "hPosition", $aPos[1]) ; The [1] element is the Y or vertical coord
IniWrite("Mac.ini", "Desktop Position", "vPosition", $aPos[0]) ; The [0] element is the X or horizontal coord

Try it the other way round! :x

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...