Author Topic: Scrolling messages in a programs title bar  (Read 4103 times)

0 Members and 1 Guest are viewing this topic.

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Scrolling messages in a programs title bar
« on: July 26, 2020, 02:24:27 pm »
I was playing around with @SierraKen scrolling thing he posted recently and it got me thinking that a scrolling message would be neat to put in a programs title bar - maybe grab headlines off the web, or other information.   I haven't added webpage info grabbing, but here's the titlebar scrolling method....

- Dav

Code: QB64: [Select]
  1.  
  2. 'TITLEBARTICKER.BAS
  3. 'Example of scrolling text in the title bar.
  4. 'You can use it as a news ticker, stock data, or other info.
  5. 'by Dav, JULY/2020
  6.  
  7. SCREEN _NEWIMAGE(500, 1, 256)
  8.  
  9. 'You could grab your headlines here from a website instead.
  10. 'I just used some random headlines from actual newspapers below.....really...
  11.  
  12. 'you could also add a Winows API to allow always stay on top of other windows.
  13.  
  14. txt$ = "...BREAKING NEWS....DOCS SAY BREATHING OXYGEN LINKED TO STAYING ALIVE.........."
  15. txt$ = txt$ + "....NEW POLL SAYS MAJORITY OF AMERICANS (4 OUT OF 10) HATE MATH..........."
  16. txt$ = txt$ + "....STUDY SHOWS TEEN PREGNANCY DROPS OFF SIGNIFICANTLY AFTER AGE 25......."
  17. txt$ = txt$ + "....NOW BREAKING....WORLD BANK SAYS THE POOR NEED MORE MONEY.............."
  18. txt$ = txt$ + "....MISSIPPI'S LITERACY PROGRAM STARTING TO SHOW IMPROVEMENT.............."
  19. txt$ = txt$ + "....BREAKING: NORTHFIELD SAYS THEY PLAN TO PLAN A STRATEGIC PLAN.........."
  20. txt$ = txt$ + "....SCIENTISTS SAY GREENLAND METEORITE MAY BE FROM OUTER SPACE............"
  21. txt$ = txt$ + "....2 YEAR STUDY SUGGESTS MOST EARTHQUAKE DAMAGE CAUSED BY SHAKING........"
  22. txt$ = txt$ + "....WA SCIENTISTS DECIDE TO KILL DUCKS TO SEE WHY THEY ARE DYING.........."
  23. txt$ = txt$ + "....PRISONER SERVING 2000 YEAR SENTANCE COULD FACE MORE TIME.............."
  24.  
  25.  
  26. txtlen = LEN(txt$)
  27.  
  28. chars = 60 'number of characters to show in title bar...
  29.  
  30. starttime = TIMER 'start a timer...
  31.  
  32.  
  33.     IF TIMER - starttime > .15 THEN GOSUB TitleScroll
  34.     _LIMIT 30
  35.  
  36.  
  37.  
  38.  
  39.  
  40. '===========
  41. TitleScroll: 'Ahhh! A GOSUB routine!!  RUN!!!
  42. '===========
  43. c = c + 1: IF c >= txtlen THEN c = 1
  44. t2$ = RIGHT$(txt$, txtlen - 1) + LEFT$(txt$, 1)
  45. txt$ = t2$: _TITLE LEFT$(txt$, chars)
  46. starttime = TIMER
  47.  
« Last Edit: July 26, 2020, 02:33:13 pm by Dav »

FellippeHeitor

  • Guest
Re: Scrolling messages in a programs title bar
« Reply #1 on: July 26, 2020, 02:25:49 pm »
My previous experience with frequent _TITLE changes in Windows was that there is a limit. Have you run this long enough it crashed? I tried a while back, so swapping compilers may have fixed something.

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Scrolling messages in a programs title bar
« Reply #2 on: July 26, 2020, 02:28:39 pm »
I haven't ran it for extended periods of time -- but I will now.  It hasn't crashed on me so far.  Thanks for the heads up.

- Dav
« Last Edit: July 26, 2020, 02:30:44 pm by Dav »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Scrolling messages in a programs title bar
« Reply #3 on: July 26, 2020, 02:36:42 pm »
I'm willing to bet that any issue with _TITLE is probably due to the difference in BASIC strings and C strings.  If it acts up with you, null terminate your title string and see if that doesn't correct the issue.

