Jump to content

Getting flicker with cursor-hover technique


timmy2
 Share

Recommended Posts

I'm using a graphic button in a form and I want to change it to another image when the cursor is hovered over the button. 

I've got that working but the button flickers constantly.

Here's my script:

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

; create borderless, chromeless form into which image will be placed
$Form1 = GUICreate("Welcome", 500, 500, -1, -1, BitOR($WS_SYSMENU,$WS_POPUP), 0)

; place image file in form
$Pic1 = GUICtrlCreatePic("Welcometest.bmp", 0, 0, 500, 500)

; needed for some reason
GuiCtrlSetState(-1,$GUI_DISABLE)

; display "Click to Continue" button
$C2CBtn = GUICtrlCreatePic("C2C_off.bmp", 300, 300)

; display all of the above as a unit
GUISetState(@SW_SHOW)

While 1
        $ggci = GUIGetCursorInfo($Form1)
        If $ggci[4] = $C2CBtn Then
            GUICtrlSetImage($C2CBtn,"C2C_on.bmp")
        Else
            GUICtrlSetImage($C2CBtn,"C2C_off.bmp")
        EndIf

        $nMsg = GuiGetMsg()
        Switch $nMsg
            Case $C2CBtn
                Exit
        EndSwitch
        Sleep(100)
    WEnd

Resulting GUI

DmFaqffm.jpg

Hovering the cursor over the button changes it as desired. Clicking on the button exits the demo (also desired).  But it flickers constantly. Not desired! ;-)

The attached button images and background image are all 24b bmp files. The actual image used in place of the white box consists of high quality text and graphics so I can't use JPG format.

 

Welcometest.bmp

C2C_off.bmp

C2C_on.bmp

Link to comment
Share on other sites

  • Developers

Couple of pointers:

  • Only set the control image when it needs changing, not 10 times per second!
  • Never use a Sleep() in a GUI message loop! as that could lead to slowness in response or missed actions.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

This is what Jos meant:

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

Global $b_on_off = 0 ; state of button: 0-off 1-on

; create borderless, chromeless form into which image will be placed
$Form1 = GUICreate("Welcome", 500, 500, -1, -1, BitOR($WS_SYSMENU,$WS_POPUP), 0)

; place image file in form
$Pic1 = GUICtrlCreatePic("Welcometest.bmp", 0, 0, 500, 500)

; needed for some reason
GuiCtrlSetState(-1,$GUI_DISABLE)

; display "Click to Continue" button
$C2CBtn = GUICtrlCreatePic("C2C_off.bmp", 300, 300)

; display all of the above as a unit
GUISetState(@SW_SHOW)

While 1
        $ggci = GUIGetCursorInfo($Form1)
        If $ggci[4] = $C2CBtn Then
            If $b_on_off = 0 Then
                GUICtrlSetImage($C2CBtn,"C2C_on.bmp")
                $b_on_off = 1
            EndIf
        Else
            If $b_on_off = 1 Then
                GUICtrlSetImage($C2CBtn,"C2C_off.bmp")
                $b_on_off = 0
            EndIf
        EndIf

        $nMsg = GuiGetMsg()
        Switch $nMsg
            Case $C2CBtn
                Exit
        EndSwitch
;~         Sleep(100)
WEnd

 

Link to comment
Share on other sites

8 hours ago, Jos said:

Couple of pointers:

  • Only set the control image when it needs changing, not 10 times per second!
  • Never use a Sleep() in a GUI message loop! as that could lead to slowness in response or missed actions.

Jos

Thank you, @Jos and @Zedna! Jos's reply inspired me to go down the path you so graciously completed for me.  Despite the changes shown below I was still wrestling with flickering during hovering when notification of your post appeared.  If coding were whittling I would be knee deep in a pile of discards.

I appreciate your help immensely.

If $ggci[4] = $C2CBtn And $iC2CBtnState = 0 Then
            GUICtrlSetImage($C2CBtn,"C2C_on.bmp")
            $iC2CBtnState = 1
        Else
            if $iC2CBtnState = 1 then GUICtrlSetImage($C2CBtn,"C2C_off.bmp")
            $iC2CBtnState = 0
        EndIf

 

Link to comment
Share on other sites

Just FYI, I created and tested the above script in AutoIt 3.3.14.2.  After upgrading to 3.3.14.5 the internal graphic is resized, I presume because GUICtrlCreatePic is now more strictly obeying the description for width and height: (default is the previously used width).

5bfe1765440b0_Hoverdemo(showingdifbetweenAIversions)-forumver.jpg.91e6ee88b007eb0bb7b8d05447f204bc.jpg

so the following line of code in my original post should now include the dimensions of the click-to-continue button:

; display "Click to Continue" button
$C2CBtn = GUICtrlCreatePic("C2C_off.bmp", 300, 300, 144, 144)

 

 

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

×
×
  • Create New...