魔兽塔纳安丛林宝箱:VB Windows API 获取窗口句柄

来源:百度文库 编辑:九乡新闻网 时间:2024/04/27 21:02:19

由于一个特殊问题和一个特殊目的,我打算写一个比较“evil”的程序(不许联想 -__-),虽然以前并没有搞过类似的东东,但凭直觉判断(可能相当不准)使用 VB 和 Windows API 搞起来会比较容易。

这个东东的第一步需要获取某个窗口的句柄,在网上找了相关资料,先照家猫画华南虎写了个可以获取鼠标所指的窗口句柄的小程序,现将代码分享如下,这么短注释我就不写了,相信都能看得懂:

Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function WindowFromPointXY Lib "user32" Alias "WindowFromPoint" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetFocus Lib "user32" () As Long
Private Declare Sub GetSystemTime Lib "kernel32" (lpSystemTime As SYSTEMTIME)
Private Type POINTAPI
x As Long
y As Long
End Type
Private Sub Form_Load()
    SetWindowPos Me.hwnd, -1, 0, 0, 200, 0, conSwpNoActivate Or conSwpShowWindow
End Sub
Private Sub Timer1_Timer()
    Dim xy As POINTAPI
    GetCursorPos xy
    ahwnd = WindowFromPointXY(xy.x, xy.y)
    Me.Caption = "Handler : " & ahwnd
End Sub