Jump to content

Button Changer


achraf
 Share

Recommended Posts

I want to create a button with image but when you my the mouse button under its change to another image

Help Me please I really need

And ThankS You

Hi and Welcome to the forums.

Not sure how you put your mouse under something, but here are some things to look up in the helpfile.

GUIGetCursorInfo ( [winhandle] )

It returns an array.

So if the select loop of you GUI is like so:

While 1
    $msg = GUIGetMsg ()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case $msg = $button
            MsgBox (0, "", "The button that was assigned to $button was clicked!")
    EndSelect
WEnd

Then all you need to do is add the GUIGetCursorInfo, and tell it to check for mouse over of the button. Like so:

While 1
    $msg = GUIGetMsg ()
    $cur = GUIGetCursorInfo ()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case $msg = $button
            MsgBox (0, "", "The button that was assigned to $button was clicked!")
        Case $cur[4] = $button
            MsgBox (0, "", "The button that was assigned to $button has the mouse over it!")
    EndSelect
WEnd

Now, the other bit. Changing the picture. All that is required is a simple function,

GUICtrlSetImage ( controlID, filename [, iconname [, icontype]] )

If you have any troubles, first look them up in the helpfile, and then post a question here. You'll find the helpfile and the examples included for most if not all of the functions will help. Finally, you'll probably notice that if you added the opposite of the $cur[4] = $button (make it not equal) that you'll be able to change the image back, but there will be lots of flickering, from both ends of the scale. I now direct you to something I wrote not too long ago, that should demonstrate what you need, but you will need to adapt it, as it changed the color of controls, not the image.

#include <GUIConstants.au3>

#Region ### START Koda GUI section ### Form=d:\my documents\Files\AU3\My Scripts\Abook\coolGUI.kxf
$Form1 = GUICreate("Form1", 801, 601, -1, -1, BitOR($WS_POPUP, $WS_BORDER))
GUISetBkColor (0xDDDDDD)
$Label1 = GUICtrlCreateLabel("", 0, 0, 800, 20, -1, $GUI_WS_EX_PARENTDRAG)
GUICtrlSetResizing (-1, $GUI_DOCKHEIGHT+$GUI_DOCKLEFT+$GUI_DOCKRIGHT)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFFFFFF)
GUICtrlSetBkColor(-1, 0x000000)
GUICtrlSetState(-1, $GUI_DISABLE)
$Label2 = GUICtrlCreateLabel("Label2", 24, 2, 539, 18, -1, $GUI_WS_EX_PARENTDRAG)
GUICtrlSetResizing (-1, $GUI_DOCKSIZE+$GUI_DOCKRIGHT+$GUI_DOCKLEFT)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFFFFFF)
GUICtrlSetBkColor(-1, 0x000000)
$title_close = GUICtrlCreateLabel("X", 788, 2, 12, 18)
GUICtrlSetResizing (-1, $GUI_DOCKSIZE+$GUI_DOCKRIGHT)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFFFFFF)
GUICtrlSetBkColor(-1, 0x000000)
$title_max = GUICtrlCreateLabel("[]", 774, 0, 12, 20)
GUICtrlSetResizing (-1, $GUI_DOCKSIZE+$GUI_DOCKRIGHT)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFFFFFF)
GUICtrlSetBkColor(-1, 0x000000)
$title_min = GUICtrlCreateLabel("_", 762, 0, 12, 20)
GUICtrlSetResizing (-1, $GUI_DOCKSIZE+$GUI_DOCKRIGHT)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFFFFFF)
GUICtrlSetBkColor(-1, 0x000000)
$icon = GUICtrlCreatePic (@Systemdir & "\oobe\images\mslogo.jpg", 1, 1, 18, 18, -1, $GUI_WS_EX_PARENTDRAG)
GUICtrlSetImage (-1, @SystemDir & "\shell32.dll", 22, 0)
GUICtrlSetResizing (-1, $GUI_DOCKSIZE+$GUI_DOCKLEFT)
;GUICtrlSetBkColor(-1, 0x000000)
GUISetState(@SW_SHOW)
#EndRegion ### START Koda GUI section ### Form=d:\my documents\Files\AU3\My Scripts\Abook\coolGUI.kxf
$hover1 = False
$hover2 = False
$hover3 = False
$unhover1 = False
$unhover2 = False
$unhover3 = False
$maxmized = False
$mousoverbtns = 0
While 1
    $nMsg = GUIGetMsg()
    $mousover = GUIGetCursorInfo()
    If IsArray($mousover) Then
        $mousoverbtns = $mousover[4]
    Else
        $mousoverbtns = 0
    EndIf
    Select
        Case $nMsg = $GUI_EVENT_CLOSE or $nMsg = $title_close
            Exit
        Case $nMsg = $title_min
            GUISetState (@SW_MINIMIZE)
        Case $nMsg = $title_max
            If $maxmized Then
                $maxmized = False
                GUISetState (@SW_RESTORE)
            Else
                $maxmized = True
                GUISetState (@SW_MAXIMIZE)
            EndIf
        Case $mousoverbtns <> 0
            If $mousover[4] = $title_close Then
                If $hover1 <> True Then
                    GUICtrlSetColor($title_close, 0xDDDDDD)
                    $hover1 = True
                    $unhover1 = True
                EndIf
            ElseIf $mousover[4] = $title_min Then
                If $hover2 <> True Then
                    GUICtrlSetColor($title_min, 0xDDDDDD)
                    $hover2 = True
                    $unhover2 = True
                EndIf
            ElseIf $mousover[4] = $title_max Then
                If $hover3 <> True Then
                    GUICtrlSetColor($title_max, 0xDDDDDD)
                    $hover3 = True
                    $unhover3 = True
                EndIf
            EndIf
        Case $mousoverbtns == 0
            If $unhover1 = True Then
                $hover1 = False
                $unhover1 = False
                GUICtrlSetColor($title_close, 0xFFFFFF)
                GUICtrlSetColor($title_max, 0xFFFFFF)
                GUICtrlSetColor($title_min, 0xFFFFFF)
            ElseIf $unhover2 = True Then
                $hover2 = False
                $unhover2 = False
                GUICtrlSetColor($title_close, 0xFFFFFF)
                GUICtrlSetColor($title_max, 0xFFFFFF)
                GUICtrlSetColor($title_min, 0xFFFFFF)
            ElseIf $unhover3 = True Then
                $hover3 = False
                $unhover3 = False
                GUICtrlSetColor($title_close, 0xFFFFFF)
                GUICtrlSetColor($title_max, 0xFFFFFF)
                GUICtrlSetColor($title_min, 0xFFFFFF)
            EndIf
    EndSelect
