Jump to content

modified _ButtonHover (from Valuater)


Sundance
 Share

Recommended Posts

I modified the _ButtonHover.au3 from Valuater, so you can have a theme for every button you create.

Here is the _ButtonHover_mod.au3

#region _ButtonHover;
;===============================================================================
;   UDF Name: _ButtonHover.au3
;
;   Version v1.0.0  January 1, 2007, built with Autoit 3.2.1.+
;   modification made by Sundance on
;                        July    11,2008, built with AutoIt 3.2.12.1
;
;   Author: Valuater, Robert M
;
;   Contributions: marfdaman(Marvin), RazerM, Quaizywabbit(AnyGUI).
;
;   Email: <Valuater [at] aol [dot] com>
;
;   Use: Mouse/Hover Over Buttons with chamge picture events
;===============================================================================

#include-once
#include <GUIConstants.au3>
#include <StaticConstants.au3>
#include <Array.au3>

Global $cHoverID[2], $cHoverImagePath[2], $cHoverImageType[2], $Global_cHID = 0, $cHoverActive = 0, $cHClicked = 0, $XButton_Location, $XBType

;===============================================================================
;
; Function Name:    _ControlHover()
; Description:    Enable Mouse/Hover Over Control Events
;                   Find if Mouse is over a known control
;                   Find if a Mouse has Clicked on a known control
;                   Add, Remove, or Verify the control within the known control list
; Parameter(s):  $cH_Mode   [default]      0 = Find if Mouse is over a known control
;                              [optional]       1 = Find if a Mouse has Clicked on a known control
;                                              2 = Add a control to the known control list
;                                              3 = Remove a control from the known control list
;                                              4 = Verify a control is in the known control list
;                  $cH_hWin [optional]     - Window handle as returned by GUICreate()
;                               [preferred]     - Window handle to save CPU usage and speed of _ControlHover()
;                                               - and Avoid array errors for "non-active window"
;                  $cH_hCntrl   [required]     - Control ID to Add, Remove, or Verify the control within the known control list
;                               [not used]      - to find if a mouse is over or has clicked on a known control
; Requirement(s):   #include<Array.au3>
; Return Value(s):
;                   $cH_Mode 0 or 1
;                               On Success   Return = 1     @error = 0, @extended = Current known control
;                              While Success Return = ""
;                              On Failure: Return = 0       @error = 1, @extended = Last known control
;                   $cH_Mode 2, 3, or 4
;                               On Success   Return = 1     @error = 0, @extended = Function's return line number
;                   All $cH_Mode's
;                              On Error:     Return = Description of the error
;                                                           @error = -1, @extended = Function's return line number
; Author(s):        Valuater, Robert M
;
;===============================================================================
Func _ControlHover($cH_Mode = 0, $cH_hWin = "", $cH_hCntrl = "", $cH_ImagePath = "", $cH_ImageType = "")
    
    Local $cH_i, $cH_Data
    If Not @Compiled Then Opt("TrayIconDebug", 1); 1=debug line info
    
    Select
        Case $cH_Mode = 0 Or $cH_Mode = 1; 0 = Check if mouse is over a control, 1 = Check if mouse clicked on a control
            
           ; Developer did not add any controls before the function mode 0 or 1 was called
            If UBound($cHoverID) = 2 Then ConsoleWrite("*** _ControlHover() Error *** " & @CRLF & "No Controls were Added" & @CRLF)
            
            If $cH_hWin = "" Then
                $cH_Data = GUIGetCursorInfo(); Get cursor data from the active window
            Else
               ; cause of the problem that the msgbox does not get the focus back to the maingui and the pushed button stays in pressed mode
               ; we waste some cpu time :-)
