Application LostFocus?

B

Brian

Is there a way to pause a form timer (or does some event fire) when an Access
app loses the focus (such as when a user minimizes the app he is supposed to
be using at work and instead goes out to browse the Internet...)

This is different from a form losing the focus because when a user minimizes
Access, the current form retains the focus, so that event never fires.
 
B

Brian

On second thought, I jumped the gun on answering my own question. The API
call tells me whether my app is minimized/maximized/restored, but not whether
it has the focus. If a user leaves the app maximized/restored but Alt-Tabs or
uses the Start button to open a different app, I still have the problem of
not knowing that the user is playing Solitaire instead of working.

What I am trying to do is capture the time the app is actually in use (has
the focus) using the timer on a a particular form.
 
J

John Nurick

I've never tried anything like this, but the logical approach would seem
to be to use the GetActiveWindow or GetForegroundWindow API function to
get the handle of the window the user is working in, and then compare
this with Screen.ActiveWindow.hWnd (and/or the handle of the Access
application window or Screen.AtiveReport.hWnd).
 
B

Brian

Thanks. I'm sure that will work.

John Nurick said:
I've never tried anything like this, but the logical approach would seem
to be to use the GetActiveWindow or GetForegroundWindow API function to
get the handle of the window the user is working in, and then compare
this with Screen.ActiveWindow.hWnd (and/or the handle of the Access
application window or Screen.AtiveReport.hWnd).
 
B

Brian

Thanks, John. It works, but there is something I don't understand here. I
used this:

Declare Function apiGetActiveWindow Lib "user32" Alias "GetActiveWindow" ()
As Long

Public Function GetActivehWnd()
Dim hwnd As Long
Dim hWndCurrent As Long

' Get the handle to the currently active window.
hwnd = apiGetActiveWindow()
hWndCurrent = hwnd

' Find the top window (which has no parent window).
While hwnd <> 0
hWndCurrent = hwnd
hwnd = apiGetParent(hwnd)
Wend

GetActivehWnd = hWndCurrent
End Function

For testing purposes, I ran it on a form timer (non-modal form) and updated
the status bar to show the value of GetActivehWnd(). This always returns a
non-zero number when the Access app containing this function has the focus
(regardless of which form/query/report in Access has the focus), but always
returns a zero when any other app (including another Access app) has the
focus.

While this does return sufficient information for my needs, I thought that
the API call would return a long integer that is different depending on which
app has the focus in Windows. Am I missing something fundamental here?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top