?
Solved

Indentifying when my application gets focus

Posted on 2006-06-06
4
Medium Priority
?
160 Views
Last Modified: 2010-04-07
Hello Experts,


     I need a way to determine when my application has recieved focus, either through clicking on the application in the taskbar or by clicking on the form window.  The form_GotFocus and _LoseFocus do not achieve this goal, since form_GotFocus only fires when all of the other applicable controls are disabled.  Is there a way, via API or not, to be able to do this?  I need to be able to open a file and input the contents when the application gets focus and in the interest of efficiency for the end-user, I would prefer them not to have to make an additional click.

Any and all help is greatly appreciated, this is a last-minute design change for a deadline project.


Gwosgood
0
Comment
Question by:gwosgood
  • 2
4 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16847533
You need to subclass and then trap the WM_ACTIVATE msg as in this PAQ:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20305542.html
0
 
LVL 13

Accepted Solution

by:
iHadi earned 2000 total points
ID: 16848164
Hi gwosgood
The following code demonstrates a simple and affective way of gaining your goal without complicated subclassing. Add a timer and a command buton to a form, st the timer interval to 100 and paste the following code:

Private Declare Function GetForegroundWindow Lib "user32" () As Long
Dim PrevActive As Boolean

Private Sub Form_GotFocus()
    MsgBox "Activated"
End Sub

Private Sub Timer1_Timer()
Dim WinHandle As Long

WinHandle = GetForegroundWindow

If Me.hWnd = WinHandle Then
    If PrevActive = False Then
        PrevActive = True
        Form_GotFocus
    End If
Else
    PrevActive = False
End If
End Sub
0
 
LVL 2

Author Comment

by:gwosgood
ID: 16851754
Thank you very much iHadi, a very simple solution, just the thing I was looking for.

One quick question though, what was the purpose of the command button?
0
 
LVL 13

Expert Comment

by:iHadi
ID: 16852460
You got me! I was writing another post that used a command button and it looks like you got a copy of my thoughts too. In this solution you do not need it only if you like clicking on empty coded buttons!
0

Featured Post

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!

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

757 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