;~               If Not WinActive($cH_hWin) Then
;~                           SetError(-1, 1, "Window not active")
;~                       EndIf
;~                       Return SetError(-1, 1, "Window not active"); Dont waste CPU if the known window is not active
;~               EndIf
                $cH_Data = GUIGetCursorInfo($cH_hWin); Get cursor data from the known window
            EndIf
            
            If Not IsArray($cH_Data) Then Return SetError(-1, 2, "Window not found"); A readable GUI window was not found
            
            For $cH_i = 1 To UBound($cHoverID) - 1; Search the known controls for the currently hovered control
                If $cH_Data[4] = $cHoverID[$cH_i] Then; Mouse is over a current known control
                    
                   ; Mode 1 - check for a Click on a known control
                    If $cH_Mode And $cH_Data[2] = 1 And Not $cHClicked Then
                        $cHClicked = 1
;~                       ConsoleWrite ( "Mouse clicked on current control" & @CRLF )
                        Return SetError(0, $Global_cHID, 1); Mouse clicked on current control
                    ElseIf $cH_Mode And $cH_Data[2] <> 1 And $cHClicked Then
                        $cHClicked = 0
;~                       ConsoleWrite ( "Release clicked current control" & @CRLF )
                        Return SetError(1, $Global_cHID, 0); Release clicked current control
                    EndIf
                    If $cH_Mode Then
;~                       ConsoleWrite ( "Mouse is still over last known control and may click again" & @CRLF )
                        Return SetError("", "", -1); Mouse is still over last known control and may click again
                    EndIf
                    
                   ; Mode 0 & 1
                    If $cHoverID[$cH_i] = $cHoverID[$Global_cHID] And $cHoverActive Then
;~                           ConsoleWrite ( "Mouse is still over last known control" & @CRLF )
                            Return SetError("", "", -1); Mouse is still over last known control
                    EndIf
;~                   ConsoleWrite ( "Mouse is not still over last known control" & @CRLF )  
                    If $cHoverActive Then
                        $cHoverActive = 0
;~                       ConsoleWrite ( "Release Active - Mouse is over a new known control" & @CRLF )
                        Return SetError(1, $Global_cHID, 0); Release Active - Mouse is over a new known control
                    EndIf
;~                   ConsoleWrite ( "Set New Path/Type/cHid" & @CRLF )
                    $cHoverActive = 1
                    $cHoverImagePath[$Global_cHID]  = $cHoverImagePath[$cH_i]
                    $cHoverImageType[$Global_cHID]  = $cHoverImageType[$cH_i]
                    $cHoverID[$Global_cHID]         = $cHoverID[$cH_i]; Re-set the current control to the new known control
;~                   ConsoleWrite ( "Mouse is over the new-current known control" & @CRLF )
                    Return SetError(0, $Global_cHID, 1); Mouse is over the new-current known control
                    
                EndIf
            Next
           ; hover control not found in the known control list
            If $cHoverActive Then
                If $cH_Mode And $cHClicked Then ; check - Release clicked current control
                    $cHClicked = 0
;~                   ConsoleWrite ( "Release clicked current control" & @CRLF )
                    Return SetError(1, $Global_cHID, 0); Release clicked current control
                EndIf
                If $cH_Mode Then
;~                       ConsoleWrite ( "Protect the release of the active control" & @CRLF )
                        Return SetError("", "", -1); Protect the release of the active control
                EndIf
                $cHoverActive = 0
;~               ConsoleWrite ( "Release Active - Mouse is over a new unknown control" & " CHmode=" & $cH_Mode & @CRLF )
                Return SetError(1, $Global_cHID, 0); Release Active - Mouse is over a new unknown control
            EndIf
            If $cH_Mode And $cHClicked Then
                $cHClicked = 0
;~               ConsoleWrite ( "Release clicked current control" & @CRLF )
                Return SetError(1, $Global_cHID, 0); Release clicked current control
            EndIf
