My application loses focus after Runprogram()

Just starting out? Need help? Post your questions and find answers here.
tdc69
New User
New User
Posts: 3
Joined: Fri Aug 15, 2014 9:47 am

My application loses focus after Runprogram()

Post 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 :D

thanks
fabulouspaul
User
User
Posts: 34
Joined: Sun Nov 23, 2014 1:18 pm

Re: My application loses focus after Runprogram()

Post 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.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: My application loses focus after Runprogram()

Post by Dude »

SetActiveWindow() with the window number of X.EXE will give it the focus again.
fabulouspaul
User
User
Posts: 34
Joined: Sun Nov 23, 2014 1:18 pm

Re: My application loses focus after Runprogram()

Post 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.
tdc69
New User
New User
Posts: 3
Joined: Fri Aug 15, 2014 9:47 am

Re: My application loses focus after Runprogram()

Post 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. :wink:
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4991
Joined: Sun Apr 12, 2009 6:27 am

Re: My application loses focus after Runprogram()

Post 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
Egypt my love
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: My application loses focus after Runprogram()

Post 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. :shock: :D
https://msdn.microsoft.com/en-us/library/windows/desktop/ms646284(v=vs.85).aspx wrote:If this message is passed on to DefWindowProc, the system will bring the window's last active popup (if it exists) or the window itself (if there is no popup window) to the foreground.
User avatar
chi
Addict
Addict
Posts: 1087
Joined: Sat May 05, 2007 5:31 pm
Location: Austria

Re: My application loses focus after Runprogram()

Post by chi »

:D

Code: Select all

Result = RunProgram("notepad.exe" ,"","", #PB_Program_Wait   )
MessageRequester("ok","I see this", #MB_SETFOREGROUND)
Et cetera is my worst enemy
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4991
Joined: Sun Apr 12, 2009 6:27 am

Re: My application loses focus after Runprogram()

Post by RASHAD »

No way with #PB_Program_Wait

Code: Select all

Result = RunProgram("notepad.exe" ,"","")
MessageRequester("ok","I see this", #MB_SYSTEMMODAL) 
Much better
Egypt my love
Post Reply