Sign in to follow this  
Followers 0
MattyD

Launch Pad

3 posts in this topic

Hey all,

Heres a fun little project of mine I have been wasting some time on. (well fun to code anyway!)

To get the most out of it, try dropping files on the client area and drag those buttons about :unsure:

Enjoy!

Matt

;Launch Pad by MattyD 14/04/2011
;#RequireAdmin
#include <ButtonConstants.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>
#include <GuiConstants.au3>
#include <GuiMenu.au3>
#include <File.au3>
#include <WinAPI.au3>
#include <Array.au3>
#include <Misc.au3>

Opt("MouseCoordMode", 2)

Global $ButtonID = -1
$Ini = "Launch Pad.ini"

Global $Dim_Pos[2]
$Dim_Pos[0] = IniRead($Ini, "Dimensions", "X", -1)
$Dim_Pos[1] = IniRead($Ini, "Dimensions", "Y", -1)
Global $Dim_Rows = IniRead($Ini, "Dimensions", "Rows", 10)
Global $Dim_Cols = IniRead($Ini, "Dimensions", "Columns", 2)
Global $Dim_Width = IniRead($Ini, "Dimensions", "BtnWidth", 80)
Global $Dim_Height = IniRead($Ini, "Dimensions", "BtnHeight", 20)
Global $Dim_XWidth = IniRead($Ini, "Dimensions", "ClearBtnWidth", $Dim_Height)
Global $Dim_Num = $Dim_Rows * $Dim_Cols
Local $Dim_Coeff = ($Dim_Width + $Dim_XWidth) / $Dim_Rows
Local $Dim_TotWidth = ($Dim_Width + $Dim_XWidth) * $Dim_Cols
Global $Dim_TotHeight = $Dim_Height * ($Dim_Rows + 1)
Local $Dim_SortWidth
If $Dim_TotWidth < 120 Then
    $Dim_SortWidth = Int($Dim_TotWidth / 3)
Else
    $Dim_SortWidth = 40
EndIf

Global $Opt_NumberButtons = 1
Global $Opt_MoveTime = 500
Global $Colours[9]

Global $hGUI = GUICreate("Launch Pad", $Dim_TotWidth, $Dim_TotHeight, $Dim_Pos[0], $Dim_Pos[1], Default, $WS_EX_ACCEPTFILES)

;GUISetState()
Local $Buttons[$Dim_Num]
Local $Clear[$Dim_Num]
Local $Context[$Dim_Num][12]
Local $Options
Local $Name = ""
Local $MenuColours[9]
Local $Sort
Global $SortContext[3]
$SortContext[0] = GUICtrlCreateContextMenu()
$SortContext[1] = GUICtrlCreateMenuItem("By Name", $SortContext[0])
$SortContext[2] = GUICtrlCreateMenuItem("By Colour", $SortContext[0])

For $i = 0 To 7
    $Colours[8 - $i] = 0
    If BitAND($i, 1) Then $Colours[8 - $i] += 0xFF
    If BitAND($i, 2) Then $Colours[8 - $i] += 0xFF00
    If BitAND($i, 4) Then $Colours[8 - $i] += 0xFF0000
    $Colours[8 - $i] = "0x" & StringReplace(Hex($Colours[8 - $i], 6), "00", "AA")
    $Colours[8 - $i] = IniRead($Ini, "Colours", 8 - $i, $Colours[8 - $i])
Next

For $i = 1 To 8
    $MenuColours[$i] = _WinAPI_CreateSolidBitmap($hGUI, $Colours[$i], 11, 11)
Next

