Jump to content

Changing label background on mouseover.


Go to solution Solved by Melba23,

Recommended Posts

in what way? I cannot seem to get anything like that to work, although I am a noob tho. Im trying to change the background color on mouseover and change it back to normal on when the mouse is removed from the Label.

Link to comment
Share on other sites

  • Moderators

ReconX,

Many ways to do this - here is one of the simplest:

#include <GUIConstantsEx.au3>

Global $fOver = False

$hGUI = GUICreate("Test", 500, 500)

$cLabel = GUICtrlCreateLabel("", 10, 10, 100, 100)
GUICtrlSetBkColor($cLabel, 0xFF0000)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    $aCInfo = GUIGetCursorInfo($hGUI)
    If $aCInfo[4] = $cLabel Then
        If $fOver = False Then
            GUICtrlSetBkColor($cLabel, 0x00FF00)
            $fOver = True
        EndIf
    Else
        If $fOver = True Then
            GUICtrlSetBkColor($cLabel, 0xFF0000)
            $fOver = False
        EndIf
    EndIf

WEnd
Please ask if you have any questions. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thank you that simplified everything ten folds, your are a true gentleman and scholar.

Could you help me out by explaining some of the tags so I could understand them more?

Such as; $aCInfo & $fOver.

Link to comment
Share on other sites

  • Moderators

ReconX,

$fOver is a flag that shows whether the cursor is over the label. That way we only change the colour once when the cursor enter/leaves the label - if we do not use the flag the label is recoloured on every pass and we get flashing like this:

#include <GUIConstantsEx.au3>

$hGUI = GUICreate("Test", 500, 500)

$cLabel = GUICtrlCreateLabel("", 10, 10, 100, 100)
GUICtrlSetBkColor($cLabel, 0xFF0000)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    $aCInfo = GUIGetCursorInfo($hGUI)
    If $aCInfo[4] = $cLabel Then
        GUICtrlSetBkColor($cLabel, 0x00FF00)
    Else
        GUICtrlSetBkColor($cLabel, 0xFF0000)
    EndIf

WEnd
$aCInfo is the array returned by the GUIGetCursorInfo function - look in the Help file to see what it contains. I use a sort of Hungarian notation for my variables so I know what each is expected to contain (a = array; i = integer; f = flag; etc).

And yes you would need to code for each label. But what exactly are you trying to do? There might be a better way to code if you explain the overall problem. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

That clears things up a little more. xD

I am trying to make a GUI that gives you the option to download certain software. The only thing similar to what I am trying to do, that I have used is the IOBitToolbox which is the program in the image.

iobit-toolbox.jpg

See how "Win Fix" is highlighted along with the image and text? That's what I am trying to achieve with the mouseover. Another thing is, that when I duplicated the code for another label, it started to flicker a little bit, is that some sort of overload for the $fOver string?

Edited by ReconX
Link to comment
Share on other sites

  • Moderators

ReconX,

As I suspected, there is a much simpler way to code something like that: ;)

#include <GUIConstantsEx.au3>

; An array to hold the label ControlIDs
Global $aLabel[5]
; A variable to show the last label which changed colour
Global $iLastLabel = 0

$hGUI = GUICreate("Test", 500, 500)

For $i = 0 To 4
    $aLabel[$i] = GUICtrlCreateLabel("", 0, 0 + (100 * $i), 500, 100)
    GUICtrlSetBkColor($aLabel[$i], 0x00FF00)
Next

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    ; Get cursor info
    $aCInfo = GUIGetCursorInfo($hGUI)
    ; loop through the label array to see which one is under the cursor
    For $i = 0 To 4
        ; If we are over a new label
        If $aCInfo[4] = $aLabel[$i] And $iLastLabel <> $i Then
            ; Recolour previos label
            GUICtrlSetBkColor($aLabel[$iLastLabel], 0x00FF00)
            ; colour current label
            GUICtrlSetBkColor($aLabel[$i], 0xFF0000)
            ; Store this label
            $iLastLabel = $i
            ; No point in looking further
            ExitLoop
        EndIf
    Next

WEnd
It always helps to explain exactly what you are trying to do when posting - it saves people suggesting less than optimal solutions and gets you a working solution much quicker. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thank you Melba23, you are awesome, and by far the best person I have came across on this forum. I am currently messing around with the code and tweaking it to my liking.

