Jump to content

Change Image on Mouse Over


Recommended Posts

Hello,

i created a GUI with two pictures, and when the mouse cursor moves over a picture it changes to another picture. Then, when cursor leaves it, the old picture is shown. My problem is, that the image is flickering, i think because the image is set again and again when the mouse is over...

Here is my code:

#NoTrayIcon
#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>

$TestGUI = GUICreate("TestGUI", 143, 74, -1, -1, -1,$WS_EX_TOOLWINDOW)

$icon_1 = GUICtrlCreatePic("", 5, 5, 64, 64)
$icon_2 = GUICtrlCreatePic("", 74, 5, 64, 64)
GUICtrlSetImage($icon_1, @ScriptDir & "\icon_1_sw.bmp")
GUICtrlSetImage($icon_2, @ScriptDir & "\icon_2_sw.bmp")
GUISetState()


While 1
    $msg = GUIGetMsg()
    $cursor = GUIGetCursorInfo ()
    If Not @error Then
        Select
            Case $msg = $GUI_EVENT_CLOSE
                ExitLoop
            Case $cursor[4] = $icon_1
                GUICtrlSetImage ($icon_1, @ScriptDir & "\icon_1_co.bmp")
            Case $cursor[4] = $icon_2
                GUICtrlSetImage ( $icon_2, @ScriptDir & "\icon_2_co.bmp")
            Case $cursor[4] <> $icon_1 Or $cursor[4] <> $icon_2
                GUICtrlSetImage ( $icon_1, @ScriptDir & "\icon_1_sw.bmp")
                GUICtrlSetImage ( $icon_2, @ScriptDir & "\icon_2_sw.bmp")
        EndSelect
    EndIf
WEnd

Exit

I believe my problem could be solved, when the image changes only once when the cursor is over the control and then changes once when it leaves, but i don't have an idea how to do this. Maybe u could help me.

Thanks :D

TestGUI.zip

Link to comment
Share on other sites

Hello,

i created a GUI with two pictures, and when the mouse cursor moves over a picture it changes to another picture. Then, when cursor leaves it, the old picture is shown. My problem is, that the image is flickering, i think because the image is set again and again when the mouse is over...

Here is my code:

#NoTrayIcon
#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>

$TestGUI = GUICreate("TestGUI", 143, 74, -1, -1, -1,$WS_EX_TOOLWINDOW)

$icon_1 = GUICtrlCreatePic("", 5, 5, 64, 64)
$icon_2 = GUICtrlCreatePic("", 74, 5, 64, 64)
GUICtrlSetImage($icon_1, @ScriptDir & "\icon_1_sw.bmp")
GUICtrlSetImage($icon_2, @ScriptDir & "\icon_2_sw.bmp")
GUISetState()


While 1
    $msg = GUIGetMsg()
    $cursor = GUIGetCursorInfo ()
    If Not @error Then
        Select
            Case $msg = $GUI_EVENT_CLOSE
                ExitLoop
            Case $cursor[4] = $icon_1
                GUICtrlSetImage ($icon_1, @ScriptDir & "\icon_1_co.bmp")
            Case $cursor[4] = $icon_2
                GUICtrlSetImage ( $icon_2, @ScriptDir & "\icon_2_co.bmp")
            Case $cursor[4] <> $icon_1 Or $cursor[4] <> $icon_2
                GUICtrlSetImage ( $icon_1, @ScriptDir & "\icon_1_sw.bmp")
                GUICtrlSetImage ( $icon_2, @ScriptDir & "\icon_2_sw.bmp")
        EndSelect
    EndIf
WEnd

Exit

I believe my problem could be solved, when the image changes only once when the cursor is over the control and then changes once when it leaves, but i don't have an idea how to do this. Maybe u could help me.

Thanks :D

TestGUI.zip

Something like this but not tested.

#NoTrayIcon
#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>

$TestGUI = GUICreate("TestGUI", 143, 74, -1, -1, -1, $WS_EX_TOOLWINDOW)