For $i = 0 To UBound($Buttons) - 1
    If $Opt_NumberButtons Then $Name = $i
    $Buttons[$i] = GUICtrlCreateButton($Name, $i * $Dim_Coeff - Mod($i, $Dim_Rows) * $Dim_Coeff, _
        Mod($i, $Dim_Rows) * $Dim_Height, _
        $Dim_Width, $Dim_Height)
    GUICtrlSetState(-1, $GUI_DROPACCEPTED)
    $Context[$i][0] = GUICtrlCreateContextMenu($Buttons[$i])
    $Context[$i][1] = GUICtrlCreateMenuItem("Edit", $Context[$i][0])
    $Context[$i][2] = GUICtrlCreateMenu("Set Colour", $Context[$i][0])
    For $j = 0 To 8
        $Context[$i][$j + 3] = GUICtrlCreateMenuItem($j, $Context[$i][2])
        _GUICtrlMenu_SetItemBmp(GUICtrlGetHandle($Context[$i][2]), $j, $MenuColours[$j])
    Next

    $Clear[$i] = GUICtrlCreateButton("X", $i * $Dim_Coeff - Mod($i, $Dim_Rows) * $Dim_Coeff + $Dim_Width, _
        Mod($i, $Dim_Rows) * $Dim_Height, _
        $Dim_XWidth, $Dim_Height)
    _WinAPI_SetWindowLong(GUICtrlGetHandle(-1), $GWL_STYLE, BitAND(_WinAPI_GetWindowLong(GUICtrlGetHandle(-1), $GWL_STYLE), BitNOT($WS_TABSTOP)))
    GUICtrlSetColor($Clear[$i], 0xAA0000)
Next

$Sort = GUICtrlCreateButton("Sort", 0, $Dim_Height * $Dim_Rows, $Dim_SortWidth, $Dim_Height)
$Options = GUICtrlCreateButton("Options", $Dim_SortWidth, $Dim_Height * $Dim_Rows, ($Dim_Width + $Dim_XWidth) * $Dim_Cols - $Dim_SortWidth, $Dim_Height)

_Update()
Global $Cursor0 = DllCall("User32.dll", "ptr", "LoadCursorW", "dword", 0, "dword", 32512)
Global $Cursor1 = DllCall("User32.dll", "ptr", "LoadCursorW", "dword", 0, "dword", 32646)
Global $Cursor2 = DllCall("User32.dll", "ptr", "LoadCursorW", "dword", 0, "dword", 32648)
Global $Cursor_Current = $Cursor0[0]

GUISetState()
AdlibRegister("_Update", 5000)

Global $Mouse_MoveInfo[3]
Local $Msg, $MousePos
While 1
    $Msg = GUIGetMsg()
    ;$Msg = $Msg[0]
    ;If $Msg And $Msg <> -11 Then ConsoleWrite($Msg & @CRLF)

    If $Mouse_MoveInfo[2] And TimerDiff($Mouse_MoveInfo[2]) > $Opt_MoveTime Then
        $MousePos = MouseGetPos()
        If _GetButton($MousePos) < 0 Then
            If $Cursor_Current <> $Cursor2[0] Then
                _SetCursor($Cursor2[0])
                $Cursor_Current = $Cursor2[0]
            EndIf
        Else
            If $Cursor_Current <> $Cursor1[0] Then
                _SetCursor($Cursor1[0])
                $Cursor_Current = $Cursor1[0]
            EndIf
        EndIf
    EndIf

    Switch $Msg
        Case -3
            $Dim_Pos = WinGetPos($hGUI)
            IniWrite($Ini, "Dimensions", "X", $Dim_Pos[0])
            IniWrite($Ini, "Dimensions", "Y", $Dim_Pos[1])
            Exit
        Case -7
            $MousePos = MouseGetPos()
            $Mouse_MoveInfo[0] = _GetButton($MousePos)
            $Mouse_MoveInfo[2] = TimerInit()
        Case -8
            $MousePos = MouseGetPos()
            $Mouse_MoveInfo[1] = _GetButton($MousePos)
            _MoveButton()
            $Mouse_MoveInfo[0] = -1
            $Mouse_MoveInfo[1] = -1
            $Mouse_MoveInfo[2] = 0
            _SetCursor($Cursor0[0])
            $Cursor_Current = $Cursor0[0]
        Case -13
            ;ConsoleWrite(@GUI_DragFile & @CRLF)
            $MousePos = MouseGetPos()
            _Add(_GetButton($MousePos), @GUI_DragFile)
        Case $Sort
            _ShowSortMenu()
        Case $SortContext[1]
            _Sort(1)
        Case $SortContext[2]
            _Sort(2)
        Case $Options
            _Options()
        Case Else
            If $Msg And $Msg <> -11 Then $Mouse_MoveInfo[0] = -1
            For $i = 0 To UBound($Buttons) - 1
                If $Msg = $Buttons[$i] Then _Button($i)
                If $Msg = $Context[$i][1] Then _Button($i, True)
                If $Msg = $Clear[$i] Then _Clear($i, False)
                If $Msg <= $Context[$i][11] And $Msg >= $Context[$i][3]  Then _
                _SetColour($i, _GUICtrlMenu_GetItemText(GUICtrlGetHandle($Context[$i][2]), $Msg, False))
            Next
    EndSwitch
