TrackBarGadget(): Show tooltip of current value?

Just starting out? Need help? Post your questions and find answers here.
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: TrackBarGadget(): Show tooltip of current value?

Post by c4s »

Thanks again and again Rashad. However, it's more than I wanted: I don't need a preview of the value the mouse is over. I just want the trackbar-tooltip to appear (on the regular track position) when the mouse is over the gadget...
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: TrackBarGadget(): Show tooltip of current value?

Post by RASHAD »

@c4s, I wish the next code will fulfill your request
And Happy New Year (I hope I am not too late)

Code: Select all

Enumeration
   #Window
   #Text
   #Trackbar
EndEnumeration

#TBM_SETTOOLTIPS = #WM_USER + 29
#TTF_TRACK = $20


Define TooltipID, ti.TOOLINFO
Define Text.s

Procedure CursorOverGadget(WindowNr, GadgetNr)
   Protected CursorX, CursorY
   Protected X, Y, Width, Height
   Protected Result = #False

   If IsWindow(WindowNr)
      CursorX = WindowMouseX(WindowNr)
      CursorY = WindowMouseY(WindowNr)
   EndIf

   If IsGadget(GadgetNr)
      X = GadgetX(GadgetNr)
      Y = GadgetY(GadgetNr)
      Width = GadgetWidth(GadgetNr)
      Height = GadgetHeight(GadgetNr)
   EndIf


   If CursorX >= X And CursorX <= X + Width
      If CursorY >= Y And CursorY <= Y + Height
         Result = #True
      EndIf
   EndIf

   ProcedureReturn Result
EndProcedure


