vb的postmessage函数的使用,完全没反应,想破脑袋也没想通 150
我是vb的初学者,遇到需要用到在指定窗口指定坐标鼠标单击,就跟按键精灵里说的后台单击一样的意思好吧,我百度了以后了解到需要用postmessage函数,网上贴出的代码看起...
我是vb的初学者,遇到需要用到在指定窗口指定坐标鼠标单击,就
跟按键精灵里说的后台单击一样的意思
好吧,我百度了以后了解到需要用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(hWndHH, wMsg, wParam, lParam)’发送消息
End Sub
我为了能看到效果,把代码里的左键按下抬起改成了右键按下抬起,类名和标题都是用
按键抓抓提取的类名和标题,试了没用后还干脆直接自己用按键抓抓提取句柄直接在
代码里填hwndx=459594,还是没用。
窗口用的是谷歌crome浏览器的窗口,用按键精灵的后台单击试了一下没问题
的会弹出右键菜单。
然后我就不明白了,我都是照做的,哪里有个错误导到致没反应?
谷歌chrome 展开
跟按键精灵里说的后台单击一样的意思
好吧,我百度了以后了解到需要用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(hWndHH, wMsg, wParam, lParam)’发送消息
End Sub
我为了能看到效果,把代码里的左键按下抬起改成了右键按下抬起,类名和标题都是用
按键抓抓提取的类名和标题,试了没用后还干脆直接自己用按键抓抓提取句柄直接在
代码里填hwndx=459594,还是没用。
窗口用的是谷歌crome浏览器的窗口,用按键精灵的后台单击试了一下没问题
的会弹出右键菜单。
然后我就不明白了,我都是照做的,哪里有个错误导到致没反应?
谷歌chrome 展开
3个回答
展开全部
hWndX =
下行加个debug.print hWndX '看看是否为0
另外应该还少一个api函数FindWindowEx
如果只用窗体一个函数就够了,如果还要窗体下的一个控件那还得需要一个api函数,spy++是看控件类型的,就一窗体根本用不了spy++
参考一下以前我写的(看怎么找控件,用的是sendmessage,你的问题就出在这里):
Option Explicit
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 SendMessage Lib "user32 " Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Const WM_SETTEXT = &HC
Private Sub Command1_Click()
Dim s As String, myhwnd As Long
s = Text1
myhwnd = FindWindow(vbNullString, "abc.txt - 记事本")
myhwnd = FindWindowEx(myhwnd, 0&, "Edit", vbNullString) '控件名用spy++查看,这里是Edit,特定的软件控件名都是固定的
If myhwnd = 0 Then
MsgBox "!!"
Exit Sub
End If
SendMessage myhwnd, WM_SETTEXT, 0, ByVal s
End Sub
下行加个debug.print hWndX '看看是否为0
另外应该还少一个api函数FindWindowEx
如果只用窗体一个函数就够了,如果还要窗体下的一个控件那还得需要一个api函数,spy++是看控件类型的,就一窗体根本用不了spy++
参考一下以前我写的(看怎么找控件,用的是sendmessage,你的问题就出在这里):
Option Explicit
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 SendMessage Lib "user32 " Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Const WM_SETTEXT = &HC
Private Sub Command1_Click()
Dim s As String, myhwnd As Long
s = Text1
myhwnd = FindWindow(vbNullString, "abc.txt - 记事本")
myhwnd = FindWindowEx(myhwnd, 0&, "Edit", vbNullString) '控件名用spy++查看,这里是Edit,特定的软件控件名都是固定的
If myhwnd = 0 Then
MsgBox "!!"
Exit Sub
End If
SendMessage myhwnd, WM_SETTEXT, 0, ByVal s
End Sub
追问
你好,你讲的是找句柄的事情,句柄我直接用按键精灵的按键抓抓取到了句柄
直接在代码里填hwndx=459594,这么做还是没用,这个句柄没有取错的,
在按键精灵里用同样的句柄有效果
追答
’api函数你申明一下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 Sub Command1_Click()
Dim lpClassName As String, lpWindowName As String, hWndX As Long
lpClassName = "Edit"
lpWindowName = "123.txt - 记事本"
hWndX = FindWindow(vbNullString, lpWindowName)
hWndX = FindWindowEx(hWndX, 0&, lpClassName, vbNullString)
If hWndX > 0 Then
Dim x, y
x = 100: y = 100
Call PostMessage(hWndX, WM_RBUTTONDOWN, 0, (x And &HFFFF) + (y And &HFFFF) * &H10000) '右键down
Call PostMessage(hWndX, WM_RBUTTONUP, 0, (x And &HFFFF) + (y And &HFFFF) * &H10000) '右键up
End If
End Sub
展开全部
你的代码是不是这样写的:
wMsg = WM_RBUTTONDOWN'右键按下消息
wParam = 1
lParam =yy*65536+xx
Call PostMessage(hWndX, wMsg, wParam, lParam)’发送消息
Sleep 300
wMsg =WM_RBUTTONUP'右键抬起消息
wMsg = WM_RBUTTONDOWN'右键按下消息
wParam = 1
lParam =yy*65536+xx
Call PostMessage(hWndX, wMsg, wParam, lParam)’发送消息
Sleep 300
wMsg =WM_RBUTTONUP'右键抬起消息
更多追问追答
追问
这一段是这样的,一模一样
追答
另外有些程序是屏蔽了这些操作的,我建议你把那个浏览器换成windows自带的记事本试试看
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
初学者就折腾这些个啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询