如何获取桌面上第二置顶的窗口句柄?
假设我有3个窗口,(A,B,C分别是三个不同程序的窗口),现在我想判断这三个窗口谁是最顶端,谁是其次,谁是最后,该如何操作呢,请发代码,谢谢!
[解决办法]
Option Explicit
Dim lngHandleA As Long
Dim lngHandleB As Long
Dim lngHandleC As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Sub Form_Load()
Form2.Show
Form3.Show
Form4.Show
Form2.Caption = "A"
Form3.Caption = "B"
Form4.Caption = "C"
lngHandleA = FindWindow(vbNullString, "A") '窗体A句柄
lngHandleB = FindWindow(vbNullString, "B") '窗体B句柄
lngHandleC = FindWindow(vbNullString, "C") '窗体C句柄
Timer1.Interval = 1000
End Sub
Private Sub Timer1_Timer()
Dim lngP As Long
lngP = GetForegroundWindow
If lngP = lngHandleA Then
Debug.Print "窗体A置顶"
ElseIf lngP = lngHandleB Then
Debug.Print "窗体B置顶"
ElseIf lngP = lngHandleC Then
Debug.Print "窗体C置顶"
Else
Debug.Print "三个窗体都没有置顶"
End If
End Sub