Page 1 of 1
Handle and focus
Posted: Thu Sep 14, 2023 12:16 pm
by jak64
Good morning,
In this little program, I retrieve the "handle" of the WEB window.
If, then, the program does something else and I want to restore the "focus" to this WEB page, is there an instruction to do so?
Thank you all
Code: Select all
Global url.s
Global hwnd.i
url="https://www.google.com/"
RunProgram(url,"","",0)
Delay(2000)
hwnd = GetForegroundWindow_()
Re: Handle and focus
Posted: Thu Sep 14, 2023 1:23 pm
by Caronte3D
Maybe: SetForegroundWindow_(hwnd) ?
Also, SetFocus_(hwnd) could be necesary.
Re: Handle and focus
Posted: Thu Sep 14, 2023 5:31 pm
by jak64
Hello Caronte3D,
I added the 2 instructions to my program... Fabulous, it works great, thank you.
This brings me 2 questions:
1) How to know the commands that end with a _
Example
SetForegroundWindow_(hwnd)
I can't find them in the Purebasic docs, is there a documented site with these instructions?
2) In my little test program that I put in my first post, I put a delay of 2 seconds, I could have put 3 or 4 seconds...
Is there a way, when I launch the program to display a website, to know if the page is completely loaded? This would save me from randomly setting a timeout, without knowing if the page is completely loaded...
Thank you all for your help
Re: Handle and focus
Posted: Thu Sep 14, 2023 8:29 pm
by Caronte3D
jak64 wrote: Thu Sep 14, 2023 5:31 pm
1) How to know the commands that end with a _
Example
SetForegroundWindow_(hwnd)
I can't find them in the Purebasic docs, is there a documented site with these instructions?
It's the Windows API:
https://learn.microsoft.com/en-us/windo ... dex-portal
The user: "RSBasic" have a nice library of examples about winAPI:
https://www.rsbasic.de/winapi-library
Re: Handle and focus
Posted: Thu Sep 14, 2023 8:39 pm
by Caronte3D
jak64 wrote: Thu Sep 14, 2023 5:31 pm
2) Is there a way, when I launch the program to display a website, to know if the page is completely loaded? This would save me from randomly setting a timeout, without knowing if the page is completely loaded...
Take a look to this example by kiffi:
viewtopic.php?p=378417#p378417
Re: Handle and focus
Posted: Thu Sep 14, 2023 8:40 pm
by jak64
Thank you Caronte3D,
I went to look at the link...
It's quite extraordinary everything there is, it will take me more than a lifetime to test all that (I'm still 68 years old!)

