Jump to content

Highlight Labels on Mouse Hover


Recommended Posts

I have the below sample code where I wanted the Black Background to be highlighted when Mouse is hovered on top of it + anywhere I keep the mouse cursor inside the labels.

Problem is, because I have multiple Labels inside the main label, the background color goes off when cursor is placed on top of any of them. Additionally it starts flickering.

I cannot keep just 1 Label with @CRLF and texts below due to various reasons inside my code. Also This is a Sample and I have 44 of these in my code where I have named most of the $hLabel_1, ... as the Label names.

Can someone help to get this corrected to get the black background highlighted only when I hover the mouse inside anywhere in the labels?

#include <GUIConstantsEx.au3>

$hGUI = GUICreate("Sample", 200, 300)
GUISetBkColor(0xFFFFFF, $hGUI) ; White Color

$hlabel_1 = GUICtrlCreateLabel("", 8, 8, 104, 104) ; Keeping this as the Background Label for Enabling on Mouse Hover
$hlabel_2 = GUICtrlCreateLabel("", 11, 11, 98, 98)
GUICtrlSetBkColor(-1, 0x008000) ; Green Color
$hlabel_3 = GUICtrlCreateLabel("Test 1", 11, 25, 98, 20)
GUICtrlSetBkColor(-1, 0x008000) ; Green Color
$hlabel_4 = GUICtrlCreateLabel("Test 2", 11, 45, 98, 20)
GUICtrlSetBkColor(-1, 0x008000) ; Green Color
$hlabel_5 = GUICtrlCreateLabel("Test 3", 11, 65, 98, 20)
GUICtrlSetBkColor(-1, 0x008000) ; Green Color


$hlabel_6 = GUICtrlCreateLabel("", 8, 119, 104, 104) ; Keeping this as the Background Label for Enabling on Mouse Hover
$hlabel_7 = GUICtrlCreateLabel("", 11, 121, 98, 98)
GUICtrlSetBkColor(-1, 0x008000) ; Green Color
$hlabel_8 = GUICtrlCreateLabel("Test 4", 11, 125, 98, 20)
GUICtrlSetBkColor(-1, 0x008000) ; Green Color
$hlabel_9 = GUICtrlCreateLabel("Test 5", 11, 145, 98, 20)
GUICtrlSetBkColor(-1, 0x008000) ; Green Color
$hlabel_10 = GUICtrlCreateLabel("Test 6", 11, 165, 98, 20)
GUICtrlSetBkColor(-1, 0x008000) ; Green Color
$hlabel_11 = GUICtrlCreateLabel("Test 7", 11, 185, 98, 20)
GUICtrlSetBkColor(-1, 0x008000) ; Green Color

GUISetState()

$ifFlag = False ; If True then Highlight Labels

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    $aInfo = GUIGetCursorInfo($hGUI)
    If $aInfo[4] = $hLabel_2 And $ifFlag = False Then
        GUICtrlSetBkColor($hlabel_1, 0x000000) ; Change White Color to Black
        $ifFlag = True
    ElseIf $aInfo[4] <> $hLabel_2 And $ifFlag = True Then
        GUICtrlSetBkColor($hlabel_1, 0xFFFFFF) ; Change Black Color to White
        $ifFlag = False
    EndIf


    If $aInfo[4] = $hlabel_7 And $ifFlag = False Then
        GUICtrlSetBkColor($hlabel_6, 0x000000) ; Change White Color to Black
        $ifFlag = True
    ElseIf $aInfo[4] <> $hlabel_7 And $ifFlag = True Then
        GUICtrlSetBkColor($hlabel_6, 0xFFFFFF) ; Change Black Color to White
        $ifFlag = False
    EndIf


WEnd

 

Link to comment
Share on other sites

Try this one.

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

$hGUI = GUICreate("Sample", 200, 300) ;,-1,-1,-1,$WS_EX_COMPOSITED)
GUISetBkColor(0xFFFFFF, $hGUI) ; White Color

