vb中PostMessage

该函数将一个消息放入(寄送)到与指定窗口创建的线程相联系消息队列里,不等待线程处理消息就返回。消息队列里的消息通过调用GetMessage和PeekMessage取得。
    函数原型:B00L PostMessage(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam);
    参数
    hWnd:其窗口程序接收消息的窗口的句柄。可取有特定含义的两个值:
    HWND.BROADCAST:消息被寄送到系统的所有顶层窗口,包括无效或不可见的非自身拥有的窗口、被覆盖的窗口和弹出式窗口。消息不被寄送到子窗口。
    NULL:此函数的操作和调用参数dwThread设置为当前线程的标识符PostThreadMessage函数一样。
    Msg:指定被寄送的消息。
    wParam:指定附加的消息特定的信息。
    IParam:指定附加的消息特定的信息。
    返回值:如果函数调用成功,返回非零值:如果函数调用失败,返回值是零。若想获得更多的错误信息,请调用GetLastError函数。

例:游戏挂机,如果一个简单的游戏挂机只需要点击鼠标就可以,那我们就可以用VB做一个简单的外挂,用SetCursorPos()等函数把鼠标移动到指定的位置上点击,但这样的话,游戏界面就一定要在前台,但如果用postmessage()函数的话,就算是游戏已经最小化了,仍然可以将鼠标点击事件发送到游戏中,这就是postmessage的好处!试试下面的例子:
'API 声明 
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long 
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long 
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 
'常量声明 
Private Const WM_LBUTTONDBLCLK = &H203 
Private Const WM_LBUTTONDOWN = &H201 
Private Const WM_LBUTTONUP = &H202 
Private Const WM_MBUTTONDBLCLK = &H209 
Private Const WM_MBUTTONDOWN = &H207 
Private Const WM_MBUTTONUP = &H208 
Private Const WM_RBUTTONDBLCLK = &H206 
Private Const WM_RBUTTONDOWN = &H204 
Private Const WM_RBUTTONUP = &H205 
Private Sub Command1_Click() 
Dim lpClassName As String, lpWindowName As String,hWndX as Long 
lpClassName = "这里填写游戏窗口的类名"’用VB企业版自带的SPY++工具可以查看游戏窗口的类名和标题 
lpWindowName = "这里填写游戏窗口的标题" 
hWndX = FindWindow(lpClassName, lpWindowName)'这一步获得游戏窗口的句柄,发送消息时需要 
Dim wMsg As Long, wParam As Long, lParam As Long, Rx As Long,xx as Integer,yy as Integer 
xx=100'点击的x坐标 
yy=100'点击的y坐标 
wMsg = WM_LBUTTONDOWN'左键按下消息 
wParam = 1 
lParam =yy*65536+xx 
Call PostMessage(hWndX, wMsg, wParam, lParam)’发送消息 
Sleep 300 
wMsg = WM_LBUTTONUP'左键抬起消息 
wParam = 1 
lParam = yy*65536+xx 
Call PostMessage(hWndX, wMsg, wParam, lParam)’发送消息 
End Sub
这个程序目的是实现游戏里的鼠标单击~~~~~~比如100.100位置

你可能感兴趣的:(游戏,vb)