Re: Handle and focus
Posted: Thu Sep 14, 2023 8:43 pm
by jak64
Thanks Caronte3D but I can't use Webgadget with Chatgpt, it doesn't work. I launch the web page directly with RunProgram as in my little example.
See you
Re: Handle and focus
Posted: Thu Sep 14, 2023 8:58 pm
by Caronte3D
Maybe you can try some alternatives, like:
viewtopic.php?t=72703
Exists several alternatives to webgadget spreaded by the forum, I don't remember, but search for it (I think webview2 or something)
Re: Handle and focus
Posted: Thu Sep 14, 2023 9:08 pm
by jak64
OK, I'll look on the form.
Thank you Caronte3D
Re: Handle and focus
Posted: Fri Sep 15, 2023 8:39 am
by Allen
Hi Jak64,
If you ( same as me) use google.com to load the web page instead of a webgadget, following code may be of interest to you. In chrome, you can monitor the reload icon at the top left corner to check if a web page is fuly loaded. When a page is loading, it show a "X", once completed, it show a reload icon.
Please noted you may need to adjust the capture position of the icon on procedure CaptureCompress2()
#Originalx=109:#Originaly=60:#Width=24:#Height=24 as your monitor resolution may be diffient from mine.
Thanks.
Allen
Code: Select all
EnableExplicit
UseCRC32Fingerprint()
Procedure WebPage2Clipboard()
Protected.INPUT InputKey
; Control key Down
ZeroMemory_(@InputKey,SizeOf(INPUT)) ;
InputKey\Type = #INPUT_KEYBOARD
InputKey\ki\wVk = #VK_CONTROL
InputKey\ki\dwFlags = 0
SendInput_(1, @InputKey, SizeOf(INPUT))
Delay(20)
;a press
InputKey\Type = #INPUT_KEYBOARD
InputKey\ki\wVk = 65
InputKey\ki\dwFlags = 0
SendInput_(1, @InputKey, SizeOf(INPUT))
Delay(20)
; a release
InputKey\Type = #INPUT_KEYBOARD
InputKey\ki\wVk = 65
InputKey\ki\dwFlags = #KEYEVENTF_KEYUP
SendInput_(1, @InputKey, SizeOf(INPUT))
Delay(100)
;c press
InputKey\Type = #INPUT_KEYBOARD
InputKey\ki\wVk = 67
InputKey\ki\dwFlags = 0
SendInput_(1, @InputKey, SizeOf(INPUT))
Delay(20)
;c release
InputKey\Type = #INPUT_KEYBOARD
InputKey\ki\wVk = 67
InputKey\ki\dwFlags = #KEYEVENTF_KEYUP
SendInput_(1, @InputKey, SizeOf(INPUT))
Delay(100)
;Control key up
InputKey\Type = #INPUT_KEYBOARD
InputKey\ki\wVk = #VK_CONTROL
InputKey\ki\dwFlags = #KEYEVENTF_KEYUP
SendInput_(1, @InputKey, SizeOf(INPUT))
Delay(100)
ProcedureReturn
EndProcedure
Procedure ReloadWebPage()
Protected.INPUT InputKey
ZeroMemory_(@InputKey,SizeOf(INPUT))
; Control key Down
InputKey\Type = #INPUT_KEYBOARD
InputKey\ki\wVk = #VK_CONTROL
InputKey\ki\dwFlags = 0
SendInput_(1, @InputKey, SizeOf(INPUT))
Delay(20)
;R press
InputKey\Type = #INPUT_KEYBOARD
InputKey\ki\wVk = 82
InputKey\ki\dwFlags = 0
SendInput_(1, @InputKey, SizeOf(INPUT))
Delay(20)
; R release
InputKey\Type = #INPUT_KEYBOARD
InputKey\ki\wVk = 82
InputKey\ki\dwFlags = #KEYEVENTF_KEYUP
SendInput_(1, @InputKey, SizeOf(INPUT))
Delay(100)
;Control key up
InputKey\Type = #INPUT_KEYBOARD
InputKey\ki\wVk = #VK_CONTROL
InputKey\ki\dwFlags = #KEYEVENTF_KEYUP
SendInput_(1, @InputKey, SizeOf(INPUT))
Delay(150)
EndProcedure
Procedure CloseChrome()
Protected.INPUT InputKey
; Control key Down
InputKey\Type = #INPUT_KEYBOARD
InputKey\ki\wVk = #VK_CONTROL
InputKey\ki\dwFlags = 0
SendInput_(1, @InputKey, SizeOf(INPUT))
Delay(50)
;w press
InputKey\Type = #INPUT_KEYBOARD
InputKey\ki\wVk = 87 ;87
InputKey\ki\dwFlags = 0
SendInput_(1, @InputKey, SizeOf(INPUT))
Delay(50)
;w release
InputKey\Type = #INPUT_KEYBOARD
InputKey\ki\wVk = 87 ;87
InputKey\ki\dwFlags = #KEYEVENTF_KEYUP
SendInput_(1, @InputKey, SizeOf(INPUT))
Delay(50)
;Control key up
InputKey\Type = #INPUT_KEYBOARD
InputKey\ki\wVk = #VK_CONTROL
InputKey\ki\dwFlags = #KEYEVENTF_KEYUP
SendInput_(1, @InputKey, SizeOf(INPUT))
Delay(50)
EndProcedure
Procedure UpdateWebAddr()
Protected.INPUT InputKey
; Control key Down
InputKey\Type = #INPUT_KEYBOARD
InputKey\ki\wVk = #VK_CONTROL
InputKey\ki\dwFlags = 0
SendInput_(1, @InputKey, SizeOf(INPUT))
Delay(20)
;Press L
InputKey\Type = #INPUT_KEYBOARD
InputKey\ki\wVk = 76 ; asc("L")
InputKey\ki\dwFlags = 0
SendInput_(1, @InputKey, SizeOf(INPUT))
Delay(20)
;Release L
InputKey\Type = #INPUT_KEYBOARD
InputKey\ki\wVk = 76
InputKey\ki\dwFlags = #KEYEVENTF_KEYUP
SendInput_(1, @InputKey, SizeOf(INPUT))
Delay(20)
; Press V
InputKey\Type = #INPUT_KEYBOARD
InputKey\ki\wVk =86
InputKey\ki\dwFlags = 0
SendInput_(1, @InputKey, SizeOf(INPUT))
Delay(20)
; Release V
InputKey\Type = #INPUT_KEYBOARD
InputKey\ki\wVk = 86
InputKey\ki\dwFlags = #KEYEVENTF_KEYUP
SendInput_(1, @InputKey, SizeOf(INPUT))
Delay(20)
;Release Ctrl
InputKey\Type = #INPUT_KEYBOARD
InputKey\ki\wVk = #VK_CONTROL
InputKey\ki\dwFlags = #KEYEVENTF_KEYUP
SendInput_(1, @InputKey, SizeOf(INPUT))
Delay(20)
;press enter
InputKey\Type = #INPUT_KEYBOARD
InputKey\ki\wVk =#VK_RETURN
InputKey\ki\dwFlags = 0
SendInput_(1, @InputKey, SizeOf(INPUT))
Delay(20)
; release enter
InputKey\Type = #INPUT_KEYBOARD
InputKey\ki\wVk = #VK_RETURN
InputKey\ki\dwFlags = #KEYEVENTF_KEYUP
SendInput_(1, @InputKey, SizeOf(INPUT))
Delay(150)
EndProcedure
Procedure.b CompIconString(*String1.quad,*String2.quad) ; string is 8 unicode char long; total 16 bytes, compare as two 64 bit quad
If *String1\q<>*String2\q
ProcedureReturn #False
EndIf
*String1+8
*String2+8
If *String1\q<>*String2\q
ProcedureReturn #False
Else
ProcedureReturn #True
EndIf
EndProcedure
Procedure.s CaptureCompress2()
Protected.i hDC,Image
Protected.i *Buffer
Protected.s Result${8}
#Originalx=109:#Originaly=60:#Width=24:#Height=24
Image=CreateImage(#PB_Any,#Width,#Height)
hDC = StartDrawing(ImageOutput(Image))
BitBlt_(hDC, 0, 0, #Width, #Height, GetDC_(0), #Originalx, #Originaly, #SRCCOPY)
*Buffer = DrawingBuffer()
Result$ = Fingerprint(*Buffer,1728, #PB_Cipher_CRC32)
StopDrawing()
ProcedureReturn Result$
EndProcedure
Procedure.s CaptureReloadIcon()
Protected.s Reload$
RunProgram("https://www.google.com/"," --start-maximized","")
Delay(5000) ;WaitCromeStable()
Repeat
Reload$=CaptureCompress2()
ReloadWebPage()
Delay(1000)
Until Reload$=CaptureCompress2()
ProcedureReturn Reload$
EndProcedure
Procedure Main()
Protected.s URL$
#MaxLoadTime=15000 ; milliseconds
#WaitChromeStable=1000
Protected.i Count,StartTime
Protected.s Current$,Reload$
URL$="https://www.purebasic.fr/english/viewtopic.php?t=82437"
Reload$=CaptureReloadIcon()
SetClipboardText(URL$)
UpdateWebAddr()
ClearClipboard()
StartTime=ElapsedMilliseconds()
Repeat ; wait till reload image appear in chrome
Current$=CaptureCompress2()
If CompIconString(@Reload$,@Current$) ; reload image appear
WebPage2Clipboard() ; copy web content in clipboard
Debug GetClipboardText()
CloseChrome()
ProcedureReturn #True
EndIf
If ElapsedMilliseconds()-StartTime>#MaxLoadTime
ClearClipboard()
Break
EndIf
Delay(300) ; check every 300 ms if reload icon appear
ForEver
; Loading of web page exist time limit
ProcedureReturn #False
EndProcedure
Main()
Re: Handle and focus
Posted: Fri Sep 15, 2023 11:01 am
by jak64
Hello Allen,
Thank you for your response, I will test that with the Chatgpt 3.5 site
Good day