;~           ConsoleWrite ( "3xnix" & @ScriptLineNumber & @CRLF )
            Return SetError("", "", -1)
            
        Case $cH_Mode = 2; Add the new control to the known control list
            
            If $cH_hCntrl = "" Then Return SetError(-1, 8, "Control not provided #1"); The control ID was not given
            _ArrayAdd($cHoverImageType, $cH_ImageType)   ; Add the control to the known control array
            _ArrayAdd($cHoverImagePath, $cH_ImagePath)   ; Add the control to the known control array
            _ArrayAdd($cHoverID,        $cH_hCntrl)    ; Add the control to the known control array
            If @error Then Return SetError(-1, 9, "Control not added"); Control not added to the known control list
            Return SetError(0, 10, 1);; Control was added to the known control list
            
        Case $cH_Mode = 3; Remove the control from the known control list
            
            If $cH_hCntrl = "" Then Return SetError(-1, 11, "Control not provided #2"); The control ID was not given
            _ArrayDelete($cHoverImageType,  $cH_hCntrl); Delete the control from the known control array list
            _ArrayDelete($cHoverImagePath,  $cH_hCntrl); Delete the control from the known control array list
            _ArrayDelete($cHoverID,         $cH_hCntrl); Delete the control from the known control array list
            If @error Then Return SetError(-1, 12, "Control not removed"); Control not deleted from the known control list
            Return SetError(0, 13, 1); Control was deleted from the known control list
            
        Case $cH_Mode = 4; Verify if control is in the known control list
            
            If $cH_hCntrl = "" Then Return SetError(-1, 14, "Control not provided #3"); The control ID was not given
            _ArraySearch($cHoverID, $cH_hCntrl); Search to verify if control is in the known control list
            If @error Then Return SetError(-1, 15, "Control not found"); Control was not found in the known control list
            Return SetError(0, 16, 1); Control was found in the known control list
            
        Case Else
            Return SetError(-1, 17, "$cH_Mode incorrect"); $cH_Mode has an incorrect value
    EndSelect
    Return SetError(-1, 18, "Unknown Error"); Error is Unknown
EndFunc  ;==>_ControlHover