WEnd

Func _Button($i, $Edit = False)
    Local $ButtonName, $CommandInfo[2]
    _Update($i)
    If Not GUICtrlRead($Buttons[$i]) Or $Edit Then
        $CommandInfo[0] = InputBox("Launch Pad", "Enter a command or file path", IniRead($Ini, $i, "Command", ""))
        If Not $CommandInfo[0] Or @error Then
            If $Edit Then Return
            Return _Clear($i)
        EndIf
        $CommandInfo[1] = InputBox("Launch Pad", "Enter the working directory (or leave blank)", IniRead($Ini, $i, "WorkingDir", ""))
        If @error Then
            If $Edit Then Return
            Return _Clear($i)
        EndIf
        If $CommandInfo[1] And Not FileExists($CommandInfo[1]) Then
            If $Edit Then Return
            Return _Clear($i)
        EndIf
        $ButtonName = InputBox("Launch Pad", "Enter a name for the button", IniRead($Ini, $i, "Label", ""))
        If @error Then
            If $Edit Then Return
            Return _Clear($i)
        EndIf
        If Not $ButtonName Then
            GUICtrlSetData($Buttons[$i], "New Command")
        Else
            GUICtrlSetData($Buttons[$i], $ButtonName)
        EndIf
        IniWrite($Ini, $i, "Label", GUICtrlRead($Buttons[$i]))
        IniWrite($Ini, $i, "Command", $CommandInfo[0])
        IniWrite($Ini, $i, "WorkingDir", $CommandInfo[1])
    Else
        If Not Run(IniRead($Ini, $i, "Command", ""), IniRead($Ini, $i, "WorkingDir", @WorkingDir)) Then _
        ShellExecute(IniRead($Ini, $i, "Command", ""), "", IniRead($Ini, $i, "WorkingDir", @WorkingDir))
    EndIf
EndFunc

Func _Update($ButtonID = -1)
    Local $Low, $High, $ColourIndex, $CurrLabel, $NewLabel

    If $ButtonID < 0 Then
        $Low = 0
        $High = UBound($Buttons) - 1
    Else
        $Low = $ButtonID
        $High = $ButtonID
    EndIf

    For $i = $Low To $High
        $NewLabel = IniRead($Ini, $i, "Label", "")
        If $NewLabel <> GUICtrlRead($Buttons[$i]) Then GUICtrlSetData($Buttons[$i], $NewLabel)

        $ColourIndex = IniRead($Ini, $i, "Colour", "")
        If $ColourIndex == "0" Or Not $ColourIndex Then
            GUICtrlSetStyle($Buttons[$i], 0x00010000)
            If IniRead($Ini, $i, "Label", "") Then
                IniDelete($Ini, $i, "Colour")
            Else
                IniDelete($Ini, $i)
            EndIf
            $ColourIndex = ""
        EndIf
        If $ColourIndex Then GUICtrlSetBkColor($Buttons[$i], $Colours[$ColourIndex])
    Next
