View Full Version : Windows program control
Hi,
A wonderful initial atraction to thinBasic was the manual comment of
"• Windows controlling (Opening, closing, set foreground, sendkeys, ...)"
In reading manual, the function set seems to include all the major actions , "like (sleep, while, ifelse, doevent. loop)" but did not see an action that toggled a running programs keys, sure i don't understand much, and overlooked structure or methods to do this.
Aside from neat examples, a goal in learning was to be able to control an operating commercial program like a browser, Word and Excel on my desktop. I would much appreciate comments about this possibility using thinBasic scripts.
Thank you
JJ
Petr Schreiber
29-03-2006, 10:15
Hi,
if you want to control other programs, there are many ways to do it.
One could be this:
USES "UI"
USES "OS"
DIM handleToWindow AS DWORD
' This will launch notepad
OS_SHELLEXECUTE ( "open", "notepad.exe"[/color], "", "", %OS_WNDSTYLE_NORMAL )
' This will store windows handle
handleToWindow = WIN_GETACTIVE
' This will set it to foreground
WIN_SETFOREGROUND(handleToWindow)
' This will send some text in
SENDKEYS ( "{ENTER}Hello from thinBASIC{ENTER}How do you do?", 10)
I recommend you to study help topics listed in helpfile under
Modules>UI>Windows
Modules>OS
... there are many useful functions
Petr
ErosOlmi
29-03-2006, 12:27
JJ,
thinBasic has some functions working on windows. You can see them in help file under "thinBasic language / Modules / UI (User Interface) / Windows".
Most of them work using window handle (unique window indentifier). You can get window handle using WIN_GETACTIVE as Petr suggested or trying to find the window you want to control using WIN_FindByClass and WIN_FindByTitle. WIN_FindByTitle is quite strong, you can find windows by partial name too. Once you have the window handle you can use it to put the window foreground, background.
SendKey function can be used to simulate user keystrokes. But you have to be sure the window you want to control is the uppermost. To do this use WIN_SetForeground.
thinBasic is not able to control programs using COM technology (Component Object Model) via COM automation. This is a very strong way to control programs but very difficult to work with on a on-the-fly basis (interpreter). It is something we are experimenting but it will take some time (months I suppose) before having something usable.
In any case, if you can be more precise on what you are trying to do, maybe we can get you some detailed examples.
Let me know and ... do not forget to register in this forum.
Ciao
Eros
Thank you , will register for future topics, to continue,
Current use is to toggle a browser to reload a page after some interval from 2 to 60 seconds, may have several browser windows on screen, working to put security cameras around home and to put them on web while traveling for view
Oh!, Missed seeing the sendkey function, it seems very feasible to do, following the advice offered (please correct me if wrong) and thank you for help files, that was quite prompt
Thank you
JJ
ErosOlmi
31-03-2006, 08:19
Perfect.
For starting, a little example just to have an idea:
USES "UI"
USES "OS"
DIM sTitleToFind AS STRING VALUE "...Microsoft Internet Explorer..."
DIM whndl AS NUMBER
whndl = WIN_FINDBYTITLE(sTitleToFind)
IF whndl = 0 THEN
OS_SHELLEXECUTE("open","iexplore.exe","","","showmaximized")
'Give enough time IE to load. On slow machines increase sleep value
SLEEP 5000
whndl = WIN_FINDBYTITLE(sTitleToFind)
END IF
IF whndl <> 0 THEN
WIN_SETFOREGROUND(whndl)
'---Set full screen and wait 0.5 sec
SENDKEYS("{F11}", 500)
'---Refresh
SENDKEYS("{F5}", 500)
'---Back to normal window
SENDKEYS("{F11}", 500)
'---Close the window
'sleep(3000)
'sendkeys("%{F4}{SLEEP 1000}")
ELSE
MSGBOX 0, "Not able to find the correct window."
END IF
Please also consider that we will change something in OS_SHELL... functions and more on Windows handling adding new functionalities.
Ciao
Eros