Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Add ToolTips to controls.


  • Please log in to reply
51 replies to this topic
Superfraggle
  • Members
  • 1019 posts
  • Last active: Sep 25 2011 01:06 AM
  • Joined: 02 Nov 2004
Just a simple add on to allow tooltips to be added to controls without having to monitor the wm_mousemove messages.


; ### Example Code #####
Gui,Add,Button,hwndbutton1 Gbut1,Test Button 1
Gui,Add,Button,hwndbutton2,Test Button 2 
Gui,Add,Button,hwndbutton3,Test Button 3
AddTooltip(button1,"Press me to change my tooltip")
AddTooltip(button2," Another Test Tooltip")
AddTooltip(button3,"A Multiline`nTest Tooltip")
Gui,show,,Test Gui
Return

But1:
AddTooltip(button1,"Wasn't that easy `;)",1)
REturn

GuiClose:
Guiescape:
Exitapp

; ### End Example Code #####

AddToolTip(con,text,Modify = 0){
  Static TThwnd,GuiHwnd
  If (!TThwnd){
    Gui,+LastFound
    GuiHwnd:=WinExist()
    TThwnd:=CreateTooltipControl(GuiHwnd)
  }
  Varsetcapacity(TInfo,44,0)
  Numput(44,TInfo)
  Numput(1|16,TInfo,4)
  Numput(GuiHwnd,TInfo,8)
  Numput(con,TInfo,12)
  Numput(&text,TInfo,36)
  Detecthiddenwindows,on
  If (Modify){
    SendMessage,1036,0,&TInfo,,ahk_id %TThwnd%
  }
  Else {
    Sendmessage,1028,0,&TInfo,,ahk_id %TThwnd%
    SendMessage,1048,0,300,,ahk_id %TThwnd%
  }
  
}

CreateTooltipControl(hwind){
  Ret:=DllCall("CreateWindowEx"
          ,"Uint",0
          ,"Str","TOOLTIPS_CLASS32"
          ,"Uint",0
          ,"Uint",2147483648 | 3
          ,"Uint",-2147483648
          ,"Uint",-2147483648
          ,"Uint",-2147483648
          ,"Uint",-2147483648
          ,"Uint",hwind
          ,"Uint",0
          ,"Uint",0
          ,"Uint",0)
          
  Return Ret
}

You need to send the function the hwnd of the control, and the text you want it to say. If you are changing a tooltip, then specify a 1 for the last parameter.

As always, comments, bugs, questions, suggestions etc.... all welcome.
Steve F AKA Superfraggle

http://r.yuwie.com/superfraggle

Rhys
  • Members
  • 761 posts
  • Last active: Aug 09 2013 04:53 PM
  • Joined: 17 Apr 2007
Groovy.

jballi
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005
Neato Bandito. 8)

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
Nice! Could you put in some comments about the names of the messages and the magic parameters? It would make changing the code easier.

haichen
  • Members
  • 200 posts
  • Last active: Oct 20 2013 01:14 PM
  • Joined: 05 Feb 2007
Yes, its nice and could be pretty helpful.
But at the moment it seems to work only with buttons.
Can the script also work with other controls?

Superfraggle
  • Members
  • 1019 posts
  • Last active: Sep 25 2011 01:06 AM
  • Joined: 02 Nov 2004
So far i've tested on Tabs,Edits,buttons,checkboxes and text (static).

Only text failed so far, and im struggling to find anydocumentation in MSDN to say it doesnt work with any type.
Steve F AKA Superfraggle

http://r.yuwie.com/superfraggle

nouser
  • Guests
  • Last active:
  • Joined: --
Of course it works for static controls. Just add a "null" g-label:
Gui,Add,TEXT,hwndTEXT1 g[color=red]NULL[/color],Testing for static.
...
...

[color=red]NULL[/color]:
Return


haichen
  • Members
  • 200 posts
  • Last active: Oct 20 2013 01:14 PM
  • Joined: 05 Feb 2007
Wonderful!

I tested it with gLabel, but there was a misspelling....sorry.

I've a question to the dllcall of CreateWindowEx. After seaching I found some VBA code for ToolTip. From that I made the following:
CreateTooltipControl(hwind){
  Ret:=DllCall("CreateWindowEx"
          ,"Uint",0
          ,"Str","TOOLTIPS_CLASS32"
          ,"Str",""
          ,"Uint",0|3
          ,"Uint",0
          ,"Uint",0
          ,"Uint",0
          ,"Uint",0
          ,"Uint",hwind
          ,"Uint",0
          ,"Uint",0
          ,"Uint",0)
  Return Ret
}

This also works. My Question: Is there a reason for the other function call?
And as a second: where does the Msg numbers come from? In google I found some code with this numbers, that have to do with slider range and toolbarbuttons.

haichen
  • Members
  • 200 posts
  • Last active: Oct 20 2013 01:14 PM
  • Joined: 05 Feb 2007
