Author Topic: Don't get me started on START!  (Read 3262 times)

0 Members and 1 Guest are viewing this topic.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Don't get me started on START!
« on: December 04, 2018, 05:40:15 pm »
I'm glad Rob decided to keep START in the SHELL part of QB64, but those great switches like ?MIN and ?MAX are lost these days due to software circumventing control even over the startup shortcuts. Programs like Paint.net, FireFox, and others simply track how they were last closed, and open the same way. It doesn't matter what your startup window in a shortcut is designated to do and it doesn't matter if you use START with a switch. I wrote a bit more about it at: https://www.tapatalk.com/groups/qbasic/the-start-command-seems-to-be-irrelevant-these-day-t39413.html

So a possible way around this, other than _SCREENCLICK, is the Windows API. Does anyone know how to get the handle of a program like FireFox?

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Don't get me started on START!
« Reply #1 on: December 04, 2018, 08:47:20 pm »
Code: QB64: [Select]
  1.   FUNCTION FindWindowA& (BYVAL ClassName AS _OFFSET, WindowName$) 'handle by title
  2.  
  3. title$ = "Example Title" 'string variable avoids title typo's
  4. _TITLE title$
  5. _DELAY .5 'Give Windows time to register the title
  6. hwnd& = FindWindowA(0, title$ + CHR$(0))[\code]
  7.  
  8. Substitute program title for one your want to search for, such as "FireFox"
  9.  
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Don't get me started on START!
« Reply #2 on: December 04, 2018, 09:38:33 pm »
Thanks!

I'm looking into it now. I printed the variable hwnd& and I noticed each time I run the program, the value changes, even though the firefox window remains open and idle. I would think that number would be constant but if it isn't that would mean you have to poll it each time before taking an action. Does that sound right to you?

Well. To test I tried to minimize firefox, so either I'm doing something wrong or an title you give it is just registered as the title of the QB program, because it's the QB program that gets minimized, not firefox.

Code: QB64: [Select]
  1.     FUNCTION FindWindowA& (BYVAL ClassName AS _OFFSET, WindowName$) 'handle by title
  2.     FUNCTION ShowWindow& (BYVAL hwnd AS _OFFSET, BYVAL nCmdShow AS LONG) 'maximize process
  3.  
  4. title$ = "firefox" 'string variable avoids title typo's
  5. _TITLE title$
  6. _DELAY .5 'Give Windows time to register the title
  7. hwnd& = FindWindowA(0, title$ + CHR$(0))
  8. PRINT hwnd&
  9. x& = ShowWindow&(hwnd&, 2)

