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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Am I the Window on Top?

Visual Basic

Am I the Window on Top?

by  jerrygoldring  Posted    (Edited  )
If your application needs to know if the current form has focus (either within you application or System-wide) you can use this snippet to decide...

if AmIOnTop(Me) then
...
end if

The following are placed in a Module...

' Returns Handle of OS-Level Window that has the focus
Declare Function GetForegroundWindow Lib "user32" () As Long

Public Function AmIOnTop(pForm as form) as boolean
Dim lngMyWindowHandle as Long
Dim lngTopWindowHandle as Long

' Get Handle of Calling Window
lngMyWindowHandle = pform.hWnd
' Get handle of Operating System's Focused Window
lngTopWindowHandle = GetForegroundWindow()
' If same, the IAmOnTop, else not
AmIOnTop = (lngTopWindowHandle = lngMyWindowHandle)
Exit Function
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top