More cohesive balloon tooltip usage
Posted: Sun Jun 17, 2007 8:20 am
Code: Select all
;============================================================================================================================
; In an effort to understand all the various balloon tool tip codes and ways of using them, I have evolved the current
; procedures for myself to make them as useful as possible. There are an awful lot of people who have posted this sort
; of code in the forums and it's impossible to know who invented it first, did it better but they all did it free and it
; makes our code look better!!!
;
; Xombie, _Paul_, Srod, Sparkie, NetMaestro and loads of others
;
; Note, this tool tip type allows you to use "Chr(13) + Chr(10)" to manually format your tips to multiple lines.
;
; Hope that someone has a use for this.
;
;============================================================================================================================
; Create a structure used to track our balloon tool tip elements used by the various procedures
;============================================================================================================================
Structure tooltipdata ; If any tooltips setup, keep track of gadgets that have them so you can remove them if needed
window.l
gadget.l
handle.l
EndStructure
;============================================================================================================================
; Create the linked list needed to keep track of all the different tool tips based on the structure above
;============================================================================================================================
Global NewList tooltips.tooltipdata() ; All tool tip handles and data here
;============================================================================================================================
; Procedural declarations in case you store these all over the place
;============================================================================================================================
Declare MyBalloonToolTips(btWindow.l, btGadget.l, btText.s)
Declare.l RemoveToolTip(btGadget.l)
Declare.l RemoveToolTipW(TtWindow.l)
Declare.l ChangeToolTip(ttGadget.l, btText.s)
;============================================================================================================================
; The routine below allows you to create a colourful balloon tool tip for every gadget in your system. You can assign each
; and every gadget its' own tooltip and keep track of them in a linked list
;
; Command: MyBalloonToolTips(#TheWindowConstant, #TheGadgetConstant, TheToolTipText)
;
; When you send this command to the procedure:
;
; 1. It first checks to see if the gadget currently exists and exits if it doesn't.
; 2. Checks to see if the gadget already has a tool tip entry and exits the procedure if it does,
; 3. Creates the tool tip entry for the gadget
; 4. Add the details to the linked list for this tool tip entry (Window, Constant, Tool tip text)
;
;============================================================================================================================
Procedure MyBalloonToolTips(btWindow.l, btGadget.l, btText.s)
If IsGadget(btGadget.l) ; Does the gadget actually exist at this time?
ForEach tooltips() ; Exit this routine if tool tip for control exists
If tooltips()\window = btWindow And tooltips()\gadget = btGadget
ProcedureReturn
EndIf
Next
ToolTipControl = CreateWindowEx_(0, "ToolTips_Class32", "", #WS_POPUP | #TTS_NOPREFIX | #TTS_BALLOON, 0, 0, 0, 0, WindowID(btWindow), 0, GetModuleHandle_(0), 0)
SetWindowPos_(ToolTipControl,#HWND_TOPMOST, 0, 0, 0, 0, #SWP_NOMOVE | #SWP_NOSIZE)
SendMessage_(ToolTipControl, #TTM_SETTIPTEXTCOLOR, 0, 0)
SendMessage_(ToolTipControl, #TTM_SETTIPBKCOLOR, $F58C0A, 0) ; Tool tip background colour used
SendMessage_(ToolTipControl, #TTM_SETMAXTIPWIDTH, 0, 180)
Button.TOOLINFO\cbSize = SizeOf(TOOLINFO)
Button\uFlags = #TTF_IDISHWND | #TTF_SUBCLASS
Button\hwnd = WindowID(btWindow)
Button\uID = GadgetID(btGadget)
Button\hInst = 0
Button\lpszText = @btText
SendMessage_(ToolTipControl, #TTM_ADDTOOL, 0, Button)
SendMessage_(ToolTipControl, #TTM_UPDATE, 0, 0)
AddElement(tooltips()) ; Add the newly created tool tip details to the linked list
tooltips()\window = btWindow
tooltips()\gadget = btGadget
tooltips()\handle = ToolTipControl
EndIf
EndProcedure
;============================================================================================================================
; The routine below allows you to remove a previously set tool tip from a particular gadget.
;
; Command: RemoveToolTip(#TheGadgetConstant)
;
; Before the gadget tool tip is removed from the linked list, it is checked for validity to avoid crashes or illegal memory
; errors.
;
;============================================================================================================================
Procedure.l RemoveToolTip(btGadget.l)
ForEach tooltips() ; Remove each tool tip that exists for a control
If tooltips()\gadget = btGadget.l And IsGadget(tooltips()\gadget) ; Is there a stored entry and does the gadget exist?
btHandle.l = tooltips()\handle
btWindow.l = tooltips()\window
DeleteElement(tooltips()) ; Delete the entry from the linked list
Button.TOOLINFO\cbSize = SizeOf(TOOLINFO)
Button\hwnd = WindowID(btWindow)
Button\uID = GadgetID(btGadget)
SendMessage_(btHandle, #TTM_DELTOOL, 0, Button) ; Delete the tool tip from the gadget
Break
EndIf
Next
EndProcedure
;============================================================================================================================
; The routine below allows you to remove all tool tips for all gadgets from a specified window without having to specify
; each gadget individually.
;
; Command: RemoveToolTipW(#TheWindowConstant)
;
; 1. The linked list is searched for all matching gadgets for the supplied window.
; 2. The window is checked for validity (In case you used this command in the wrong place and the window didn't exist!
; 3. The gadget itself is checked for validity in case you removed it somewhere else.
;
;============================================================================================================================
Procedure.l RemoveToolTipW(TtWindow.l)
ForEach tooltips() ; Check each tooltip entry
If tooltips()\window = TtWindow.l And IsWindow(TtWindow.l) ; If there is an entry in the list for this window
btHandle.l = tooltips()\handle ; Get the current tooltip handle
btWindow.l = tooltips()\window ; Get the current window handle
btGadget.l = tooltips()\gadget ; Get the current gadget handle
If IsGadget(btGadget.l) ; Is the gadget valid and active?
DeleteElement(tooltips()) ; Delete the current element from the list
Button.TOOLINFO\cbSize = SizeOf(TOOLINFO) ; Get tooltip info size
Button\hwnd = WindowID(btWindow) ; Get the windows id for this element
Button\uID = GadgetID(btGadget) ; Get the gadget id for this element
SendMessage_(btHandle, #TTM_DELTOOL, 0, Button) ; Delete the tooltip for this window from the list and gadget
EndIf
EndIf ; End conditional test
Next ; Next iteration
EndProcedure
;============================================================================================================================
; The routine below allows you to change the tool tip text for a specific gadget
;
; Command: ChangeToolTip(#TheGadgetConstant, TheToolTipText)
;
; If a matching gadget is found in the linked list and it is valid, the tool tip change is sent to it
;
;============================================================================================================================
Procedure.l ChangeToolTip(ttGadget.l, btText.s)
ForEach tooltips() ; Check each tooltip entry
If tooltips()\gadget = ttGadget.l And IsGadget(ttGadget.l) ; If there is an entry in the list for this window
btHandle.l = tooltips()\handle ; Get the current tooltip handle
btWindow.l = tooltips()\window ; Get the current window handle
btGadget.l = tooltips()\gadget ; Get the current gadget handle
ttChange.TOOLINFO\cbSize = SizeOf(TOOLINFO)
ttChange\hwnd = WindowID(btWindow.l)
ttChange\uId = GadgetID(btGadget.)
ttChange\lpszText = @hText.s
SendMessage_(btHandle.l, #TTM_UPDATETIPTEXT, 0, @btText.s)
EndIf
Next tooltips()
EndProcedure