When a Tooltip is changed from another Window, the ToolTip is not updated.

Changing
If (Modify){
    SendMessage,1036,0,&TInfo,,ahk_id %TThwnd%
  }
  Else {
    Sendmessage,1028,0,&TInfo,,ahk_id %TThwnd%
    SendMessage,1048,0,300,,ahk_id %TThwnd%
  }

to
If (Modify)
    SendMessage,1036,0,&TInfo,,ahk_id %TThwnd% 
  Else 
  	SendMessage, 1028,0,&TInfo,,ahk_id %TThwnd% 
  SendMessage,1048,0,300,,ahk_id %TThwnd%
helps.

Superfraggle
  • Members
  • 1019 posts
  • Last active: Sep 25 2011 01:06 AM
  • Joined: 02 Nov 2004

This also works. My Question: Is there a reason for the other function call?
And as a second: where does the Msg numbers come from? In google I found some code with this numbers, that have to do with slider range and toolbarbuttons.


The first function call(createwindow) creates a tooltip control, this is required to store/create/show/hide the tool tips, this is only needed once. We then add the tool tips to the control, they can be attached to either controls or areas, at present only controls are supported.

The messages are

TTM_ADDTOOL = 1028 (used to add a tool, and assign it to a control)
TTM_UPDATETIPTEXT = 1036 (used to adjust the text of a tip)
TTM_SETMAXTIPWIDTH = 1048 (This one allows the use of multiline tooltips.)


When a Tooltip is changed from another Window, the ToolTip is not updated.



I am unable to replicate this, they change fine in all my tests. And unless I am missing something I can't see how the changes would fix it.

All you are doing is sending the maxwidth message everytime, instead of only during an add, I'll take a proper look later.
Steve F AKA Superfraggle

http://r.yuwie.com/superfraggle

haichen
  • Members
  • 200 posts
  • Last active: Oct 20 2013 01:14 PM
  • Joined: 05 Feb 2007
In the first part of my question I wanted to know, if you can explain the found
differences like

,"Str","TOOLTIPS_CLASS32"
,"Str",""
,"Uint",0|3
,"Uint",0

,"Str","TOOLTIPS_CLASS32"
,"Uint",0
,"Uint",2147483648 | 3

especially I want to know if my found version is wrong, or if both are right.

I tried a short script to test my update-problem. You're right, it works well.
I have scripted a longer program where I can change the language on the fly over two windows. There it works only with the above changes. So it seems to be a problem with my program. [EDIT] I found the problem and fixed it. I had a try with onmessage before. I deleted this and now your code worked as it should.

Thanks for your answer and this wonderful function!!
:D

Superfraggle
  • Members
  • 1019 posts
  • Last active: Sep 25 2011 01:06 AM
  • Joined: 02 Nov 2004
The number in mine stands for WS_POPUP which is one of the window styles that MSDN has in its example.

As to the str and Uint differences. Officially it should be an str, but as I am sending null I believe this does not matter. (Although I could be mistaken.)
Steve F AKA Superfraggle

http://r.yuwie.com/superfraggle

jballi
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005
I just started to play with this script again after letting sit on the shelf for a few weeks. I created an extended version of the sample script provided by Superfraggle. Hopefully, someone will find it of use. Instructions included in the script:

;-- Note.  To get this script to work, you'll need to add (or include) the
;   AddTooltip and CreateTooltipControl functions from the first post to the
;   bottom of this script.  For the picture example, copy any picture file (jpg,
;   bmp, etc.) to the same directory as this script and name it "picture.bmp".


#NoEnv

gui -MinimizeBox
gui Add,Button,w150 hwndbutton1 gbut1,Test Button 1
AddTooltip(button1,"Button 1: Press me to change my tooltip")


gui Add,Button,y+0 w150 hwndbutton2 gbut2,Test Button 2 
AddTooltip(button2,"Button 2: Press me to turn off the tooltip for this button")


gui Add,Button,y+0 w150 hwndbutton3,Test Button 3
AddTooltip(button3,"A Multiline Test Tooltip`n2nd line`n3rd line`n4th line")


gui Add,Checkbox,w150 hwndCheckbox_hWnd,Checkbox Control
AddTooltip(Checkbox_hWnd,"Tooltip for the Checkbox control")


gui Add,Radio,w150 hwndRadio_hWnd,Radio Control
AddTooltip(Radio_hWnd,"Tooltip for the Radio control")


gui Add,Edit,w150 hwndEdit_hWnd,Edit Control
AddTooltip(Edit_hWnd,"Tooltip for the Edit control")