If I remark out _TITLE$, it returns 0, which makes me think the routine is specific to the QB program only, and as coded cannot be used to find the handle of another program. :(

Pete
« Last Edit: December 04, 2018, 10:03:43 pm by Pete »
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Don't get me started on START!
« Reply #3 on: December 05, 2018, 03:00:27 am »
Set the title of the QB64 program to something else.

find$ = "Firefox"
_TITLE$ = "QB64 hwnd Finder"
hwnd& = FindWindowA(0, find$ + CHR$(0))
PRINT hwnd&

When multiple windows have the same title, FindWindowA simply returns the value for the first one it comes across.  It's like using a bloodhound named "Rambo"  to hunt an escaped prisoner named "Rambo".

Every time you get close to catching the guy, he gets away when you yell for him to, "Stop Rambo!!"
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Don't get me started on START!
« Reply #4 on: December 05, 2018, 06:24:24 am »
:(  :(  :( ...

It's more complicated, I'm afraid. As I suspected, it isn't just looking for the process, it's trying to find the title of the page, so when it's firefox, it depends on what page is in the active tab. You have to use the title of that page. So even if you shelled to that page, the author of that page could change the title, after which your program could no longer find it.

Here are some tests I conducted. Weird, but in the case of Task manager, it found it but could not minimize it...

Code: QB64: [Select]
  1.     FUNCTION FindWindowA%& (BYVAL ClassName AS _OFFSET, WindowName$) 'handle by title
  2.     FUNCTION ShowWindow& (BYVAL hwnd AS _OFFSET, BYVAL nCmdShow AS LONG) 'maximize process
  3.  
  4. 'find$ = "calculator" ' Works
  5. 'find$ = "qb64 x32" ' Works
  6. find$ = "Bing - mozilla firefox" ' Works but you need active window displayed.
  7. 'find$ = "QB64.org Forum - Index - mozilla firefox"
  8. 'find$ = "Document - WordPad" ' Works
  9. 'find$ = "untitled - Nvu" ' Works
  10. 'find$ = "Untitled - Notepad" ' Works
  11. 'find$ = "Task Manager" ' Finds but cannot minimize.
  12. _TITLE "hwnd Finder"
  13. hwnd%& = FindWindowA(0, find$ + CHR$(0))
  14. PRINT hwnd%&
  15. x& = ShowWindow&(hwnd%&, 2)
  16. PRINT x& ' Should return value 24.
  17.  

I really want to find a better way to find and control just the firefox browser, itself. Maybe looking into some parent/child function is the answer?

Pete

Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Don't get me started on START!
« Reply #5 on: December 05, 2018, 02:58:35 pm »
Well I'm going to settle for this Mickey Mouse work around. If I can't get the title to the open tab, I can just force a tab I know the tile of onto the browser, and then control it. New Tab would be one, but I'm going to use Bing.com in the example. One WARNING is if you do the last step, to close the browser, the Restore Previous Session info gets lost. Just restore the previous session with the tools selection, and close the browser. You can "x" to exit the program or just hit a key one more time.

This demo only works in Windows, because it uses the Windows API calls. It opens firefox, keeps the QB app in focus, and minimizes it, restores it, makes it full screen, and closes it.

Code: QB64: [Select]
  1.     FUNCTION FindWindowA%& (BYVAL ClassName AS _OFFSET, WindowName$) 'handle by title
  2.     FUNCTION ShowWindow& (BYVAL hwnd AS _OFFSET, BYVAL nCmdShow AS LONG) 'maximize process
  3.     FUNCTION SetForegroundWindow& (BYVAL hwnd AS _OFFSET) 'set foreground window process(focus)
  4.     FUNCTION GetForegroundWindow%& 'find currently focused process handle
  5.  
  6. _SCREENMOVE 300, 200
  7. title$ = "hwnd Finder"
  8. _TITLE title$
  9. find$ = "Bing - mozilla firefox" ' Works but you need active window displayed.
  10. findqb$ = title$
  11. SHELL _HIDE "firefox.exe bing.com"
  12. GOSUB focus
  13. PRINT "Make Firefox window size and press any key to minimize."
  14. PRINT "It will restore in 5 sec."
  15. x& = ShowWindow&(hwnd%&, 2)
  16. ' 0 closes app. 1 restores app. 2 minimizes app. 3 maximizes app.
  17. y& = ShowWindow&(hwnd%&, 1)
  18. GOSUB focus
  19. PRINT "Press any key to Maximize Firefox."
  20. y& = ShowWindow&(hwnd%&, 3)
  21. GOSUB focus
  22. PRINT "Press any key to Close Firefox."
  23. y& = ShowWindow&(hwnd%&, 0)
  24.  
  25. focus:
  26. hwnd%& = FindWindowA(0, findqb$ + CHR$(0))
  27. FGwin%& = GetForegroundWindow%& 'get current process in focus
  28. IF FGwin%& <> hwnd%& THEN z& = SetForegroundWindow&(hwnd%&) 'set focus when necessary
  29. hwnd%& = FindWindowA(0, find$ + CHR$(0))
  30.  

Pete
« Last Edit: December 05, 2018, 05:10:43 pm by Pete »
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline robpugh0829

  • Newbie
  • Posts: 19
    • View Profile
Re: Don't get me started on START!
« Reply #6 on: December 09, 2018, 12:39:52 pm »
What's funny is at work the other day I was tasked to create a user interface that would run 360 videos but I needed to use 2 different programs to run them. Oh, and they needed it the next day, so I turned to trusty old QB64 and I actually was able to run batch scripts that did what I needed it to do using START and /MAX and they worked fine. I was quite surprised by it. I made the interface less than 2MB as well, which size was important to them as well. I was very happy with how QB64 performed.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Don't get me started on START!
« Reply #7 on: December 09, 2018, 01:32:48 pm »
Sure, some programs will still work with START /M, but not all. Programs like Firefox override it. In other words, yes, it still works in QB64, as it did in QBasic, but it depends on if the program you are using it on accepts it. I'm glad your video program(s) did!

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Don't get me started on START!
« Reply #8 on: December 09, 2018, 06:53:23 pm »
Hi Pete

in my experience this method doesn't work for QB64 because when we load a file into it  the title bar show the name of the file loaded...
 so you need search exactly the title bar that is now....
the same does Notepad.exe, Wordpad.exe....
IMHO  FindWindowA%& finds the exact string of the  WindowName$ in the title of the window.
just to say if I use
Quote
'find$ = "Untitled - Notepad" ' Works
I must localize to my language to let work the code after using in a previous line the following code
Code: QB64: [Select]
  1. SHELL _DONTWAIT "Notepad.exe"
  2. _delay .5 ' to wait OS while it loads Notepad

It maybe that in API windows there is a findwindow like INSTR keyword of  QB64 or another way to get a list of the titles of the windows opened on the desktop to search that one we must manage from into QB64 application.


Thanks for share your experience
Programming isn't difficult, only it's  consuming time and coffee

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Don't get me started on START!
« Reply #9 on: December 09, 2018, 07:04:40 pm »
I actually got through all that stuff this week and made the program!

It also uses the Windows API to wait until the title loads, so you don't need a delay.

Code: QB64: [Select]
  1. i = 0
  2. DO ' Wait until title is registered.
  3.     i = i + 1
  4.     hwnd%& = FindWindowA(0, findbrowsertitle$ + CHR$(0))
  5.     _DELAY .2
  6.     IF hwnd%& THEN EXIT DO
  7. LOOP UNTIL i = 100 ' Fail safe in case SHELL fails.

https://www.qb64.org/forum/index.php?topic=860.0

Check it out.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Don't get me started on START!
« Reply #10 on: December 09, 2018, 07:25:46 pm »
Quote
Check it out.
Thanks I'll do tomorrow
I need some sleep now. Your creature needs more time to be watched.

Programming isn't difficult, only it's  consuming time and coffee