If OpenWindow(#Window, 0, 0, 200, 70, "TrackBar Tooltip", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
   TextGadget(#Text, 10, 10, 180, 20, "Range from -5.0% to +5.0%")
   TrackBarGadget(#Trackbar, 10, 40, 180, 20, 0, 100)

   TooltipID = CreateWindowEx_(0, #TOOLTIPS_CLASS, "", #TTS_NOPREFIX, 0, 0, 0, 0, WindowID(#Window), 0, GetModuleHandle_(0), 0)
   ti\cbSize = SizeOf(TOOLINFO)
   ti\hWnd = WindowID(#Window)
   ti\uFlags = #TTF_IDISHWND | #TTF_TRACK | #TTF_CENTERTIP
   ti\uId = GadgetID(#Trackbar)
   SendMessage_(GadgetID(#Trackbar), #TBM_SETTOOLTIPS, TooltipID, 0)

   Repeat
      Select WaitWindowEvent()
          
         Case #WM_MOUSEMOVE
            GetWindowRect_(GadgetID(#Trackbar),r.RECT)
            GetCursorPos_(@p.POINT)
               Text = StrF((GetGadgetState(#Trackbar) - 50) / 10.0, 1) + "%"
               ti\lpszText = @Text
            If CursorOverGadget(#Window, #Trackbar) = #True
               SendMessage_(TooltipID, #TTM_UPDATETIPTEXT, 0, @ti)
               SendMessage_(TooltipID, #TTM_TRACKACTIVATE, #True, @ti)
               SendMessage_(TooltipID, #TTM_ADDTOOL, 0, @ti)              
               SendMessage_(TooltipID, #TTM_TRACKPOSITION, 0,r\top << 20 + (GetGadgetState(#Trackbar)*(r\right-r\left)/100) + r\left)
               SendMessage_(TooltipID, #TTM_TRACKACTIVATE, #False, @ti)
               Delay(25)
            EndIf

         Case #PB_Event_CloseWindow
            Break
      EndSelect
   ForEver
EndIf

Edit:Updated
Last edited by RASHAD on Thu Dec 30, 2010 9:17 am, edited 1 time in total.
Egypt my love
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: TrackBarGadget(): Show tooltip of current value?

Post by c4s »

That's something I can work with. It flickers a little (on Windows XP) but it works as intended. Great!

I wish you a Happy New Year as well, Rashad. (you aren't too late, still about 48 hours here ;))
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: TrackBarGadget(): Show tooltip of current value?

Post by RASHAD »

Last post updated
1- Fixed one bug with PB x64
2- The flicker is minimized
3- The code is optimized a little bit
Egypt my love
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: TrackBarGadget(): Show tooltip of current value?

Post by c4s »

A little inspired by your code I finally came up with the following solution and I think it's pretty good. The idea behind is to reuse the existing tooltip, see for yourself:

Code: Select all

Enumeration
	#Window
	#Text
	#Trackbar
EndEnumeration

#TBM_SETTOOLTIPS = #WM_USER + 29
#TTF_TRACK = $20


Define TooltipID, ti.TOOLINFO, TooltipTrackActive
Define Text.s

Procedure CursorOverGadget(WindowNr, GadgetNr)
	Protected CursorX, CursorY
	Protected X, Y, Width, Height
	Protected Result = #False

	If IsWindow(WindowNr)
		CursorX = WindowMouseX(WindowNr)
		CursorY = WindowMouseY(WindowNr)
	EndIf

	If IsGadget(GadgetNr)
		X = GadgetX(GadgetNr)
		Y = GadgetY(GadgetNr)
		Width = GadgetWidth(GadgetNr)
		Height = GadgetHeight(GadgetNr)
	EndIf


	If CursorX >= X And CursorX <= X + Width
		If CursorY >= Y And CursorY <= Y + Height
			Result = #True
		EndIf
	EndIf

	ProcedureReturn Result
EndProcedure


If OpenWindow(#Window, 0, 0, 200, 70, "TrackBar Tooltip", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
	TextGadget(#Text, 10, 10, 180, 20, "Range from -5.0% to +5.0%")
	TrackBarGadget(#Trackbar, 10, 40, 180, 20, 0, 100)

	TooltipID = CreateWindowEx_(0, #TOOLTIPS_CLASS, "", #TTS_NOPREFIX, 0, 0, 0, 0, WindowID(#Window), 0, GetModuleHandle_(0), 0)
	ti\cbSize = SizeOf(TOOLINFO)
	ti\hWnd = WindowID(#Window)
	ti\uFlags = #TTF_IDISHWND | #TTF_TRACK | #TTF_CENTERTIP
	ti\uId = GadgetID(#Trackbar)
	SendMessage_(GadgetID(#Trackbar), #TBM_SETTOOLTIPS, TooltipID, 0)
	SendMessage_(TooltipID, #TTM_ADDTOOL, 0, @ti)

	Repeat
		Select WaitWindowEvent()
			Case #PB_Event_Gadget
				If EventGadget() = #Trackbar
					Text = StrF((GetGadgetState(#Trackbar) - 50) / 10.0, 1) + "%"
					ti\lpszText = @Text
					SendMessage_(TooltipID, #TTM_UPDATETIPTEXT, 0, @ti)
					SendMessage_(TooltipID, #TTM_TRACKACTIVATE, #True, @ti)
				EndIf

			Case #WM_LBUTTONDOWN
				If CursorOverGadget(#Window, #Trackbar) = #True
					TooltipTrackActive = #True
					SendMessage_(TooltipID, #TTM_TRACKACTIVATE, #False, @ti)
				EndIf

			Case #WM_LBUTTONUP
				TooltipTrackActive = #False
				SendMessage_(TooltipID, #TTM_TRACKACTIVATE, #True, @ti)

			Case #WM_MOUSEMOVE
				If TooltipTrackActive = #False
					If CursorOverGadget(#Window, #Trackbar) = #True
						SendMessage_(TooltipID, #TTM_TRACKACTIVATE, #True, @ti)
						GetWindowRect_(GadgetID(#Trackbar), r.RECT) : GetCursorPos_(@p.POINT)
						SendMessage_(TooltipID, #TTM_TRACKPOSITION, 0, r\top << 20 + (GetGadgetState(#Trackbar) * (r\right - r\left) / 100) + r\left)
					Else
						SendMessage_(TooltipID, #TTM_TRACKACTIVATE, #False, @ti)
					EndIf
				EndIf

			Case #PB_Event_CloseWindow
				Break
		EndSelect
	ForEver
EndIf
You said that you "Fixed one bug with PB x64". I don't have your previous code so I can't see what you've changed (and I don't have x64 to see for myself). So is my code "fixed" as well?
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: TrackBarGadget(): Show tooltip of current value?

Post by RASHAD »

Great ,just great :)
The snippet works fine with PB x86,PB x64
No flickers at all
Except one small annoying behavior (May be for me only )
At start when you move the mouse to the gadget area the tool tip does not show up until
you press the mouse left button
But still in the best stage ,so leave it as it is
Egypt my love
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: TrackBarGadget(): Show tooltip of current value?

Post by c4s »

I updated the code because the tooltip was a little shifted and didn't show up at the beginning:

Code: Select all

Enumeration
	#Window
	#Text
	#Trackbar
EndEnumeration

#TBM_SETTOOLTIPS = #WM_USER + 29
#TTF_TRACK = $20


Define TooltipID, ti.TOOLINFO, TooltipTrackActive, r.RECT, r2.RECT
Define Text.s

Procedure CursorOverGadget(WindowNr, GadgetNr)
	Protected CursorX, CursorY
	Protected X, Y, Width, Height
	Protected Result = #False

	If IsWindow(WindowNr)
		CursorX = WindowMouseX(WindowNr)
		CursorY = WindowMouseY(WindowNr)
	EndIf

	If IsGadget(GadgetNr)
		X = GadgetX(GadgetNr)
		Y = GadgetY(GadgetNr)
		Width = GadgetWidth(GadgetNr)
		Height = GadgetHeight(GadgetNr)
	EndIf


	If CursorX >= X And CursorX <= X + Width
		If CursorY >= Y And CursorY <= Y + Height
			Result = #True
		EndIf
	EndIf

	ProcedureReturn Result
EndProcedure

Macro MakeLong(LoWord, HiWord)
	(((LoWord) & $FFFF) + (HiWord) << 16)
EndMacro


If OpenWindow(#Window, 0, 0, 200, 70, "TrackBar Tooltip", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
	TextGadget(#Text, 10, 10, 180, 20, "Range from -5.0% to +5.0%")
	TrackBarGadget(#Trackbar, 10, 40, 180, 20, 0, 100)
	 SetGadgetState(#Trackbar, 50)  ; Initiate with "0.0%"

	TooltipID = CreateWindowEx_(0, #TOOLTIPS_CLASS, "", #TTS_NOPREFIX, 0, 0, 0, 0, WindowID(#Window), 0, GetModuleHandle_(0), 0)
	ti\cbSize = SizeOf(TOOLINFO)
	ti\hWnd = WindowID(#Window)
	ti\uFlags = #TTF_IDISHWND | #TTF_TRACK | #TTF_CENTERTIP
	ti\uId = GadgetID(#Trackbar)
	Text = StrF((GetGadgetState(#Trackbar) - 50) / 10.0, 1) + "%"  ; Fill text value so that something can be displayed
	ti\lpszText = @Text
	SendMessage_(GadgetID(#Trackbar), #TBM_SETTOOLTIPS, TooltipID, 0)
	SendMessage_(TooltipID, #TTM_ADDTOOL, 0, @ti)

	Repeat
		Select WaitWindowEvent()
			Case #PB_Event_Gadget
				If EventGadget() = #Trackbar
					Text = StrF((GetGadgetState(#Trackbar) - 50) / 10.0, 1) + "%"
					ti\lpszText = @Text
					SendMessage_(TooltipID, #TTM_UPDATETIPTEXT, 0, @ti)
					SendMessage_(TooltipID, #TTM_TRACKACTIVATE, #True, @ti)
				EndIf

			Case #WM_LBUTTONDOWN
				If CursorOverGadget(#Window, #Trackbar) = #True
					TooltipTrackActive = #True
					SendMessage_(TooltipID, #TTM_TRACKACTIVATE, #False, @ti)
				EndIf

			Case #WM_LBUTTONUP
				TooltipTrackActive = #False
				SendMessage_(TooltipID, #TTM_TRACKACTIVATE, #True, @ti)

			Case #WM_MOUSEMOVE
				If TooltipTrackActive = #False
					If CursorOverGadget(#Window, #Trackbar) = #True
						GetWindowRect_(GadgetID(#Trackbar), @r)
						SendMessage_(GadgetID(#Trackbar), #TBM_GETTHUMBRECT, 0, @r2)
						SendMessage_(TooltipID, #TTM_TRACKPOSITION, 0, MakeLong(r\left + r2\left + (r2\right - r2\left) / 2, r\top - r2\top))
						SendMessage_(TooltipID, #TTM_TRACKACTIVATE, #True, @ti)
					Else
						SendMessage_(TooltipID, #TTM_TRACKACTIVATE, #False, @ti)
					EndIf
				EndIf

			Case #PB_Event_CloseWindow
				Break
		EndSelect
	ForEver
EndIf
The only thing I don't like right now is this "+ 3" correction which I need on my system. I tried it with and without XP style and with different window styles (borderless, sizeable) and the tooltip always needs this small position correction. Do you know where it comes from?
Without this "+ 3" I'd say it's perfect. :)

Edit:
Fixed the "+ 3" problem
Last edited by c4s on Thu Dec 30, 2010 7:15 pm, edited 1 time in total.
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: TrackBarGadget(): Show tooltip of current value?

Post by RASHAD »

Now it looks much much better
Good one c4s
Exactly it should be 3.5 not 3
That is because the width of the Thump is 7 pixels
Check r2\right - r2\left = 7
Egypt my love
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: TrackBarGadget(): Show tooltip of current value?

Post by c4s »

Ah well, that makes sense now. I fixed the code.

Rashad, I think we did a good job 8). Maybe I'm going to post it in the Tips & Tricks section someday soon - You can do it as well if you would like to...
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: TrackBarGadget(): Show tooltip of current value?

Post by RASHAD »

You do :D
It is your baby no doubt
Egypt my love
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Re: TrackBarGadget(): Show tooltip of current value?

Post by RichardL »

Just my two cents worth for an old thread...

This code was written to do much the same, but for scrollbars.It uses just one API call... and that could be worked around. It allows maths and colour changes for the ToolTip data and also shows the control position when the various parts of the scroll bar are clicked. No hover...

Code: Select all

; Simple ToolTip ro report ScrollBar() position

Declare.l WinCallback(hwnd, uMsg, wParam, lParam)     ; Window callback       
Enumeration 1
  #VS_1
  #VS_2
  #VS_3
  #HS_1
  #HS_2
  #TT_Data
EndEnumeration
Enumeration 1
  #MainW
  #TipW
EndEnumeration
 
; Open test window with three vertical scroll bars and two horizontal
OpenWindow(#MainW,100,100,500,500,"Test")
ScrollBarGadget(#VS_1,20,20, 15,200,0,105,5,#PB_ScrollBar_Vertical)
ScrollBarGadget(#VS_2,120,20, 25,200,0,1005,5,#PB_ScrollBar_Vertical)
ScrollBarGadget(#VS_3,200,60,35,200,0,105,5,#PB_ScrollBar_Vertical)
ScrollBarGadget(#HS_1,20,300,320,20,0,1005,5)
ScrollBarGadget(#HS_2,20,350,350,30,0,105,5)

; A small window and text gadget used for reporting values
OpenWindow(#TipW,0,0,40,15,"",#PB_Window_Invisible | #PB_Window_BorderLess)
StickyWindow(2,#True)
TextGadget(#TT_Data,0,0,40,15,"",#PB_Text_Center)
SetGadgetColor(#TT_Data, #PB_Gadget_BackColor,#Yellow)

; Start window callback
SetWindowCallback(@WinCallback())

; Skeleton event manager
Finish.w = #False
Repeat
  Select WaitWindowEvent(5)
    
    Case #PB_Event_CloseWindow        ; Close windows
      Finish = #True
      
  EndSelect
Until Finish = #True
End

Procedure.l WinCallback(hwnd, uMsg, wParam, lParam)            ;- Window callback
  Result = #PB_ProcessPureBasicEvents
  Select uMsg 
    Case #WM_VSCROLL                                           ;{- VERTICAL scroll bars
      If wParam <> 8
        G = GetDlgCtrlID_(lParam)                              ; Get Gadget# corresponding to ID
        v = GetGadgetState(G)                                  ; Get current slider value
        x = WindowX(#MainW) + GadgetX(G)+GadgetWidth(G)+4      ; X position for data display
        y = WindowY(#MainW) + GadgetY(G)+15+18 + Int(v/GetGadgetAttribute(G,#PB_ScrollBar_Maximum)* (GadgetHeight(G)-30)); Y...
        
        ; Format dispalyed data specific to each scrollbar
        Select G
          Case #VS_1
            SetGadgetText(#TT_Data,Str(v))
          Case #VS_2
            SetGadgetText(#TT_Data,StrF(100*v/1000,1)+"%")
          Case #VS_3
            SetGadgetText(#TT_Data,Str(50-v))
            If v<50
              SetGadgetColor(#TT_Data, #PB_Gadget_BackColor,#Red)
            Else
              SetGadgetColor(#TT_Data, #PB_Gadget_BackColor,#Green)
            EndIf
        EndSelect
        
        ResizeWindow(#TipW,x,y, #PB_Ignore, #PB_Ignore)        ; Position the data window
        HideWindow(#TipW,#False)                               ; Reveal it
      Else                                                     ; Mouse UP
        HideWindow(#TipW,#True)                                ; Hide the data window
        SetGadgetColor(#TT_Data, #PB_Gadget_BackColor,#Yellow) ; Re-colour with default
      EndIf
      ;}
    Case #WM_HSCROLL                                           ;{- HORIZONTAL scroll bars
      If wParam <> 8
        G = GetDlgCtrlID_(lParam)                              ; Get Gadget#
        v = GetGadgetState(G)                                  ; Current value
        x = WindowX(#MainW) + GadgetX(G) + 15 + Int(v/GetGadgetAttribute(G,#PB_ScrollBar_Maximum)* (GadgetWidth(G)-30)) - WindowWidth(2)/2
        y = WindowY(#MainW) + 18 + GadgetY(G) - WindowHeight(2)
        SetGadgetText(#TT_Data,Str(v))                         ; Put result in data window
        ResizeWindow(#TipW,x,y, #PB_Ignore, #PB_Ignore)        ; Position the data window
        HideWindow(#TipW,#False)                               ; Reveal it
      Else                                                     ; Mouse UP
        HideWindow(#TipW,#True)                                ; Hide the data window
        SetGadgetColor(#TT_Data, #PB_Gadget_BackColor,#Yellow) ; Re-colour with default
      EndIf
      ;}
  EndSelect
  ProcedureReturn Result
EndProcedure


Post Reply