Jump to content

Tooltip for GUI Menu control


Saad
 Share

Go to solution Solved by Danyfirex,

Recommended Posts

I'm writing an application where I'd like to add a tooltip to a GUI Menu control.

In the below code, I'd like to display $sBookmarkTT when I hover over $idBookmark1 "Bookmark1".

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 625, 442, 370, 232)
$idFile = GUICtrlCreateMenu("&File")
$idFileExit = GUICtrlCreateMenuItem("&Exit", $idFile)
$idBookmarks = GUICtrlCreateMenu("&Bookmarks")
$idBookmark1 = GUICtrlCreateMenuItem("Bookmark1", $idBookmarks)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

$sBookmarkTT = "This is a tooltip for bookmark1"

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

Thanks.

Link to comment
Share on other sites

  • Solution

You can do this:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiMenu.au3>
#include <GuiToolTip.au3>
#Region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("Form1", 625, 442, 370, 232)
Global $idFile = GUICtrlCreateMenu("&File")
Global $idFileExit = GUICtrlCreateMenuItem("&Exit", $idFile)
Global $idBookmarks = GUICtrlCreateMenu("&Bookmarks")
Global $idBookmark1 = GUICtrlCreateMenuItem("Bookmark1", $idBookmarks)
GUISetState(@SW_SHOW)
GUIRegisterMsg($WM_MENUSELECT, "WM_MENUSELECT")
#EndRegion ### END Koda GUI section ###
Global $g_sBookmarkTT = "This is a tooltip for bookmark1"
Global $g_hMain = _GUICtrlMenu_GetMenu($Form1)
Global $g_hBookmark1 = _GUICtrlMenu_GetItemSubMenu($g_hMain, 1)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func WM_MENUSELECT($hWnd, $iMsg, $wParam, $lParam)
    Local $iIndex = BitAND($wParam, 0xFFFF)
    Local $iFlag = BitShift($wParam, 16)
    If BitAND($iFlag, $MF_MOUSESELECT) Then
        ToolTip("")
        If $lParam = $g_hBookmark1 Then
            ToolTip($g_sBookmarkTT, Default, Default, "", 0, 1)
        EndIf
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_MENUSELECT

Saludos

Edited by Danyfirex
Link to comment
Share on other sites

@Danyfirex very nice, with WM_MENUSELECT and MF_MOUSESELECT, I never used them until now :)

I have 2 questions concerning your script. Please look at the 1st minor display issue :

1071532169_Tooltipinmenu1.png.8bafc06df18b062562e7f95cffcac2ed.png

This happens for example when you click on the Bookmarks menu, then hover over the Bookmark1 item (the tooltip appears), then press Escape (or left key for example) and the tooltip doesn't disappear at once (as it should). I'm aware OP didn't mention the keyboard to navigate but well, if we can prevent bad displays, let's do it :)

A 2nd issue will appear as soon as there are more than 1 item in the Bookmarks menu (which is a frequent case) for example by adding this line to your script :

Global $idBookmark2 = GUICtrlCreateMenuItem("Bookmark2", $idBookmarks)

936977610_Tooltipinmenu2.png.06ef27aef3aff4b1e656f1397580da90.png

To solve both issues, I tried this :
* Replacing MF_MOUSESELECT with MF_HILITE, it seems to solve issue #1
* Using $iIndex to display the Tooltip only for the Bookmark1 item, it seems to solve issue #2

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiMenu.au3>
#include <GuiToolTip.au3>
#Region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("Form1", 625, 442, 370, 232)
Global $idFile = GUICtrlCreateMenu("&File")
Global $idFileExit = GUICtrlCreateMenuItem("&Exit", $idFile)
Global $idBookmarks = GUICtrlCreateMenu("&Bookmarks")
Global $idBookmark1 = GUICtrlCreateMenuItem("Bookmark1", $idBookmarks)

Global $idBookmark2 = GUICtrlCreateMenuItem("Bookmark2", $idBookmarks)

