Link to home
Start Free TrialLog in
Avatar of cbueno
cbueno

asked on

Setting a focus to a form

Hello to all!!

I got a program where execute a new proram using the shell command
   Shell "scrsaver.exe", vbMaximizedFocus

Now how do i get the focus back on this program if I click away???
I am using win2000.

This has to work when i use alt tab, so if i alt tab this program and switch to another the program should get the focus again!!! Any ideas my friends???

Carlos Bueno
carlosbueno2000@yahoo.com
Avatar of PlanetCpp
PlanetCpp

you would proably have to hook the application you called and look for when it loses focus
Avatar of cbueno

ASKER

Well currently i am traying the lost focus event but without any hooks!! A guess what i doesnt work!!! =(
cbueno,

When you shell to the new program, you must obtain it's handle.  Then the calling program would use the API to see if that handle was still active.  When it becomes inactive, focus would then return to your program.

You'll need some declarations:

Public Const STATUS_PENDING = &H103&
Public Const PROCESS_QUERY_INFORMATION = &H400

Then some API declarations:
 
Public Declare Function OpenProcess Lib "Kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long

Public Declare Function GetExitCodeProcess Lib "Kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long

And then you'll need some sub or function to do the actual calling and then waiting:

Sub eShell()
  Dim lInst As Long
  Dim lProcessId As Long
  Dim lExitCode As Long

  ' This does the actual calling to the new program...
  lInst = Shell("c:\app_to_run.exe", vbNormalFocus)

  ' Now. Once you've called the program, you have the program instance.  You need to get the id of the process you just called...
  lProcessId = OpenProcess(PROCESS_QUERY_INFORMATION, False, lInst)

  ' Then you'll sit inside this do/loop, checking to see if the process issues an exit code or in other words, ends.
  Do
     Call GetExitCodeProcess(lProcessId, lExitCode)
     ' Make sure you have this DoEvents to give timeslices back to the OS
     DoEvents
  Loop While lExitCode = STATUS_PENDING

  ' Finally, when the exit code no longer matches the status_pending value, you fall through the loop and exit

End Sub

Hope this helps,
Sweat

Avatar of cbueno

ASKER

Hi Sweat

So if i now have the instance of the program how should i then set ->the form back to on focus on the lost_focus event of the form???
cbueno,

You don't.

When you shell to the "other" program, you put the first program into a sort of sleep.  It sits there inside the Do/Loop waiting for the "other" program to end.  When it does, the API returns a value saying that the "other" program is no longer pending.  Then the first program falls through the Do/Loop and control returns to the first program again.

I thought that's what you were looking for.  Was I off base?

Sweat

Avatar of cbueno

ASKER

Sweat

Funny enough i kind of lost track of it meself!! lol too many hours programming. I am a student and the labs are closing now so i will give it a try latter on tomorrow and i will give feedback. I cant try it tonight cos PC is bust

Till later,
cbueno
 
Avatar of cbueno

ASKER

Hi Sweat

Ok my question!!!
When i execute lets say the scrsaver.exe using the shell command and the scrsaver.exe is in focus and lets say i decide to use ALT TAB to the other programs running at the back and i pick lets say explorer-->how can i make it so that once I click on explorer, the scrsaver.exe will come on top again rather than explorer. Now my question is are you off base??? That is the question...
ASKER CERTIFIED SOLUTION
Avatar of Sweat
Sweat

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial