Setting a focus to a form

Posted on 2003-03-12
Medium Priority
Last Modified: 2010-05-01
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
Question by:cbueno
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3

Expert Comment

ID: 8121819
you would proably have to hook the application you called and look for when it loses focus

Author Comment

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

Expert Comment

ID: 8122008

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&

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.
     Call GetExitCodeProcess(lProcessId, lExitCode)
     ' Make sure you have this DoEvents to give timeslices back to the OS
  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,

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 8122208
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???

Expert Comment

ID: 8122561

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?



Author Comment

ID: 8122771

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,

Author Comment

ID: 8126865
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...

Accepted Solution

Sweat earned 320 total points
ID: 8129059

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,



Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month11 days, 11 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question