GUISetState(@SW_SHOW)
GUIRegisterMsg($WM_MENUSELECT, "WM_MENUSELECT")
#EndRegion ### END Koda GUI section ###
Global $g_sBookmarkTT = "This is a tooltip for bookmark1"
Global $g_hMain = _GUICtrlMenu_GetMenu($Form1)
Global $g_hBookmark1 = _GUICtrlMenu_GetItemSubMenu($g_hMain, 1)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func WM_MENUSELECT($hWnd, $iMsg, $wParam, $lParam)
    Local $iIndex = BitAND($wParam, 0xFFFF)
    ConsoleWrite($iIndex & @crlf)
    Local $iFlag = BitShift($wParam, 16)

    ; If BitAND($iFlag, $MF_MOUSESELECT) Then
    If BitAND($iFlag, $MF_HILITE) Then
        ToolTip("")

        ; If $lParam = $g_hBookmark1 Then
        If $lParam = $g_hBookmark1 And $iIndex = 6 Then

            ToolTip($g_sBookmarkTT, Default, Default, "", 0, 1)
        EndIf
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_MENUSELECT

The problem is I added a ConsoleWrite in the script to know that the Bookmark1 item has an index of 6. Maybe there's another simple way to know if the highlited item is Bookmark1 (the one associated with the Tooltip) without using a constant 6 in the script ?

Link to comment
Share on other sites

I just did it fast, never mind about other things like keyboard etc.

thank you for your solution. 😉

 

Saludos

Link to comment
Share on other sites

@pixelsearch I think you will find that your constant 6 is also the ID returned by GUICtrlCreateMenuItem.

So, it should be as simple as:  $iIndex = $idBookmark1

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiMenu.au3>
#include <GuiToolTip.au3>
#Region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("Form1", 625, 442, 370, 232)
Global $idFile = GUICtrlCreateMenu("&File")
Global $idFileExit = GUICtrlCreateMenuItem("&Exit", $idFile)
Global $idBookmarks = GUICtrlCreateMenu("&Bookmarks")
Global $idBookmark1 = GUICtrlCreateMenuItem("Bookmark1", $idBookmarks)
Global $idBookmark2 = GUICtrlCreateMenuItem("Bookmark2", $idBookmarks)

GUISetState(@SW_SHOW)
GUIRegisterMsg($WM_MENUSELECT, "WM_MENUSELECT")
#EndRegion ### END Koda GUI section ###
Global $g_sBookmarkTT = "This is a tooltip for bookmark1"
Global $g_hMain = _GUICtrlMenu_GetMenu($Form1)
Global $g_hBookmark1 = _GUICtrlMenu_GetItemSubMenu($g_hMain, 1)
ConsoleWrite($idBookmark1 & @CRLF)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func WM_MENUSELECT($hWnd, $iMsg, $wParam, $lParam)
    Local $iIndex = BitAND($wParam, 0xFFFF)
    ConsoleWrite($iIndex & @crlf)
    Local $iFlag = BitShift($wParam, 16)

    ; If BitAND($iFlag, $MF_MOUSESELECT) Then
    If BitAND($iFlag, $MF_HILITE) Then
        ToolTip("")

        ; If $lParam = $g_hBookmark1 Then
        If $lParam = $g_hBookmark1 And $iIndex = $idBookmark1 Then

            ToolTip($g_sBookmarkTT, Default, Default, "", 0, 1)
        EndIf
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_MENUSELECT

 

Edited by kurtykurtyboy
Link to comment
Share on other sites

@kurtykurtyboy you're right, I should have spent a bit more time on this.
IIRC, controls id's always start from 3, for example if we add this line :

ConsoleWrite($idFile & "   " & $idFileExit & "   " & $idBookmarks & "   " & $idBookmark1 & "   " & $idBookmark2 & @CRLF)

Console display :

3   4   5   6   7

Now always the same question : where are controls id's 1 & 2 in AutoIt ?
I remember having found by accident a possible control id = 1 corresponding to :

GUICtrlCreateTabItem("")

Then I asked LarsJ if it was correct. His answer was :