Func _HoverButton($XBtext, $XBleft, $XBtop, $XBwidth, $XBheight, $XBcolor = "", $XBImagePath = "", $XBImageType = "")
    If Not FileExists($XBImagePath & "\") Or ($XBImagePath = "") Or ($XBImageType = "") Then
        MsgBox(64, "Error", " $XBImagePath/Type - was not found/specified   ", 3)
        Return
    EndIf
    
    Local $Xbtn[4]
    $Xbtn[0] = GUICtrlCreatePic($XBImagePath & "\Normal" & $XBImageType, $XBleft, $XBtop, $XBwidth, $XBheight)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    $Xbtn[1] = GUICtrlCreateLabel($XBtext, $XBleft, $XBtop, $XBwidth, $XBheight, $SS_NOTIFY & $SS_CENTER)
    If $XBcolor <> "" Then GUICtrlSetColor(-1, $XBcolor)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    $Xbtn[2] = $XBImagePath
    $Xbtn[3] = $XBImageType
    _ControlHover(2, "", $Xbtn[1], $XBtn[2], $XBtn[3])
    Return $Xbtn[0]
EndFunc  ;==>_HoverButton

Func _CheckHoverAndPressed($_GUI = "")
    $CtrlId2 = _ControlHover(1, $_GUI)
    
    If $CtrlId2 = 1 Then
        GUICtrlSetImage($cHoverID[$Global_cHID]-1, @ScriptDir & "\" & $cHoverImagePath[$Global_cHID] & "\Press" & $cHoverImageType[$Global_cHID])
;~       ConsoleWrite ( $cHoverID[$tempID2]-1 & " result=" & $result & @CRLF )
    ElseIf $CtrlId2 = 0 Then
        GUICtrlSetImage($cHoverID[$Global_cHID]-1, @ScriptDir & "\" & $cHoverImagePath[$Global_cHID] & "\Over" & $cHoverImageType[$Global_cHID])
    EndIf
    
    
    $CtrlId = _ControlHover(0, $_GUI)

    If $CtrlId = 1 Then
        GUICtrlSetImage($cHoverID[$Global_cHID]-1, @ScriptDir & "\" & $cHoverImagePath[$Global_cHID] & "\Over" & $cHoverImageType[$Global_cHID])
;~       ConsoleWrite ( "   SetImage = " & @ScriptDir & "\" & $cHoverImagePath[$Global_cHID] & "\Over" & $cHoverImageType[$Global_cHID] & @CRLF)
    ElseIf $CtrlId = 0 Then
        GUICtrlSetImage($cHoverID[$Global_cHID]-1, @ScriptDir & "\" & $cHoverImagePath[$Global_cHID] & "\Normal" & $cHoverImageType[$Global_cHID])
;~       ConsoleWrite ( "   SetImage = " & @ScriptDir & "\" & $cHoverImagePath[$Global_cHID] & "\Normal" & $cHoverImageType[$Global_cHID] & @CRLF)
    EndIf
EndFunc  ;==>_CheckHoverAndPressed
#endregion

Here comes the _Button_Demo_mod.au3

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>
#include <_ButtonHover_mod.au3>
#include <File.au3>

; required
; $XButton_Location = @ScriptDir & "\Buttons\Style_1"

$XButton_Location = FileReadLine(@ScriptDir & "\Buttons\BDefault.txt", 1)
If Not FileExists($XButton_Location) Then $XButton_Location = FileSelectFolder("Button Folders", @ScriptDir & "\Buttons", 2)

$color = ""
If StringInStr($XButton_Location, "15") Then $color = 0xFFFFFF

$Main_GUI = GUICreate("_ButtonHover Demo")

$List = GUICtrlCreateList("", 40, 80, 100, 100)
GUICtrlCreateLabel($XButton_Location, 20, 20, 400, 20)

$Button_1 = _HoverButton ("Small", 180, 80, 70, 15, $color, "Buttons\Style_06",".jpg")
$Button_2 = _HoverButton ("Medium", 180, 120, 90, 30, $color, "Buttons\Style_07",".gif")
$Button_3 = _HoverButton ("Long", 180, 180, 190, 30, $color, "Buttons\Style_08",".jpg")
$Button_4 = _HoverButton ("Large", 180, 240, 190, 60, $color, "Buttons\Style_09",".jpg")

$Button_5 = GUICtrlCreateButton("Select", 50, 200, 80, 25)
GUICtrlSetTip($Button_5, "Select a Button Style")
GUISetState()

$FileList = _FileListToArray(@ScriptDir & "\Buttons", "*.*", 2)
If (Not IsArray($FileList)) Or (@error = 1) Then
    MsgBox(0, "", "No Files\Folders Found.", 5)
Else
    For $x = 1 To UBound($FileList) - 1
        If StringInStr($FileList[$x], "more") Then ContinueLoop
        $result = StringInStr($FileList[$x], "\", 1, -1)
        $final = StringTrimRight($FileList[$x], $result)
        GUICtrlSetData($List, $final, 1)
    Next
EndIf

While 1
   ; required
    

    $msg = GUIGetMsg()
    _CheckHoverAndPressed ($Main_GUI)
    Select
        
        Case $msg = $Button_1
            MsgBox(64, "test", " You pressed Small   ", 3)
        Case $msg = $Button_2
            MsgBox(64, "test", " You pressed Medium- long   ", 3)
        Case $msg = $Button_3
            MsgBox(64, "test", " You pressed Long   ", 3)
        Case $msg = $Button_4
            MsgBox(64, "test", " You pressed Large   ", 3)
        Case $msg = $Button_5
            Buttoner()
        Case $msg = -3
            Exit
        
    EndSelect

WEnd

Func Buttoner()
    $New_Btn = GUICtrlRead($List)
    If $New_Btn = "" Then Return
    FileDelete(@ScriptDir & "\Buttons\BDefault.txt")
    FileWrite(@ScriptDir & "\Buttons\BDefault.txt", @ScriptDir & "\Buttons\" & $New_Btn)
    Run(FileGetShortName(@AutoItExe) & " " & FileGetShortName(@ScriptFullPath))
    Exit
EndFunc  ;==>Buttoner

Put these two files into the folder where the source files from Valuater where.

Here you can get the source package: Valuaters _ButtonHover

Hope someone find this usefull. I think i will clean up some of the code but now weekend is coming. So see you guys.

greetz

Sundance

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...