I assume that each label would have to have another label to input the text over it?

Link to comment
Share on other sites

  • Moderators
  • Solution

ReconX,

 

I assume that each label would have to have another label to input the text over it

No, just add the text to the label itself - perhaps using an array of the required text like this:

#include <GUIConstantsEx.au3>

; An array to hold the label ControlIDs
Global $aLabel[5]
; A variable to show the last label which changed colour
Global $iLastLabel = 0
; An array to hold the label text
Global $aText[5] = ["First label", "Second label", "Third label", "Fourth label", "Fifth label"]

$hGUI = GUICreate("Test", 500, 500)

For $i = 0 To 4
    $aLabel[$i] = GUICtrlCreateLabel($aText[$i], 0, 0 + (100 * $i), 500, 100)
    GUICtrlSetBkColor($aLabel[$i], 0x00FF00)
Next

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    ; Get cursor info
    $aCInfo = GUIGetCursorInfo($hGUI)
    ; loop through the label array to see which one is under the cursor
    For $i = 0 To 4
        ; If we are over a new label
        If $aCInfo[4] = $aLabel[$i] And $iLastLabel <> $i Then
            ; Recolour previos label
            GUICtrlSetBkColor($aLabel[$iLastLabel], 0x00FF00)
            ; colour current label
            GUICtrlSetBkColor($aLabel[$i], 0xFF0000)
            ; Store this label
            $iLastLabel = $i
            ; No point in looking further
            ExitLoop
        EndIf
    Next

WEnd
All clear. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Without For-Next.

#include <GUIConstantsEx.au3>

Example()


Func Example()
    Local $hGui, $x, $y, $b[5]
    $hGui = GUICreate("Hover control to change its background", 400, 400)
    $x = GUICtrlCreateLabel("Label x", 10, 10, 50)
    $y = GUICtrlCreateLabel("Label y", 10, 30, 50)
    GUICtrlCreateButton("Button", 10, 60)
    GUICtrlCreateEdit("", 10, 100, 110, 150)
    GUISetState(@SW_SHOW)

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
        EndSwitch

        $a = GUIGetCursorInfo($hGui)
        If $b[4] <> $a[4] Then
            GUICtrlSetBkColor($b[4], 0xFFFFFF) ; White
            $b = $a
            GUICtrlSetBkColor($a[4], 0xFF0000) ; Red in hex colour format 0xRRGGBB
        EndIf

    WEnd

    GUIDelete()
EndFunc   ;==>Example
Link to comment
Share on other sites

Malkey nice example but if you want to "color" by $variable like only $x or $y instead of everything?  :)

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Link to comment
Share on other sites

Malkey nice example but if you want to "color" by $variable like only $x or $y instead of everything?  :)

@ Terenz

 An example:-

#include <GUIConstantsEx.au3>

Example()


Func Example()
    Local $hGui, $x, $y, $b[5]
    $hGui = GUICreate("Hover control to change its background", 400, 400, -1, -1, -1)
    $x = GUICtrlCreateLabel("Label x", 10, 10, 50)
    $y = GUICtrlCreateLabel("Label y", 10, 30, 50)
    GUICtrlCreateButton("Button", 10, 60)
    GUICtrlCreateEdit("", 10, 100, 110, 150)
    GUISetState(@SW_SHOW)

    ; Loop until the user exits.
    While 1
        $a = GUIGetCursorInfo($hGui)
        Select
            Case GUIGetMsg() = $GUI_EVENT_CLOSE
                ExitLoop

            Case ($a[4] = $x Or $a[4] = 0) And ($b[4] <> $a[4]) ;  "Or $a[4] = 0" must appear at least once in multiple Cases
                GUICtrlSetBkColor($b[4], 0xFFFFFF) ; White
                $b = $a
                GUICtrlSetBkColor($a[4], 0xFF0000) ; Red in hex colour format 0xRRGGBB

            Case $a[4] = $y And ($b[4] <> $a[4]) ;  "Or $a[4] = 0" not needed (already exists in above Case)
                GUICtrlSetBkColor($b[4], 0xFFFFFF) ; White
                $b = $a
                GUICtrlSetBkColor($a[4], 0x0000FF) ; Blue in hex colour format 0xRRGGBB

        EndSelect
    WEnd
EndFunc   ;==>Example
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...