How to Find the Handle of the TopMost WindowID: Q126386
|
There can be many TopMost windows in the Z-Order. TopMost windows are those that have their WS_EX_TOPMOST Windows style set. This article describes how to find the handle of the Window that occupies the top-most position above all other windows -- that is, the top-most TopMost window. This is the window that has the highest Z-Order in Windows internal Window Manager List.
Given the window handle of the startup form or any other non-child form,
you can use the GetWindow API function to find the very first window
(highest Z-Order) in the Window Manager List. This window need not
neccessarily be the top-most visible window on the screen. However, if the
SetActiveWindow API function is called immediately after, the next visible
Top-most window (the one that is actually visible as the topmost Window on
the screen) will be Activated. Then you can use the GetActiveWindow API
function to get the window handle of the top-most visible Window on the
screen.
'Enter each Declare statement as one, single line:
Declare Function GetActiveWindow Lib "User" () As Integer
Declare Function GetWindow Lib "User"
(ByVal hWnd As Integer, ByVal wCmd As Integer) As Integer
Declare Function SetActiveWindow Lib "User"
(ByVal hWnd As Integer) As Integer
Global Const GW_HWNDFIRST = 0
Global Const GW_HWNDNEXT = 2
Sub Command1_Click ()
Dim TopMosthWnd%, OldActivehWnd%, NewActivehWnd%
TopMosthWnd% = GetWindow(Me.hWnd, GW_HWNDFIRST)
OldActivehWnd% = SetActiveWindow(TopMosthWnd%)
NewActivehWnd% = GetActiveWindow()
' The following code is required only if there is more than one
' non-child form in the project. In this case you will have to
' start from the next topmost window, if focus shifts to the
' other non-child forms, just before this code on the original
' form is executed.
While NewActivehWnd% = 0
TopMosthWnd% = GetWindow(TopMosthWnd%, GW_HWNDNEXT)
OldActivehWnd% = SetActiveWindow(TopMosthWnd%)
NewActivehWnd% = GetActiveWindow()
Wend
End Sub
' This is Optional. It is used to show the effect of another
' non-child form:
Sub Form_Load ()
form2.Show
End Sub
TopMosthWnd% = GetWindow(Me.hWnd, GW_HWNDFIRST)
TopMosthWnd% = GetWindow(Me.hWnd, GW_OWNER)
TopMosthWnd% = GetWindow(TopMosthWnd%, GW_HWNDFIRST)
Global Const GW_OWNER = 4
Additional query words: 3.00
Keywords :
Version :
Platform :
Issue type :
Last Reviewed: June 10, 1999