$hlabel_1 = GUICtrlCreateLabel("", 8, 8, 104, 104) ; Keeping this as the Background Label for Enabling on Mouse Hover
$hlabel_2 = GUICtrlCreateLabel("", 11, 11, 98, 98)
GUICtrlSetBkColor(-1, 0x008000) ; Green Color
$hlabel_3 = GUICtrlCreateLabel("Test 1", 11, 25, 98, 20)
GUICtrlSetBkColor(-1, 0x008000) ; Green Color
$hlabel_4 = GUICtrlCreateLabel("Test 2", 11, 45, 98, 20)
GUICtrlSetBkColor(-1, 0x008000) ; Green Color
$hlabel_5 = GUICtrlCreateLabel("Test 3", 11, 65, 98, 20)
GUICtrlSetBkColor(-1, 0x008000) ; Green Color


$hlabel_6 = GUICtrlCreateLabel("", 8, 119, 104, 104) ; Keeping this as the Background Label for Enabling on Mouse Hover
$hlabel_7 = GUICtrlCreateLabel("", 11, 121, 98, 98)
GUICtrlSetBkColor(-1, 0x008000) ; Green Color
$hlabel_8 = GUICtrlCreateLabel("Test 4", 11, 125, 98, 20)
GUICtrlSetBkColor(-1, 0x008000) ; Green Color
$hlabel_9 = GUICtrlCreateLabel("Test 5", 11, 145, 98, 20)
GUICtrlSetBkColor(-1, 0x008000) ; Green Color
$hlabel_10 = GUICtrlCreateLabel("Test 6", 11, 165, 98, 20)
GUICtrlSetBkColor(-1, 0x008000) ; Green Color
$hlabel_11 = GUICtrlCreateLabel("Test 7", 11, 185, 98, 20)
GUICtrlSetBkColor(-1, 0x008000) ; Green Color

GUISetState()

Local $ifFlag = False ; If True then Highlight Labels
Local $idHov = 0

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
    $aInfo = GUIGetCursorInfo($hGUI)
    ;#cs
    If $idHov = 0 And $ifFlag = False And _
            Not ($aInfo[4] = $hlabel_1 Or $aInfo[4] = $hlabel_2 Or $aInfo[4] = $hlabel_6 Or $aInfo[4] = $hlabel_7) Then
        GUICtrlSetBkColor($aInfo[4], 0xFFFFFF) ; Change White Color
        $ifFlag = True
        $idHov = $aInfo[4]
        Sleep(10)
    ElseIf $idHov <> $aInfo[4] And $ifFlag = True And _
            Not ($aInfo[4] = $hlabel_1 Or $aInfo[4] = $hlabel_2 Or $aInfo[4] = $hlabel_6 Or $aInfo[4] = $hlabel_7) Then
        GUICtrlSetBkColor($idHov, 0x008000) ; Change Black Color to White
        $ifFlag = False
        $idHov = 0
        Sleep(10)
    EndIf
    ;#ce
    Sleep(10)

    ;  $hlabel_6 = GUICtrlCreateLabel("", 8, 119, 104, 104)
    If ($aInfo[0] > 8 And $aInfo[0] < (8 + 104) And $aInfo[1] > 119 And $aInfo[1] < (119 + 104)) Then
        GUICtrlSetBkColor($hlabel_6, 0x000000) ; Change White Color to Black
        GUICtrlSetBkColor($hlabel_1, 0xFFFFFF) ; Change Black Color to White
        Sleep(10)

        ; $hlabel_1 = GUICtrlCreateLabel("", 8, 8, 104, 104)
    ElseIf ($aInfo[0] > 8 And $aInfo[0] < (8 + 104) And $aInfo[1] > 8 And $aInfo[1] < (8 + 104)) Then
        GUICtrlSetBkColor($hlabel_1, 0x000000) ; Change White Color to Black
        GUICtrlSetBkColor($hlabel_6, 0xFFFFFF) ; Change Black Color to White
        Sleep(10)
    ElseIf Not ($aInfo[0] > 8 And $aInfo[0] < (8 + 104) And $aInfo[1] > 119 And $aInfo[1] < (119 + 104)) And _
            Not ($aInfo[0] > 8 And $aInfo[0] < (8 + 104) And $aInfo[1] > 8 And $aInfo[1] < (8 + 104)) Then
        GUICtrlSetBkColor($hlabel_1, 0xFFFFFF) ; Change to White
        GUICtrlSetBkColor($hlabel_6, 0xFFFFFF) ; Change to White
        Sleep(10)
    EndIf