$icon_1 = GUICtrlCreatePic("", 5, 5, 64, 64)
$icon_2 = GUICtrlCreatePic("", 74, 5, 64, 64)
GUICtrlSetImage($icon_1, @ScriptDir & "\icon_1_sw.bmp")
GUICtrlSetImage($icon_2, @ScriptDir & "\icon_2_sw.bmp")
GUISetState()

$Icon1UnderCursor = False
$Icon2UnderCursor = False
While 1
    $msg = GUIGetMsg()
    $cursor = GUIGetCursorInfo()
    If Not @error Then
        Select
            Case $msg = $GUI_EVENT_CLOSE
                ExitLoop
                
            Case $cursor[4] = $icon_1 And $Icon1UnderCursor = False
                GUICtrlSetImage($icon_1, @ScriptDir & "\icon_1_co.bmp")
                $Icon1UnderCursor = True
                
            Case $cursor[4] = $icon_2 And $Icon2UnderCursor = False
                GUICtrlSetImage($icon_2, @ScriptDir & "\icon_2_co.bmp")
                $Icon2UnderCursor = True
                
            Case $cursor[4] <> $icon_1 And $$Icon1UnderCursor
                GUICtrlSetImage($icon_1, @ScriptDir & "\icon_1_sw.bmp")
                $Icon1UnderCursor = False

            Case $cursor[4] <> $icon_2 And $Icon2UnderCursor
                GUICtrlSetImage($icon_2, @ScriptDir & "\icon_2_sw.bmp")
                $Icon2UnderCursor = False
        EndSelect
    EndIf
WEnd

Exit
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I'm such an idiot... so easy and i didn't see that... thanks. I improved it, if somebody needs it :D

#NoTrayIcon
#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>

Dim $iHGap = 5, $iVGap = 0, $iCount = 9, $iSize = 48, $iGap = 5, $icon[$iCount + 1], $oIcon[$iCount + 1]

$TestGUI = GUICreate("TestGUI", 300, 300, -1, -1, -1, $WS_EX_TOOLWINDOW)



GUICtrlCreateGroup("", $iHGap, $iVGap, Ceiling($iCount / 2) * $iSize + (2+(Ceiling($iCount/2)+1) * $iGap), 2 * $iSize + 3 * $iGap+8)
For $a = 0 To $iCount
    $oIcon[$a] = 1
    If $a < $iCount / 2 Then
        $icon[$a] = GUICtrlCreatePic("", $iHGap+$iGap + ($a * $iSize) + ($a * $iGap)+1, $iVGap+2 * $iGap+2, $iSize, $iSize, $BS_BITMAP)
    Else
        $b = $a - Ceiling($iCount / 2)
        $icon[$a] = GUICtrlCreatePic("", $iHGap+$iGap + ($b * $iSize) + ($b * $iGap)+1,  $iVGap+3 * $iGap + $iSize+2, $iSize, $iSize, $BS_BITMAP)
    EndIf
    GUICtrlSetImage($icon[$a], @ScriptDir & "\icons\icon_" & $a & "b.bmp")
Next
GUICtrlCreateGroup("", -99, -99, 1, 1)

GUISetState()

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
    EndSelect
    $cursor = GUIGetCursorInfo()
    If Not @error Then
        For $a = 0 To $iCount
            If $cursor[4] = $icon[$a] And $oIcon[$a] = 1 Then
                $oIcon[$a] = 0
                GUICtrlSetImage($icon[$a], @ScriptDir & "\icons\icon_" & $a & "a.bmp")
            ElseIf $cursor[4] <> $icon[$a] And $oIcon[$a] = 0 Then
                $oIcon[$a] = 1
                GUICtrlSetImage($icon[$a], @ScriptDir & "\icons\icon_" & $a & "b.bmp")
            EndIf
        Next
    EndIf
    Sleep (1)
WEnd

Exit

Thank you martin :D

Edited by yetrael
Link to comment
Share on other sites

  • 7 years later...

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