Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 174
  • Last Modified:

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
0
cbueno
Asked:
cbueno
  • 4
  • 3
1 Solution
 
PlanetCppCommented:
you would proably have to hook the application you called and look for when it loses focus
0
 
cbuenoAuthor Commented:
Well currently i am traying the lost focus event but without any hooks!! A guess what i doesnt work!!! =(
0
 
SweatCommented:
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

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
cbuenoAuthor Commented:
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???
0
 
SweatCommented:
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

0
 
cbuenoAuthor Commented:
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
 
0
 
cbuenoAuthor Commented:
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...
0
 
SweatCommented:
cbueno,

I'm on the West Coast, sorry for the delay.

"...how can i make it so that once I click on explorer, the scrsaver.exe will come on top again rather than explorer..."

So are you saying that you want the screen saver to be the application that stays on-top of all other programs???  If so, then there has to be some kind of key combination that will remove the screen saver so that other programs can function.

Let me know,

Sweat


0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now