WEnd

 

Edited by Malkey
Added Sleep() functions to slow down flickering.
Link to comment
Share on other sites

If you have 44 frames then you might use a 2D array

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

$hGUI = GUICreate("Sample", 200, 300) ;,-1,-1,-1,$WS_EX_COMPOSITED)
GUISetBkColor(0xFFFFFF, $hGUI) ; White Color

Local $hlabels[9][2]

$hlabel_b1 = GUICtrlCreateLabel("", 8, 8, 104, 104) ; Keeping this as the Background Label for Enabling on Mouse Hover
$hlabels[0][0] = GUICtrlCreateLabel("", 11, 11, 98, 98)
$hlabels[1][0] = GUICtrlCreateLabel("Test 1", 11, 25, 98, 20)
$hlabels[2][0] = GUICtrlCreateLabel("Test 2", 11, 45, 98, 20)
$hlabels[3][0] = GUICtrlCreateLabel("Test 3", 11, 65, 98, 20)
For $i = 0 to 3
   GUICtrlSetBkColor($hlabels[$i][0], 0x008000) ; Green Color
   $hlabels[$i][1] = $hlabel_b1
Next

$hlabel_b2 = GUICtrlCreateLabel("", 8, 119, 104, 104) ; Keeping this as the Background Label for Enabling on Mouse Hover
$hlabels[4][0] = GUICtrlCreateLabel("", 11, 121, 98, 98)
$hlabels[5][0] = GUICtrlCreateLabel("Test 4", 11, 125, 98, 20)
$hlabels[6][0] = GUICtrlCreateLabel("Test 5", 11, 145, 98, 20)
$hlabels[7][0] = GUICtrlCreateLabel("Test 6", 11, 165, 98, 20)
$hlabels[8][0] = GUICtrlCreateLabel("Test 7", 11, 185, 98, 20)
For $i = 4 to 8
   GUICtrlSetBkColor($hlabels[$i][0], 0x008000) ; Green Color
   $hlabels[$i][1] = $hlabel_b2
Next

GUISetState()

Local $idHov[2] = [0, 0], $toggle


While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

 $aInfo = GUIGetCursorInfo($hGUI)

; pointer outside labels
 If $aInfo[4] = 0 AND $toggle = 0 Then
     GUICtrlSetBkColor($idHov[0], 0x008000) ; Green Color
     GUICtrlSetBkColor($idHov[1], 0xFFFFFF) ; Change Black Color to White
     $toggle = 1 
     Local $idHov[2] = [0, 0]
     Continueloop
 EndIf

; pointer inside same label
If $aInfo[4] = $idHov[0] OR $aInfo[4] = $idHov[1] Then Continueloop

; pointer inside another label
    GUICtrlSetBkColor($idHov[0], 0x008000) ; Green Color
    GUICtrlSetBkColor($idHov[1], 0xFFFFFF) ; Change Black Color to White
    $toggle = 0 
    For $i = 0 to 8
       If $aInfo[4] = $hlabels[$i][0] Then
            $idHov[0] = $hlabels[$i][0]
            $idHov[1] = $hlabels[$i][1]
        EndIf
    Next
  GUICtrlSetBkColor($idHov[0], 0xFFFFFF) ; Change White Color
  GUICtrlSetBkColor($idHov[1], 0x000000) ; Change White Color to Black

WEnd

 

Edited by mikell
comments added...
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...