EndFunc

Func _Clear($i, $Confirm = True)
    If Not IniRead($Ini, $i, "Label", "") Then Return
    If Not $Confirm Then
        If MsgBox(4, "Confirm Delete", "Are you sure?") <> 6 Then Return
    EndIf
    IniDelete($Ini, $i)
    IniWrite($Ini, $i, "Colour", 0)
    _Update($i)
EndFunc

Func _Sort($Type)
    Local $Skip, $Start
    AdlibUnRegister("_Update")
    Local $Sections = IniReadSectionNames($Ini)
    Local $Labels[UBound($Sections) - 1][3]
    For $i = 1 To UBound($Sections) - 1
        If Not Number($Sections[$i]) And $Sections[$i] <> "0" Then
            $Skip += 1
            ContinueLoop
        EndIf
        $Labels[$i - 1][0] = IniRead($Ini, $Sections[$i], "Label", "")
        $Labels[$i - 1][1] = IniRead($Ini, $Sections[$i], "Colour", "")
        If Not $Labels[$i - 1][1] Then $Labels[$i - 1][1] = "9"
        $Labels[$i - 1][2] = $Sections[$i] & "_Old"
        IniRenameSection($Ini, $Sections[$i], $Sections[$i] & "_Old")
    Next
    _ArraySort($Labels, 1)
    ReDim $Labels[UBound($Labels) - $Skip][3]

    If $Type = 1 Then
        _ArraySort($Labels)
    ElseIf $Type = 2 Then
        _ArraySort($Labels, 0, 0, 0, 1)
        For $i = 0 To UBound($Labels) - 2
            If Number($Labels[$i][1]) < Number($Labels[$i + 1][1]) Then
                _ArraySort($Labels, 0, $Start, $i)
                $Start = $i + 1
            EndIf
        Next
    EndIf

    For $i = 0 To UBound($Labels) - 1
        IniRenameSection($Ini, $Labels[$i][2], $i)
    Next

    _Update()
    AdlibRegister("_Update", 5000)
EndFunc

Func _GetButton(ByRef $MousePos)
    Local $Grid[2]
    $Grid[0] = Int($MousePos[0] / ($Dim_Width + $Dim_XWidth))
    $Grid[1] = Int($MousePos[1] / $Dim_Height)
    ;ConsoleWrite($Grid[0] & ":" & $Grid[1] & @CRLF)
    ;ConsoleWrite($Grid[0] * $Dim_Rows + $Grid[1] & @CRLF)

    If $Grid[1] >= $Dim_Rows Or $Grid[0] >= $Dim_Cols Then Return -1
    If $MousePos[0] < 0 Or $MousePos[1] < 0 Then Return -1

    Return $Grid[0] * $Dim_Rows + $Grid[1]
EndFunc

Func _MoveButton()
    If Not $Mouse_MoveInfo[2] Or TimerDiff($Mouse_MoveInfo[2]) < $Opt_MoveTime Then Return 0
    If $Mouse_MoveInfo[0] < 0 Or $Mouse_MoveInfo[1] < 0 Then Return 0
    AdlibUnRegister("_Update")
    IniRenameSection($Ini, $Mouse_MoveInfo[1], $Mouse_MoveInfo[1] & "_Old")
    IniRenameSection($Ini, $Mouse_MoveInfo[0], $Mouse_MoveInfo[1])
    For $i = $Mouse_MoveInfo[1] To $Dim_Num - 1
        If Not IniRead($Ini, $i, "label", "") Then
            IniRenameSection($Ini, $Mouse_MoveInfo[1] & "_Old", $i)
            ExitLoop
        EndIf
    Next
    If $i = $Dim_Num Then
        For $i = 0 To $Mouse_MoveInfo[1] - 1
            If Not IniRead($Ini, $i, "label", "") Then
                IniRenameSection($Ini, $Mouse_MoveInfo[1] & "_Old", $i)
                ExitLoop
            EndIf
        Next
    EndIf
    If Not IniRead($Ini, $Mouse_MoveInfo[0], "Colour", "") Then IniWrite($Ini, $Mouse_MoveInfo[0], "Colour", 0)
    If Not IniRead($Ini, $Mouse_MoveInfo[1], "Colour", "") Then IniWrite($Ini, $Mouse_MoveInfo[1], "Colour", 0)
    If Not IniRead($Ini, $i, "Colour", "") Then IniWrite($Ini, $i, "Colour", 0)
    _Update($Mouse_MoveInfo[0])
    _Update($Mouse_MoveInfo[1])
    _Update($i)
    AdlibRegister("_Update", 5000)
