先贴一下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
如果是一个按钮的话,那么直接发送一个点击的消息即可:
SendMessage(hWnd, BM_CLICK, 0, 0);
如果一定要发送 Enter 消息,可以这样:
SendMessage(hWnd, WM_KEYDOWN, VK_RETURN, 0);
SendMessage(hWnd, WM_KEYUP, VK_RETURN, 0);
hWnd 代表按钮的句柄。