txt$ = t2$: _TITLE LEFT$(txt$, chars) + CHR$(0)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Scrolling messages in a programs title bar
« Reply #4 on: July 26, 2020, 03:48:15 pm »
Quote
"....NEW POLL SAYS MAJORITY OF AMERICANS (4 OUT OF 10) HATE MATH..........."

LOL

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Scrolling messages in a programs title bar
« Reply #5 on: July 26, 2020, 03:49:39 pm »
@Dav Perhaps something like this?

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(500, 1, 256)
  2.  
  3. txt$ = GetNews("2020-07-25", "2020-07-26", "north korea")
  4.  
  5. txtlen = LEN(txt$)
  6.  
  7. chars = 60 'number of characters to show in title bar...
  8.  
  9. starttime = TIMER 'start a timer...
  10.  
  11.  
  12.     IF TIMER - starttime > .15 THEN GOSUB TitleScroll
  13.     _LIMIT 30
  14.  
  15.  
  16.  
  17.  
  18.  
  19. '===========
  20. TitleScroll: 'Ahhh! A GOSUB routine!!  RUN!!!
  21. '===========
  22. c = c + 1: IF c >= txtlen THEN c = 1
  23. t2$ = RIGHT$(txt$, txtlen - 1) + LEFT$(txt$, 1)
  24. txt$ = t2$: _TITLE LEFT$(txt$, chars)
  25. starttime = TIMER
  26.  
  27. FUNCTION GetNews$ (startdate AS STRING, enddate AS STRING, topic AS STRING)
  28.     DIM URL AS STRING
  29.     DIM URLFile AS STRING
  30.     DIM newsrequest AS STRING
  31.     DIM a%
  32.     URLFile = "newsrequest"
  33.     URL = "http://newsapi.org/v2/everything?q=" + FormatAsHTTP(topic) + "&from=" + startdate + "&to=" + enddate + "&sortBy=popularity&apiKey=8509ac8ea3b14cadbb1cafc08cacf1cd"
  34.     a% = API_request(URL, URLFile)
  35.     DIM U AS INTEGER
  36.     U = FREEFILE
  37.     OPEN URLFile FOR BINARY AS #U
  38.     IF LOF(U) <> 0 THEN
  39.         newsrequest = SPACE$(LOF(U))
  40.         GET #U, , newsrequest
  41.         CLOSE #U
  42.         KILL URLFile
  43.     ELSE
  44.         CLOSE #U
  45.         KILL URLFile
  46.         GetNews = ""
  47.         EXIT FUNCTION
  48.     END IF
  49.     DIM headlines(0) AS STRING
  50.     DIM journals(0) AS STRING
  51.     DIM x AS INTEGER
  52.     DO
  53.         x = x + 1
  54.         REDIM _PRESERVE headlines(x) AS STRING
  55.         REDIM _PRESERVE journals(x) AS STRING
  56.         headlines(x) = MID$(newsrequest, INSTR(newsrequest, "title" + CHR$(34) + ":") + 8)
  57.         headlines(x) = LEFT$(headlines(x), INSTR(headlines(x), CHR$(34) + "," + CHR$(34)) - 1)
  58.  
  59.         journals(x) = MID$(newsrequest, INSTR(newsrequest, "name" + CHR$(34) + ":") + 7)
  60.         journals(x) = LEFT$(journals(x), INSTR(journals(x), CHR$(34)) - 1)
  61.         newsrequest = MID$(newsrequest, INSTR(newsrequest, "title" + CHR$(34) + ":") + 8)
  62.     LOOP UNTIL INSTR(newsrequest, "title" + CHR$(34) + ":") = 0
  63.     newsrequest = ""
  64.     FOR x = 2 TO UBOUND(headlines)
  65.         newsrequest = newsrequest + "....From " + journals(x - 1) + ": " + headlines(x) + " "
  66.     NEXT
  67.     GetNews = newsrequest
  68.  
  69. FUNCTION FormatAsHTTP$ (Request AS STRING)
  70.     DIM start%
  71.     DIM position%
  72.     start% = 1
  73.     DO
  74.         position% = INSTR(start%, Request, " ")
  75.         IF position% THEN
  76.             MID$(Request, position%, 1) = "+"
  77.             start% = position% + 1
  78.         END IF
  79.     LOOP UNTIL position% = 0
  80.     FormatAsHTTP = Request
  81.  
  82.     FUNCTION URLDownloadToFileA (BYVAL pCaller AS LONG, szURL AS STRING, szFileName AS STRING, BYVAL dwReserved AS LONG, BYVAL lpfnCB AS LONG)
  83.  
  84. FUNCTION API_request (URL AS STRING, File AS STRING)
  85.     API_request = URLDownloadToFileA(0, URL + CHR$(0), File + CHR$(0), 0, 0)
