Option Explicit
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function SendMessageByStr& Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String)
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Private Declare Sub GetCursorPos Lib "user32" (lpPoint As POINTAPI)
Private Declare Function WindowFromPoint Lib "user32" (ByVal ptY As Long, ByVal ptX As Long) As Long
Private Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
Private Declare Function GetWindowWord Lib "user32" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal h&, ByVal hb&, ByVal x&, ByVal y&, ByVal cx&, ByVal cy&, ByVal f&) As Long
Private Declare Function GetActiveWindow& Lib "user32" ()
'This centers the form. I usually add a function to center
'a form but since this is just a one-form kinda program,
'I just put it here.
Me.Left = (Screen.Width - Me.Width) / 2
Me.Top = (Screen.Height - Me.Height) / 2
'This calls the StayOnTop function. You can find this
'particular function under every rock and in just about
'every book on Visual Basic. :)
StayOnTop Me
End Sub
Private Sub Label1_Click()
End
End Sub
'使本窗体alwaysontop
Private Sub StayOnTop(frm As Form)
'Among the things I played with was removing the Hex function from
'this code. Personally, I don't care much about Hex values. If that
'kind of thing floats *your* boat, pull out the KB article and
'change it back. :)
Dim ptCursor As POINTAPI
Dim sWindowText As String * 100
Dim sClassName As String * 100
Dim hWndOver As Long
Dim hWndParent As Long
Dim sParentClassName As String * 100
Dim wID As Long
Dim lWindowStyle As Long
Dim hInstance As Long
Dim sParentWindowText As String * 100
Dim sModuleFileName As String * 100
Static hWndLast As Long
Call GetCursorPos(ptCursor) ' Get cursor position
hWndOver = WindowFromPoint(ptCursor.x, ptCursor.y) ' Get window cursor is over
If hWndOver <> hWndLast Then ' If changed update display
hWndLast = hWndOver ' Save change
Cls ' Clear the form
Print "Window Handle: "; (hWndOver) ' Display window handle
Print "Focus: "; GetActiveWindow() 'I added this
r = GetWindowText(hWndOver, sWindowText, 100) ' Window text
Print "Window Text: " & Left(sWindowText, r)
r = GetClassName(hWndOver, sClassName, 100) ' Window Class
Print "Window Class Name: "; Left(sClassName, r)
' Get handle of parent window:
hWndParent = GetParent(hWndOver)
' If there is a parent get more info:
If hWndParent <> 0 Then
' Get ID of window:
wID = GetWindowWord(hWndOver, GWW_ID)
Print "Window ID Number: "; (wID)
Print "Parent Window Handle: "; (hWndParent)
' Get the text of the Parent window:
r = GetWindowText(hWndParent, sParentWindowText, 100)
Print "Parent Window Text: " & Left(sParentWindowText, r)
' Get the class name of the parent window:
r = GetClassName(hWndParent, sParentClassName, 100)
Print "Parent Window Class Name: "; Left(sParentClassName, r)
Else
' Update fields when no parent:
Print "Window ID Number: N/A"
Print "Parent Window Handle: N/A"
Print "Parent Window Text : N/A"
Print "Parent Window Class Name: N/A"
End If
' Get window instance:
hInstance = GetWindowWord(hWndOver, GWW_HINSTANCE)
' Get module file name:
r = GetModuleFileName(hInstance, sModuleFileName, 100)
Print "Module: "; Left(sModuleFileName, r)
End If