I didn't know that controlID #1 is used by GUICtrlCreateTabItem(""). I also don't know what controlID #2 is used for.

If we work on the help file example of GUICtrlCreateTabItem, we can display in the Console this controlID = 1

#include <GUIConstantsEx.au3>

Example()

Func Example()
        GUICreate("My GUI Tab", 250, 150); will create a dialog box that when displayed is centered

        GUISetBkColor(0x00E0FFFF)
        GUISetFont(9, 300)

        Local $idTab = GUICtrlCreateTab(10, 10, 200, 100)

        ; GUICtrlCreateTabItem("tab0")
        Local $idTabItem = GUICtrlCreateTabItem("tab0")

        GUICtrlCreateLabel("label0", 30, 80, 50, 20)
        GUICtrlCreateButton("OK0", 20, 50, 50, 20)
        GUICtrlCreateInput("default", 80, 50, 70, 20)

        GUICtrlCreateTabItem("tab----1")
        GUICtrlCreateLabel("label1", 30, 80, 50, 20)
        GUICtrlCreateCombo("", 20, 50, 60, 120)
        GUICtrlSetData(-1, "Trids|CyberSlug|Larry|Jon|Tylo|guinness", "Jon"); default Jon
        GUICtrlCreateButton("OK1", 80, 50, 50, 20)

        GUICtrlCreateTabItem("tab2")
        GUICtrlSetState(-1, $GUI_SHOW); will be display first
        GUICtrlCreateLabel("label2", 30, 80, 50, 20)
        GUICtrlCreateButton("OK2", 140, 50, 50)

        ; GUICtrlCreateTabItem(""); end tabitem definition
        Local $idTabItemEND = GUICtrlCreateTabItem(""); end tabitem definition
        ConsoleWrite($idTab & "   " & $idTabItem & "   " & $idTabItemEND & @crlf)

        GUICtrlCreateLabel("Click on tab and see the title", 20, 130, 250, 20)

        GUISetState(@SW_SHOW)

        Local $idMsg
        ; Loop until the user exits.
        While 1
                $idMsg = GUIGetMsg()

                If $idMsg = $GUI_EVENT_CLOSE Then ExitLoop
                If $idMsg = $idTab Then
                        ; display the clicked tab
                        WinSetTitle("My GUI Tab", "", "My GUI Tab" & GUICtrlRead($idTab))
                EndIf
        WEnd
EndFunc   ;==>Example

Console :

3   4   1

If this is correct, maybe we should ask Jon one of these days what is controlID #2 in AutoIt ?

Link to comment
Share on other sites

@pixelsearch

Quote

Now always the same question : where are controls id's 1 & 2 in AutoIt ?

 

id 1: is a hidden edit control AutoIt creates over an overlapped hidden window named "AutoIt v3"

id 2: It seems to be when we do GUICreate it increments the id count so when we create our first control we get it assigned with id 3.

 

Saludos

 

Link to comment
Share on other sites

  • Administrators

From the source:

// Limits
#define AUT_GUI_FIRSTCONTROL        3           // First usable control ID (1 (IDOK) and 2 (IDCANCEL) is used by the OS and 0 is not used)
#define AUT_GUI_MAXCONTROLID        65535       // Maximum control ID - limited to WORD size (unsigned short)

Obviously caused a clash in an early build. Lots of 1 and 2 used by the OS so we just picked the first free one.

 

Link to comment
Share on other sites

  • Administrators

From winuser.h

/*
 * Dialog Box Command IDs
 */
#define IDOK                1
#define IDCANCEL            2
#define IDABORT             3
#define IDRETRY             4
#define IDIGNORE            5
#define IDYES               6
#define IDNO                7
#if(WINVER >= 0x0400)
#define IDCLOSE         8
#define IDHELP          9
#endif /* WINVER >= 0x0400 */

Windows button controls for OK and Cancel are usually 1 and 2 and IIRC mapped to Enter (Default) and Esc (Cancel) - the rest of them don't come into play in a custom dialog.  We could have just picked 100 to be honest and made it all the more cryptic :)

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