我已经得到窗口上句按键的句柄,怎么用SENDMESSAGE,发送回车,赐教
2个回答
展开全部
先贴一下sendmessage 的代码(经VB6.0测试通过,直接复制套用就可以)
'先声明调用API
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
SendMessage hWnd,VbKeyReturn,0,0
个人不用SendMessage,觉得PostMessage比较实在
那个那个。。如果是后台的话建议用postmessage,我把自动生成扫描码的函数也发给你,全都复制在窗体代码里就可以。这只是比较简单的雏形,如果你是针对游戏的话,有些游戏对于发送回车键会比较麻烦,不是的话就不用多花时间啦。
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 Const WM_KEYDOWN = &H100'键码声明
Private Const WM_KEYUP = &H101
PostMessage chwnd, WM_KEYDOWN, vbKeyReturn, MakeKeyLparam(vbKeyReturn, WM_KEYDOWN)
Private Function MakeKeyLparam(ByVal VirtualKey As Long, ByVal flag As Long) As Long
Dim s As String
Dim Firstbyte As String 'lparam参数的24-31位
If flag = WM_KEYDOWN Then '如果是按下键
Firstbyte = "00"
Else
Firstbyte = "C0" '如果是释放键
End If
Dim Scancode As Long '获得键的扫描码
Scancode = MapVirtualKey(VirtualKey, 0)
Dim Secondbyte As String 'lparam参数的16-23位,即虚拟键扫描码
Secondbyte = Right("00" & Hex(Scancode), 2)
s = Firstbyte & Secondbyte & "0001" '0001为lparam参数的0-15位,即发送次数和其它扩展信息
MakeKeyLparam = Val("&H" & s)
End Function
'先声明调用API
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
SendMessage hWnd,VbKeyReturn,0,0
个人不用SendMessage,觉得PostMessage比较实在
那个那个。。如果是后台的话建议用postmessage,我把自动生成扫描码的函数也发给你,全都复制在窗体代码里就可以。这只是比较简单的雏形,如果你是针对游戏的话,有些游戏对于发送回车键会比较麻烦,不是的话就不用多花时间啦。
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 Const WM_KEYDOWN = &H100'键码声明
Private Const WM_KEYUP = &H101
PostMessage chwnd, WM_KEYDOWN, vbKeyReturn, MakeKeyLparam(vbKeyReturn, WM_KEYDOWN)
Private Function MakeKeyLparam(ByVal VirtualKey As Long, ByVal flag As Long) As Long
Dim s As String
Dim Firstbyte As String 'lparam参数的24-31位
If flag = WM_KEYDOWN Then '如果是按下键
Firstbyte = "00"
Else
Firstbyte = "C0" '如果是释放键
End If
Dim Scancode As Long '获得键的扫描码
Scancode = MapVirtualKey(VirtualKey, 0)
Dim Secondbyte As String 'lparam参数的16-23位,即虚拟键扫描码
Secondbyte = Right("00" & Hex(Scancode), 2)
s = Firstbyte & Secondbyte & "0001" '0001为lparam参数的0-15位,即发送次数和其它扩展信息
MakeKeyLparam = Val("&H" & s)
End Function
展开全部
如果是一个按钮的话,那么直接发送一个点击的消息即可:
SendMessage(hWnd, BM_CLICK, 0, 0);
如果一定要发送 Enter 消息,可以这样:
SendMessage(hWnd, WM_KEYDOWN, VK_RETURN, 0);
SendMessage(hWnd, WM_KEYUP, VK_RETURN, 0);
hWnd 代表按钮的句柄。
SendMessage(hWnd, BM_CLICK, 0, 0);
如果一定要发送 Enter 消息,可以这样:
SendMessage(hWnd, WM_KEYDOWN, VK_RETURN, 0);
SendMessage(hWnd, WM_KEYUP, VK_RETURN, 0);
hWnd 代表按钮的句柄。
追问
SendMessage(hWnd, BM_CLICK, 0, 0);是什么意思?
追答
BM_CLICK 是按钮(Button Message)点击消息,相当于鼠标左键在按钮上点击一下。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询