Marquee Scrolling Text
Posted: Tue Apr 01, 2025 6:38 pm
Hello PB Team,
I've created a window that includes a String Text, Button and a Marquee text.
The issue with the marquee starting and ending points.
How can I make this marquee starts from the right window edge and ends at the left edge (better to have 10 pixels margines from each edge)?
I've created a window that includes a String Text, Button and a Marquee text.
The issue with the marquee starting and ending points.
How can I make this marquee starts from the right window edge and ends at the left edge (better to have 10 pixels margines from each edge)?
Code: Select all
; Marquee variables
Global marqueeText.s = "This is my beautiful Marquee Text ..."
Global marqueePosition.i = 0
Global marqueeDirection.i = 1
Global marqueeSpeed.i = 1
Procedure UpdateMarquee()
; Calculate the visible portion of the marquee text
marqueeTextLength = Len(marqueeText)
gadgetWidth = GadgetWidth(9)
visibleChars = gadgetWidth / 6 ; Approximate number of characters that fit (6 pixels per char)
; If the text is shorter than the gadget, pad it with spaces to make it scroll smoothly
If marqueeTextLength < visibleChars
paddedText.s = marqueeText + Space(visibleChars - marqueeTextLength + 10)
Else
paddedText = marqueeText + " " ; Add some spaces at the end for smoother scrolling
EndIf
; Update marquee position
marqueePosition + marqueeDirection * marqueeSpeed
; Handle wrapping
If marqueeDirection = 1 ; Right to left
If marqueePosition > Len(paddedText)
marqueePosition = -visibleChars
EndIf
Else ; Left to right
If marqueePosition < -Len(paddedText)
marqueePosition = visibleChars
EndIf
EndIf
; Build the visible portion of the text
visibleText.s = ""
For i = 0 To visibleChars
pos = (marqueePosition + i) % Len(paddedText)
If pos < 0
pos + Len(paddedText)
EndIf
visibleText + Mid(paddedText, pos + 1, 1)
Next
SetGadgetText(9, visibleText)
EndProcedure
If OpenWindow(0, 0, 0, 320, 220, "Marque Text", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
StringGadget(1, 10, 138, 300, 22, "Hello PB team!", #PB_String_ReadOnly)
ButtonGadget(2, 130, 80, 40, 20, "Close") ; Close button
; Flat marquee text gadget at the bottom of the window
TextGadget(9, 10, 198, WindowWidth(0) - 20, 20, "")
SetGadgetAttribute(9, #PB_Text_Border, #True) ; Hide text outside the window
SetGadgetColor(9, #PB_Gadget_BackColor, RGB(240, 240, 240))
AddWindowTimer(0, 4, 100) ; Timer for marquee animation
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
Select EventGadget()
Case 2 ; Close button
Event = #PB_Event_CloseWindow ; Trigger close event
EndSelect
Case #PB_Event_Timer
Select EventTimer()
Case 4
UpdateMarquee()
EndSelect
EndSelect
Until Event = #PB_Event_CloseWindow
CloseWindow(0)
EndIf