gui Add,Text,w150 hwndText_hWnd gNull,Text Control
Tip=
   (ltrim join`s
    Tooltip for the Text control.  Note that the Tooltip for a Text control does
    not show unless a g-label for the control is defined.
   )
AddTooltip(Text_hWnd,Tip)


gui Add,Picture,w150 h100 hwndPicture_hWnd gnull,Picture.bmp
Tip=
   (ltrim join`s
    Tooltip for the Picture control.  Note that the Tooltip for a Picture
    control does not show unless a g-label for the control is defined.   
   )
AddTooltip(Picture_hWnd,Tip)


gui Add,DropDownList,w150 r3 hwndDropDownList_hWnd,DropDownList Control||2|3
AddTooltip(DropDownList_hWnd,"Tooltip for the DropDownList control")


gui Add,ComboBox,w150 r3 hwndComboBox_hWnd,ComboBox Control||2|3
    ;-- A ComboBox is actually two controls:  An Edit control and a Drop-down
    ;   button. Note that handle returned for this control is for the
    ;   drop-down button, not for the Edit control.
Tip=
   (ltrim join`s
    Tooltip for the drop-down button piece of the ComboBox control.  Note that
    this tip is different than the Edit piece of the control.
   )
AddTooltip(ComboBox_hWnd,Tip)

gui Add,ListBox,w150 r3 hwndListBox_hWnd,ListBox Control||2|3
AddTooltip(ListBox_hWnd,"Tooltip for the ListBox control")

gui Add,ListView,w150 h50 hwndListView_hWnd,ListView Control
Tip=
   (ltrim join`s
    Tooltip for the ListView control.  Note that this tip is different than
    the header piece of the control.
   )
AddTooltip(ListView_hWnd,Tip)

gui Add,DateTime,w150 hwndDateTime_hWnd  ;,DateTime Control
AddTooltip(DateTime_hWnd,"Tooltip for the DateTime control")


;-- Uncomment this if you want to see the MonthCal control.  Works the same as DateTime
;;;;;gui Add,MonthCal,w150 hwndMonthCal_hWnd  ;,MonthCal Control
;;;;;AddTooltip(MonthCal_hWnd,"Tooltip for the MonthCal control")


gui Add,Progress,w150 hwndProgress_hWnd,65  ;Progress Control
AddTooltip(Progress_hWnd,"Tooltip for the Progress control")


gui Add,Slider,w150 hwndSlider_hWnd,45  ;Slider Control
AddTooltip(Slider_hWnd,"Tooltip for the Slider control")


gui Add,Hotkey,w150 hwndHotkey_hWnd,45  ;Hotkey Control
AddTooltip(Hotkey_hWnd,"Tooltip for the Hotkey control")


gui Add,Edit,w150 h20 hwndEdit2_hWnd,Dummy Edit Control  ;-- Used by UpDown control
Tip=
   (ltrim join`s
    This Edit control was created so that the attached UpDown control could be
    demonstrated.
   )
AddTooltip(Edit2_hWnd,Tip)


gui Add,UpDown,hwndUpDown_hWnd Range 1-10,5
AddTooltip(UpDown_hWnd,"Tooltip for the UpDown control")

gui Show,,Tooltip Test


;-- Get hWnd to the Edit control piece of the ComboBox
ControlGet ListViewHeader_hWnd,hWnd,,SysHeader321,Tooltip Test
Tip=
   (ltrim join`s
    Tooltip for the header of the ListView control.  Note that this tip is
    different than the rest of the ListView control.
   )
AddTooltip(ListViewHeader_hWnd,Tip)


;-- Get hWnd to the Edit control piece of the ComboBox
ControlGet EditComboBox_hWnd,hWnd,,Edit2,Tooltip Test
Tip=
   (ltrim join`s
    Tooltip for the Edit piece of the ComboBox control.  Note that this tip is
    different than the drop-down button piece of this control.
   )
AddTooltip(EditComboBox_hWnd,Tip)

return


But1:
AddTooltip(button1,"Wasn't that easy `;)",true)
return

But2:
AddTooltip(button2,"",true)
return


Null:
return

GUIClose:
GUIescape:
ExitApp



;-- Copy the AddTooltip and CreateTooltipControl functions from the 1st post
;   here.  The original post can be found here:
;   http://www.autohotkey.com/forum/viewtopic.php?t=30300



jballi
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005
Found one tiny bug. The function indiscriminately sets DetectHiddenWindows to On. The function needs to set DetectHiddenWindows to what it was before returning. Something like:

[color=red] l_DetectHiddenWindows:=A_DetectHiddenWindows[/color]
Detecthiddenwindows,on
If (Modify){
  SendMessage,1036,0,&TInfo,,ahk_id %TThwnd%
}
Else {
  Sendmessage,1028,0,&TInfo,,ahk_id %TThwnd%
  SendMessage,1048,0,300,,ahk_id %TThwnd%
}
[color=red]DetectHiddenWindows %l_DetectHiddenWindows%[/color]
I hope this helps.

HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008
Check out new ToolTip() Options, many thanks Superfraggle ;)

ToolTip() - Advanced ToolTip Features