Basic Univers
Enumeration
  #WIN
  #TRACK1
  #TRACK2
  #OPT
  #BT1
  #BT2
EndEnumeration

#Class_TrackBar = "msctls_trackbar32"


Procedure.s GetGadgetClass(hGadget.l)
  Protected Class.s
  Class = Space(255)
  GetClassName_(hGadget, @Class, 254)
  ProcedureReturn Class
EndProcedure



Procedure VerticalTrackBar(nBar.l)
  If IsGadget(nBar) And GetGadgetClass(GadgetID(nBar)) = #Class_TrackBar
    SetWindowLong_(GadgetID(nBar), #GWL_STYLE, GetWindowLong_(GadgetID(nBar), #GWL_STYLE) | 2)
  EndIf
EndProcedure


Procedure HorizontalTrackBar(nBar.l)
  If IsGadget(nBar) And GetGadgetClass(GadgetID(nBar)) = #Class_TrackBar
    SetWindowLong_(GadgetID(nBar), #GWL_STYLE, GetWindowLong_(GadgetID(nBar), #GWL_STYLE) & ~2)
  EndIf
EndProcedure



Procedure.l GetMinTB(nBar.l)
  If IsGadget(nBar) And GetGadgetClass(GadgetID(nBar)) = #Class_TrackBar
    ProcedureReturn SendMessage_(GadgetID(nBar), #TBM_GETRANGEMIN, 0, 0)
  EndIf
EndProcedure

Procedure.l GetMaxTB(nBar.l)
  If IsGadget(nBar) And GetGadgetClass(GadgetID(nBar)) = #Class_TrackBar
    ProcedureReturn SendMessage_(GadgetID(nBar), #TBM_GETRANGEMAX, 0, 0)
  EndIf
EndProcedure

Procedure SetMinTB(nBar.l, val.l)
  If IsGadget(nBar) And GetGadgetClass(GadgetID(nBar)) = #Class_TrackBar
    SendMessage_(GadgetID(nBar), #TBM_SETRANGEMIN, 0, val)
  EndIf
EndProcedure

Procedure SetMaxTB(nBar.l, val.l)
  If IsGadget(nBar) And GetGadgetClass(GadgetID(nBar)) = #Class_TrackBar
    SendMessage_(GadgetID(nBar), #TBM_SETRANGEMAX, 0, val)
  EndIf
EndProcedure




Procedure ReplaceTrackBarCursor(nBar.l, nWin.l)
  Protected curPos.Point, curWin.l, style.l

  curWin = WindowID() ; On enregistre la fenetre courante
  GetCursorPos_(curPos)
  
  If IsWindow(nWin) And IsGadget(nBar) And GetGadgetClass(GadgetID(nBar)) = #Class_TrackBar
    UseWindow(nWin)
    style = GetWindowLong_(GadgetID(nBar), #GWL_STYLE)
    If style & 2 ; ---- Verticale ----
      curPos\y = curPos\y -(WindowY() + GadgetY(nBar)+ 10)
      If GetWindowLong_(WindowID(), #GWL_STYLE) & #WS_POPUP <> #WS_POPUP
        curPos\y - 20 ; (pr la barre de titre)
      EndIf
      SetGadgetState(nBar,(GetMaxTB(nBar)-(curPos\y *(GetMaxTB(nBar)- GetMinTB(nBar))) /(GadgetHeight(nBar)- 20))+ GetMinTB(nBar) )
    Else ; --- Horizontale ---
      curPos\x = curPos\x -(WindowX() + GadgetX(nBar)+ 10)
      SetGadgetState(nBar,((curPos\x *(GetMaxTB(nBar)- GetMinTB(nBar))) /(GadgetWidth(nBar)- 20))+ GetMinTB(nBar) )
    EndIf
    UseWindow(GetDlgCtrlID_(curWin)) ; On rend le focus à la fenetre
  Else
    Debug "gad/win not initialized or not a TrackBar"
  EndIf
EndProcedure




If OpenWindow(#WIN, 0, 0, 320, 200, #PB_Window_ScreenCentered | #PB_Window_SystemMenu, "CheckBoxGadget") And CreateGadgetList(WindowID())
  TrackBarGadget(#TRACK1, 10, 40, 250, 20, 0, 10000)
  SetGadgetState(#TRACK1, 100)
  TrackBarGadget(#TRACK2, 10, 80, 100, 100, 0, 10000, #PB_TrackBar_Vertical)
  CheckBoxGadget(#OPT, 10, 60, 250, 20, "Activer l'aide")
  ButtonGadget(#BT1, 130, 120, 80, 20, "Horizontal")
  ButtonGadget(#BT2, 130, 150, 80, 20, "Vertical")
  
  Repeat
    EventID = WaitWindowEvent()
  
    If EventID = #PB_Event_Gadget
    
      Select EventGadgetID()
      
        Case #TRACK1
          If GetGadgetState(#OPT)
            ReplaceTrackBarCursor(#TRACK1, #WIN)
          EndIf
        ; EndCase
        
        Case #TRACK2
          If GetGadgetState(#OPT)
            ReplaceTrackBarCursor(#TRACK2, #WIN)
          EndIf
        ; EndCase
        
        
        Case #BT1
          HorizontalTrackBar(#TRACK2)
        ; EndCase
        
        Case #BT2
          VerticalTrackBar(#TRACK2)
        ; EndCase
      
      EndSelect
      
    EndIf

  Until EventID =#PB_Event_CloseWindow
EndIf