Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to determine if application has focus? 1

Status
Not open for further replies.

MikeBronner

Programmer
May 9, 2001
756
US
Sorry for being such a pain tonight, but I guess I saved all my problems for today :).

I need to check my application to see if it has focus. How would I check that? I'm sure its simple,, but can't seem to get my head on straight tonight.

Thanks again for all your help :->. Best Regards and many Thanks!
Michael G. Bronner X-)

"Who cares how time advances? I am drinking [beer] today." Edgar Allan Poe
 

What about using the gotfocus and lostfocus events of your forms to set a boolean?

Sunaj
 
I don't believe that works on the application level. I need to check the application, and not the form.

Any ideas? Best Regards and many Thanks!
Michael G. Bronner X-)

"Who cares how time advances? I am drinking [beer] today." Edgar Allan Poe
 
Hi,

Yes I can see that it dosen't work -sorry.
try this:

Private Declare Function GetActiveWindow Lib "user32" () As Long

If GetActiveWindow <> 0 Then
'Application in focus
Else
'Application not in focus
End If

Sunaj
 
I am having difficulty with this too - I need to make by application (a text editor) the focus, as when the user presses backspace to delete some of their text, it acts like the Back button...

How can I tell my application (an activex control) that is is the focus item?
 
Thanks sunaj!

Your post led me in the right direction. However, your solution does not work under Windows 2000 or Windows NT, as they handle the GetActiveWindow call differently.

I was able to put together the following workaround:

Private Declare Function GetForegroundWindow Lib &quot;user32&quot; () as Long

Dim lWindowID as Long

Private Sub Form_Load()
[your code]
lWindowID = GetForegroundWindow
End Sub

Private Sub YourSub()
If (GetForegroundWindow = lWindowID) Then
[your code here]
End If
End Sub

Good luck Silentz :) Best Regards and many Thanks!
Michael G. Bronner X-)

&quot;Who cares how time advances? I am drinking [beer] today.&quot; Edgar Allan Poe
 
I wanted to add that the reason you need to store the WindowID upon loading the form, is that each open window receives a unique (inconsistant) ID. The only way to make sure that the current window is active (or inactive) is to compare the saved window ID with the current one, as the GetForegroundWindow can be executed in the background.

In m y instance, I have set up a timer control to check if the window is active in combination with a roll-over image menu system. The timer will continue to execute and check the GetForegroundWindow even when the application does not have the user's focus.

I used this method to disable the roll-over image menu system in the case that the application does not have the user's focus. This has two reasons: it saves resources as well as does not distract the user (sounds, images) when he or she is not currently using that application. Best Regards and many Thanks!
Michael G. Bronner X-)

&quot;Who cares how time advances? I am drinking [beer] today.&quot; Edgar Allan Poe
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top