« Last Edit: July 26, 2020, 04:17:58 pm by SpriggsySpriggs »
Shuwatch!

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Scrolling messages in a programs title bar
« Reply #6 on: July 26, 2020, 04:29:58 pm »
@Dav Perhaps something like this?

That's it! 

Thanks!

- Dav

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Scrolling messages in a programs title bar
« Reply #7 on: July 26, 2020, 04:54:23 pm »
My previous experience with frequent _TITLE changes in Windows was that there is a limit. Have you run this long enough it crashed? I tried a while back, so swapping compilers may have fixed something.
I've seen that before too. I think usually for me it was due to changes happening too quickly.
Shuwatch!

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Scrolling messages in a programs title bar
« Reply #8 on: July 26, 2020, 06:28:20 pm »
That's really cool guys! I thought of this also when I made my giant text scroller but didn't know _TITLE could do it or not. :)

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Scrolling messages in a programs title bar
« Reply #9 on: July 27, 2020, 01:14:28 am »
This is cool! Thanks for sharing this.
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Scrolling messages in a programs title bar
« Reply #10 on: July 29, 2020, 09:38:31 pm »
Updated my source code for the "News Ticker Tape" program using my better functions that way I can more reliably return all the values for the headlines and publishers. Works much better now:

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(500, 1, 256)
  2.  
  3. txt$ = GetNews("2020-07-28", "2020-07-30", "brexit")
  4.  
  5. txtlen = LEN(txt$)
  6.  
  7. chars = 60 'number of characters to show in title bar...
  8.  
  9. starttime = TIMER 'start a timer...
  10.  
  11.  
  12.     IF TIMER - starttime > .10 THEN GOSUB TitleScroll
  13.     _LIMIT 30
  14.  
  15.  
  16.  
  17.  
  18.  
  19. '===========
  20. TitleScroll: 'Ahhh! A GOSUB routine!!  RUN!!!
  21. '===========
  22. c = c + 1: IF c >= txtlen THEN c = 1
  23. t2$ = RIGHT$(txt$, txtlen - 1) + LEFT$(txt$, 1)
  24. txt$ = t2$: _TITLE LEFT$(txt$, chars)
  25. starttime = TIMER
  26.  
  27. FUNCTION GetNews$ (startdate AS STRING, enddate AS STRING, topic AS STRING)
  28.     DIM URL AS STRING
  29.     DIM URLFile AS STRING
  30.     DIM newsrequest AS STRING
  31.     DIM a%
  32.     URLFile = "newsrequest"
  33.     URL = "http://newsapi.org/v2/everything?q=" + FormatAsHTTP(topic) + "&from=" + startdate + "&to=" + enddate + "&sortBy=popularity&apiKey=8509ac8ea3b14cadbb1cafc08cacf1cd"
  34.     a% = API_request(URL, URLFile)
  35.     DIM U AS INTEGER
  36.     U = FREEFILE
  37.     OPEN URLFile FOR BINARY AS #U
  38.     IF LOF(U) <> 0 THEN
  39.         newsrequest = SPACE$(LOF(U))
  40.         GET #U, , newsrequest
  41.         CLOSE #U
  42.         KILL URLFile
  43.     ELSE
  44.         CLOSE #U
  45.         KILL URLFile
  46.         GetNews = ""
  47.         EXIT FUNCTION
  48.     END IF
  49.     REDIM headlines(0) AS STRING
  50.     REDIM journals(0) AS STRING
  51.     DIM x AS INTEGER
  52.     GetAllKey "name", newsrequest, journals()
  53.     GetAllKey "title", newsrequest, headlines()
  54.     newsrequest = ""
  55.     FOR x = 1 TO UBOUND(journals)
  56.         newsrequest = newsrequest + "....From " + journals(x) + ": " + headlines(x) + "  "
  57.     NEXT
  58.     GetNews = newsrequest
  59.  
  60. FUNCTION FormatAsHTTP$ (Request AS STRING)
  61.     DIM start%
  62.     DIM position%
  63.     start% = 1
  64.     DO
  65.         position% = INSTR(start%, Request, " ")
  66.         IF position% THEN
  67.             MID$(Request, position%, 1) = "+"
  68.             start% = position% + 1
  69.         END IF
  70.     LOOP UNTIL position% = 0
  71.     FormatAsHTTP = Request
  72.  
  73.     FUNCTION URLDownloadToFileA (BYVAL pCaller AS LONG, szURL AS STRING, szFileName AS STRING, BYVAL dwReserved AS LONG, BYVAL lpfnCB AS LONG)
  74.  
  75. FUNCTION API_request (URL AS STRING, File AS STRING)
  76.     API_request = URLDownloadToFileA(0, URL + CHR$(0), File + CHR$(0), 0, 0)
  77.  
  78. FUNCTION GetKey$ (keyname AS STRING, JSON AS STRING)
  79.     DIM jkey AS STRING
  80.     IF INSTR(JSON, CHR$(34) + keyname + CHR$(34)) THEN
  81.         jkey = MID$(JSON, INSTR(JSON, CHR$(34) + keyname + CHR$(34)) + LEN(keyname))
  82.         jkey = MID$(jkey, INSTR(jkey, ":") + 1)
  83.         jkey = String.Replace(jkey, "\" + CHR$(34), "'")
  84.         IF MID$(jkey, 1, 1) = CHR$(34) THEN
  85.             jkey = MID$(jkey, 2)
  86.         END IF
  87.         jkey = MID$(jkey, 1, INSTR(jkey, CHR$(34)) - 1)
  88.         IF RIGHT$(jkey, 1) = "," THEN
  89.             jkey = MID$(jkey, 1, LEN(jkey) - 1)
  90.         END IF
  91.     ELSE
  92.         GetKey = ""
  93.     END IF
  94.     GetKey = jkey
  95.  
  96. SUB GetAllKey (keyname AS STRING, JSON AS STRING, ParseKey() AS STRING)
  97.     DIM unchangejson AS STRING
  98.     DIM jkey AS STRING
  99.     DIM x
  100.     unchangejson = JSON
  101.     DO
  102.         IF INSTR(unchangejson, CHR$(34) + keyname + CHR$(34)) THEN
  103.             x = x + 1
  104.             REDIM _PRESERVE ParseKey(x) AS STRING
  105.             unchangejson = MID$(unchangejson, INSTR(unchangejson, CHR$(34) + keyname + CHR$(34)) + LEN(keyname))
  106.             jkey = unchangejson
  107.             jkey = MID$(jkey, INSTR(jkey, ":") + 1)
  108.             jkey = String.Replace(jkey, "\" + CHR$(34), "'")
  109.             IF MID$(jkey, 1, 1) = CHR$(34) THEN
  110.                 jkey = MID$(jkey, 2)
  111.             END IF
  112.             jkey = MID$(jkey, 1, INSTR(jkey, CHR$(34)) - 1)
  113.             IF RIGHT$(jkey, 1) = "," THEN
  114.                 jkey = MID$(jkey, 1, LEN(jkey) - 1)
  115.             END IF
  116.             ParseKey(x) = jkey
  117.         END IF
  118.     LOOP UNTIL INSTR(unchangejson, CHR$(34) + keyname + CHR$(34)) = 0
  119.  
  120. FUNCTION String.Replace$ (a AS STRING, b AS STRING, c AS STRING)
  121.     j = INSTR(a, b)
  122.     IF j > 0 THEN
  123.         r$ = LEFT$(a, j - 1) + c + String.Replace(RIGHT$(a, LEN(a) - j + 1 - LEN(b)), b, c)
  124.     ELSE
  125.         r$ = a
  126.     END IF
  127.     String.Replace = r$
« Last Edit: July 30, 2020, 07:06:17 am by SpriggsySpriggs »
Shuwatch!