在游戏外挂编程当中,要控制某些动作,需要知道游戏窗口的句柄和类名。怎样知道游戏窗口的句柄和类名呢?可以用以下的方法来实现(编程语言:VB)
==========================================
'声明API
'热键API,用于下面检测鼠标右键是否单击
Private Declare Function GetAsyncKeyState Lib _
"user32" (ByVal vKey As Long) As Integer
'获取类名的API
Private Declare Function GetClassName Lib "user32" Alias _
"GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName _
As String, ByVal nMaxCount As Long) As Long
'设置窗体属性的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
'获取鼠标光标位置的API
Private Declare Function GetCursorPos Lib "user32" _
(lpPoint As POINTAPI) As Long
'获取光标处窗口句柄的API
Private Declare Function WindowFromPointXY Lib "user32" _
Alias "WindowFromPoint" (ByVal xPoint As Long, ByVal yPoint _
As Long) As Long
'代表鼠标右键单击的常量
Private Const VK_RBUTTON = &H2
'鼠标光标位置的数据结构
Private Type POINTAPI
x As Long
y As Long
End Type
Private Sub Form_DblClick()
'双击本程序窗体,就可以重新启动获取句柄功能
Timer1.Enabled = True
End Sub
Private Sub Form_Load()
'让窗体总在最前面,方便观察数据
SetWindowPos Me.hwnd, -1, 0, 0, Me.Width / 15, _
Me.Height / 15, SWP_NOACTIVATE Or SWP_SHOWWINDOW
End Sub
Private Sub Timer1_Timer()
Dim xy As POINTAPI
'要获取的窗口句柄
Dim ahwnd As Long
'要获取的类名
Dim cName As String
'获取鼠标光标位置
GetCursorPos xy
'获取光标处窗口句柄
ahwnd = WindowFromPointXY(xy.x, xy.y)
Text1.Text = ahwnd
cName = String(&H100, vbNullChar) '缓冲256长度的字符串。
'获取窗口类名
GetClassName ahwnd, ByVal cName, Len(cName)
Text2.Text = cName
'鼠标右键单击后就停止检测,所以显示的就是单击处的句柄和类名
If GetAsyncKeyState(VK_RBUTTON) Then
Timer1.Enabled = False
End If
End Sub








TAG:
评分(