Jump to content
pixelsearch

Listview : Tooltip while hovering

Recommended Posts

Hi everybody :)
Here is a simple script that allows to display a tooltip while hovering the mouse over a listview cell.

The tooltip disappears as soon as the cell isn't hovered over anymore. The 2 eventual listview scrollbars and the listview headers area are correctly excluded from the hovering process.

The GUI is resizable and maximizable and the ListView is a native-created one (only because it's a bit simpler to auto-resize the listview while resizing the GUI) but the basis of this script would work too on a UDF-created listview

Both hovered cells "Mary 10" and "Mary 30" display a tooltip in the script below :

#include <GuiListView.au3>
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>

$hGUI = GUICreate("Hover over 'Mary 10' or 'Mary 30'", 420, 400, -1, -1, _
    BitOR($GUI_SS_DEFAULT_GUI, $WS_SIZEBOX, $WS_MAXIMIZEBOX))

$idListView = GUICtrlCreateListView("Peter|Paul|Mary", 60, 50, 300, 300)
GUICtrlSetResizing(-1, $GUI_DOCKAUTO)

$hListView = GuiCtrlGetHandle($idListView)
$iHeaderHeight = _WinAPI_GetWindowHeight(_GUICtrlListView_GetHeader($hListView))

For $iRow = 0 To 39
    $sRow = StringFormat("%2s", $iRow)
    GUICtrlCreateListViewItem( _
        "Peter " & $sRow & " |" & _
        "Paul  " & $sRow & " |" & _
        "Mary  " & $sRow & (($iRow = 10 Or $iRow = 30) ? " <" : ""), $idListView)
Next

For $iCol = 0 To 2
    _GUICtrlListView_SetColumnWidth($hListview, $iCol, 100)
Next

GUISetState()

$bToolTip = False ; False = no tooltip actually displayed, True = tooltip displayed

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            GUIDelete($hGUI)
            Exit

        Case $GUI_EVENT_MOUSEMOVE
            Local $aMPos = MouseGetPos()
            Local $aLVPos = WinGetPos($hListView)
            Local $aLVCli = WinGetClientSize($hListView)

            If $aMPos[0] <= $aLVPos[0] OR $aMPos[0] > $aLVPos[0] + $aLVCli[0] + 1 _
                OR $aMPos[1] <= $aLVPos[1] + $iHeaderHeight OR $aMPos[1] > $aLVPos[1] + $aLVCli[1] + 1 Then
                If $bToolTip Then
                    ToolTip("")
                    $bToolTip = False
                EndIf
                ContinueLoop
            EndIf

            Local $aHit = _GUICtrlListView_SubItemHitTest($hListView)
            If ($aHit[0] = 10 Or $aHit[0] = 30) And $aHit[1] = 2 Then ; item 10 or 30 / subitem 2
                ToolTip("Infos " & _GUICtrlListView_GetItemText($hListview, $aHit[0], $aHit[1]) & @lf & _
                    "Topmost visible item : " & _GUICtrlListView_GetTopIndex($hListview) & @lf & _
                    "Time " & @HOUR & ":" & @MIN & ":" & @SEC)
                $bToolTip = True
            Else
                If $bToolTip Then
                    ToolTip("")
                    $bToolTip = False
                EndIf
            EndIf
    EndSwitch
WEnd

For the record, it all started with Siwa's request in this link (Melba23's thread) and after several tries, here is the final result, which could be useful to our Forum members.

Edited by pixelsearch
fixed 1 pixel gap, so tooltip disappears more accurately

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...