WEnd

Hope all this helped!

Cheers

Brett

Link to comment
Share on other sites

Requires version 3.2.11.0 or Higher and Themes

#AutoIt3Wrapper_au3check_parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <GUIConstantsEx.au3>
#include <GuiButton.au3>
#include <GuiImageList.au3>

Opt("MustDeclareVars", 1)

_Main()

Func _Main()
    Local $hImage, $y = 70, $iIcon = 125, $btn[6], $rdo[6], $chk[6], $hImageSmall
    
    GUICreate("Buttons", 510, 400)
    GUISetState()

    $hImage = _GUIImageList_Create(32, 32, 5, 3, 6)
    For $x = 6 To 11
        _GUIImageList_AddIcon($hImage, "shell32.dll", $x, True)
    Next

    $hImageSmall = _GUIImageList_Create(16, 16, 5, 3, 6)
    For $x = 6 To 11
        _GUIImageList_AddIcon($hImageSmall, "shell32.dll", $x)
    Next

    $btn[0] = GUICtrlCreateButton("Button1", 10, 10, 90, 50)
    _GUICtrlButton_SetImageList($btn[0], $hImage)

    $rdo[0] = GUICtrlCreateRadio("Radio Button1", 120, 10, 120, 25)
    _GUICtrlButton_SetImageList($rdo[0], $hImageSmall)

    $chk[0] = GUICtrlCreateCheckbox("Check Button1", 260, 10, 120, 25)
    _GUICtrlButton_SetImageList($chk[0], $hImageSmall)


    For $x = 1 To 5
        $btn[$x] = GUICtrlCreateButton("Button" & $x + 1, 10, $y, 90, 50)
        _GUICtrlButton_SetImageList($btn[$x], _GetImageListHandle("shell32.dll", $iIcon + $x, True), $x)
        $rdo[$x] = GUICtrlCreateRadio("Radio Button" & $x + 1, 120, $y, 120, 25)
        _GUICtrlButton_SetImageList($rdo[$x], _GetImageListHandle("shell32.dll", $iIcon + $x), $x)
        $chk[$x] = GUICtrlCreateCheckbox("Check Button" & $x + 1, 260, $y, 120, 25)
        _GUICtrlButton_SetImageList($chk[$x], _GetImageListHandle("shell32.dll", $iIcon + $x), $x)
        $y += 60
    Next
    
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
        EndSwitch
    WEnd

    Exit
EndFunc   ;==>_Main

; using image list to set 1 image and have text on button
Func _GetImageListHandle($sFile, $nIconID = 0, $fLarge = False)
    Local $iSize = 16
    If $fLarge Then $iSize = 32
    
    Local $hImage = _GUIImageList_Create($iSize, $iSize, 5, 3)
    If StringUpper(StringMid($sFile, StringLen($sFile) - 2)) = "BMP" Then
        _GUIImageList_AddBitmap($hImage, $sFile)
    Else
        _GUIImageList_AddIcon($hImage, $sFile, $nIconID, $fLarge)
    EndIf
    Return $hImage
EndFunc   ;==>_GetImageListHandle

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

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