EndFunc

Func _SetCursor(ByRef $Cursor)
    DllCall("User32.dll", "ptr", "SetCursor", "ptr", $Cursor)
EndFunc

Func _Add($Button, $Command)
    AdlibUnRegister("_Update")
    IniWrite($Ini, $Button, "Command", $Command)
    Local $Drive, $Dir, $FName, $Ext
    _PathSplit($Command, $Drive, $Dir, $FName, $Ext)
    IniWrite($Ini, $Button, "WorkingDir", StringTrimRight($Drive & $Dir, 1))
    IniWrite($Ini, $Button, "Label", $FName)
    _Update()
    AdlibRegister("_Update", 5000)
EndFunc

Func _SetColour($i, $ColourIndex)
    If Not IniRead($ini, $i, "Label", "") And $ColourIndex <> 0 Then Return
    IniWrite($Ini, $i, "Colour", $ColourIndex)
    _Update($i)
EndFunc

Func _Options()
    $Dim_Pos = WinGetPos($hGUI)
    IniWrite($Ini, "Dimensions", "X", $Dim_Pos[0])
    IniWrite($Ini, "Dimensions", "Y", $Dim_Pos[1])

    AdlibUnRegister("_Update")
    GUIDelete()
    $hOptions = GUICreate("Options", 280, 180, $Dim_Pos[0], $Dim_Pos[1])

    GUICtrlCreateLabel("Rows:", 10, 10, 64, 20, 0x200)
    $opt_Rows = GUICtrlCreateInput($Dim_Rows, 74, 10, 40, 20, 0x2080)
    GUICtrlCreateUpdown(-1, 0x20)

    GUICtrlCreateLabel("Columns:", 10, 30, 64, 20, 0x200)
    $opt_Columns = GUICtrlCreateInput($Dim_Cols, 74, 30, 40, 20, 0x2080)
    GUICtrlCreateUpdown(-1, 0x20)

    GUICtrlCreateLabel("Btn Width:", 10, 70, 64, 20, 0x200)
    $opt_btnWidth = GUICtrlCreateInput($Dim_Width, 74, 70, 40, 20, 0x2080)
    GUICtrlCreateUpdown(-1, 0x20)

    GUICtrlCreateLabel("X Btn Width:", 10, 90, 64, 20, 0x200)
    $opt_ClearbtnWidth = GUICtrlCreateInput($Dim_XWidth, 74, 90, 40, 20, 0x2080)
    GUICtrlCreateUpdown(-1, 0x20)

    GUICtrlCreateLabel("Btn Hieght:", 10, 110, 64, 20, 0x200)
    $opt_btnHeight = GUICtrlCreateInput($Dim_Height, 74, 110, 40, 20, 0x2080)
    GUICtrlCreateUpdown(-1, 0x20)

    $opt_Save = GUICtrlCreateButton("Save", 10, 140, 52, 30, 0x01)
    $opt_Default = GUICtrlCreateButton("Defaults", 62, 140, 52, 30)

    Local $opt_Colours[9][3]
    For $i = 1 To 8
        GUICtrlCreateLabel("Colour " & $i & ":", 134, 20 * ($i - 1) + 10, 66, 20, 0x200)
        $opt_Colours[$i][0] = GUICtrlCreateInput($Colours[$i], 190, 20 * ($i - 1) + 10, 60, 20)
        $opt_Colours[$i][1] = GUICtrlCreateInput("", 250, 20 * ($i - 1) + 10, 20, 20)
        _WinAPI_SetWindowLong(GUICtrlGetHandle(-1), $GWL_STYLE, BitAND(_WinAPI_GetWindowLong(GUICtrlGetHandle(-1), $GWL_STYLE), BitNOT($WS_TABSTOP)))
        $opt_Colours[$i][2] = GUICtrlRead($opt_Colours[$i][0])
        GUICtrlSetBkColor($opt_Colours[$i][1], GUICtrlRead($opt_Colours[$i][0]))
    Next
    GUISetState()

    While 1
        $Msg = GUIGetMsg()
        For $i = 1 To 8
            If $opt_Colours[$i][2] <> GUICtrlRead($opt_Colours[$i][0]) Then
                $opt_Colours[$i][2] = GUICtrlRead($opt_Colours[$i][0])
                $Msg = $opt_Colours[$i][0]
                ExitLoop
            EndIf
        Next
        Switch $Msg
            Case $opt_Default, $opt_Save
                If $Msg = $opt_Default Then
                    If MsgBox(4, "Confirm Reset", "Are you sure?") <> 6 Then ContinueLoop
                    IniDelete($Ini, "Dimensions")
                    IniDelete($Ini, "Colours")
                Else
                    If MsgBox(4, "Confirm Save", "These values aren't checked for validity" & @CRLF & "Are you sure they are legal?") <> 6 Then ContinueLoop
                    IniWrite($Ini, "Dimensions", "Rows", GUICtrlRead($opt_Rows))
                    IniWrite($Ini, "Dimensions", "Columns", GUICtrlRead($opt_Columns))
                    IniWrite($Ini, "Dimensions", "BtnWidth", GUICtrlRead($opt_btnWidth))
                    IniWrite($Ini, "Dimensions", "BtnHeight", GUICtrlRead($opt_btnHeight))
                    IniWrite($Ini, "Dimensions", "ClearBtnWidth", GUICtrlRead($opt_ClearbtnWidth))
                    For $i = 1 To 8
                        IniWrite($Ini, "Colours", $i, GUICtrlRead($opt_Colours[$i][0]))
                    Next
                EndIf
                ContinueCase
            Case -3
                If Not @Compiled Then
                    Run('"' & @AutoItExe & '" "' & @ScriptFullPath & '"')
                Else
                    Run('"' & @ScriptFullPath & '"')
                EndIf
                Exit
            Case $opt_Colours[1][0] To $opt_Colours[8][0]
                For $i = 1 To 8
                    If $Msg = $opt_Colours[$i][0] Then GUICtrlSetBkColor($opt_Colours[$i][1], GUICtrlRead($opt_Colours[$i][0]))
                Next
        EndSwitch
    WEnd
EndFunc

Func _ShowSortMenu()
    Local $stPoint = DllStructCreate("int;int")
    DllStructSetData($stPoint, 1, 0)
    DllStructSetData($stPoint, 2, $Dim_TotHeight)

    DllCall("user32.dll", "int", "ClientToScreen", "hwnd", $hGUI, "ptr", DllStructGetPtr($stPoint))
    DllCall("user32.dll", "int", "TrackPopupMenuEx", "hwnd", GUICtrlGetHandle($SortContext[0]), "int", 0, "int", _
        DllStructGetData($stPoint, 1), "int", DllStructGetData($stPoint, 2), "hwnd", $hGUI, "ptr", 0)
EndFunc

Share this post


Link to post
Share on other sites



Thanks mate - very much appreciated.

M

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