Page 1 of 1
My application loses focus after Runprogram()
Posted: Thu Dec 21, 2017 10:51 am
by tdc69
Hello friends,
I have a very silly problem and I will put a simple example.
Code: Select all
Result = RunProgram("notepad.exe" ,"","", #PB_Program_Wait )
MessageRequester("ok","I see this",0)
My X. EXE program calls NOTEPAD. EXE, wait for it to finish and then I want to leave a message on screen.
The problem is that my X. EXE program loses focus and not how to fix it before displaying the message.
I have looked at the help, Libraries Process/window, the forum, Google and I do not see a simple solution and I think it must exist. I warned you, it was a silly problem
thanks
Re: My application loses focus after Runprogram()
Posted: Thu Dec 21, 2017 11:16 am
by fabulouspaul
Hi tdc69,
i dont know if it solves your problem of focus, but you could try
Code: Select all
Result = RunProgram("notepad.exe" ,"","", #PB_Program_Open)
While ProgramRunning(Result)
Delay(100)
Wend
MessageRequester("ok","I see this",0)
You could put your own program back in focus after starting the external program.
Re: My application loses focus after Runprogram()
Posted: Thu Dec 21, 2017 1:20 pm
by Dude
SetActiveWindow() with the window number of X.EXE will give it the focus again.
Re: My application loses focus after Runprogram()
Posted: Thu Dec 21, 2017 2:27 pm
by fabulouspaul
Dude wrote:SetActiveWindow() with the window number of X.EXE will give it the focus again.
I am not sure this will help to get the program back in front again, as the help says
Code: Select all
Remarks
The function will only change the focus within the program. It can not bring the program to the foreground when another program has the focus.
In RSBASICs WinAPI library i found the following
Code: Select all
RunProgram("notepad.exe")
Delay(2000)
SetWindowPos_(FindWindow_("notepad",#Null),#HWND_BOTTOM,0,0,0,0,#SWP_NOMOVE|#SWP_NOSIZE)
This puts the started program to the background an the focus will return to the previous program... but i havent checked that.
Re: My application loses focus after Runprogram()
Posted: Wed Dec 27, 2017 10:21 am
by tdc69
I have tried all your suggestions and something else, but I have not found the solution. Maybe the function itself Runprogram, at least when you ask to wait for your application, should ensure the return to focus and continue its process. In the meantime, any idea would be grateful to not have to rewrite in C. Thank you.

Re: My application loses focus after Runprogram()
Posted: Wed Dec 27, 2017 11:21 am
by RASHAD
Code: Select all
flags = #PB_Window_SystemMenu| #PB_Window_MaximizeGadget| #PB_Window_MinimizeGadget| #PB_Window_ScreenCentered | #PB_Window_SizeGadget
OpenWindow(0,0,0,400,300,"Test",Flags)
Result = RunProgram("notepad.exe" ,"","",#PB_Program_Open| #PB_Program_Read )
Delay(100)
SendMessage_(#HWND_BROADCAST, #WM_SYSCOMMAND, #SC_HOTKEY, WindowID(0))
MessageRequester("ok","I see this",0)
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Quit = 1
EndSelect
Until Quit = 1
End
Re: My application loses focus after Runprogram()
Posted: Wed Dec 27, 2017 12:03 pm
by Dude
RASHAD wrote:SendMessage_(#HWND_BROADCAST, #WM_SYSCOMMAND, #SC_HOTKEY, WindowID(0))
Nice tip, Rashad! I never knew that #SC_HOTKEY could force a window to the foreground and get the focus.
Re: My application loses focus after Runprogram()
Posted: Fri Jan 05, 2018 1:59 am
by chi
Code: Select all
Result = RunProgram("notepad.exe" ,"","", #PB_Program_Wait )
MessageRequester("ok","I see this", #MB_SETFOREGROUND)
Re: My application loses focus after Runprogram()
Posted: Fri Jan 05, 2018 3:10 am
by RASHAD
No way with #PB_Program_Wait
Code: Select all
Result = RunProgram("notepad.exe" ,"","")
MessageRequester("ok","I see this", #MB_SYSTEMMODAL)
Much better