求postmessageapi的一个例子

西山剑客 2008-07-14 11:26:53
百分求一函数,使该函数一旦调用,就会自动向title为"记事本模拟"的后台窗口发送按键三下,一个数字键1,一个功能键f1,一个切换键Tab。
明文要求:该函数能在后台运行,并且是给后台窗口发送的按键信号。必须亲自调试成功后才发过来,不要想当然。很多postmessage 这个api的例子是无效的。一经测试成功,立即送分!本人每天来此看两到三次。
...全文
115 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
西山剑客 2008-08-09
  • 打赏
  • 举报
回复
谢谢这位朋友,只是最近太忙了,一直没上这个论坛,今天收到email提醒,才来揭贴,至欠。
true_guowei 2008-08-01
  • 打赏
  • 举报
回复
楼主不结贴,貌似是骗子?
true_guowei 2008-08-01
  • 打赏
  • 举报
回复
分让楼上的抢走了,:)
代码能用
danielinbiti 2008-07-15
  • 打赏
  • 举报
回复
'module
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
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
Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wFlag As Long) As Long
Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Public Const WM_CHAR = &H102
Public hndl As Long
Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Dim sSave As String, Ret As Long
Ret = GetWindowTextLength(hwnd)
sSave = Space(Ret)
GetWindowText hwnd, sSave, Ret + 1
If Trim(sSave) = "无标题 - 记事本" Then
hndl = hwnd
EnumWindowsProc = False
Else
EnumWindowsProc = True
End If
End Function


'form
Option Explicit
Sub sendCharToNotePad()
Dim hwnd As Long
Dim lpClassName As String
Dim nullpos As Long
EnumWindows AddressOf EnumWindowsProc, ByVal 0&
hwnd = GetNextWindow(hndl, 5)
Do While hwnd <> 0
lpClassName = Space(256)
Call GetClassName(hwnd, lpClassName, 256)
nullpos = InStr(lpClassName, Chr$(0))
If nullpos > 0 Then
lpClassName = Left$(lpClassName, nullpos - 1)
End If
If (Trim(lpClassName) = "Edit") Then '往记事本写内容
Exit Do
End If
hwnd = GetNextWindow(hwnd, 2)
Loop
Call PostMessage(hwnd, WM_CHAR, vbKey1, 0) '如果发送频繁,发送完sleep,另外postmessage没有sendmessage效果好,postmessage会放到消息发送队列,可能被冲掉的,如果要模拟按键,则改WM_CHAR参数
Call PostMessage(hwnd, WM_CHAR, vbKeyF1, 0)
Call PostMessage(hwnd, WM_CHAR, vbKeyTab, 0)
End Sub
Private Sub Command1_Click()
Call sendCharToNotePad
End Sub
silencenet 2008-07-15
  • 打赏
  • 举报
回复
将例子中的PostMessage函数的参数变量定义为模块级试试

1,486

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