欢迎光临

木子屋

标题: 使用FindWindowEx获取多个窗体示例

作者: dnawo

日期: 2009-09-02 07:24 PM

分类: Win编程

内容: 下边例子获取所有打开的记事本程序，并写入字符串"mzwu.com"：
Module1.bas：
复制内容到剪贴板 程序代码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
Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long
Public Const WM_KEYDOWN = &H100
Public Const WM_KEYUP = &H101
Public Const WM_CHAR = &H102
'虚拟码转为扫描码
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
form1代码：
复制内容到剪贴板 程序代码Private Sub Form_Load()
Dim notepad, edit As Long

notepad = FindWindowEx(0, 0, "Notepad", vbNullString) '获取记事本句柄

Do While (notepad > 0)
edit = FindWindowEx(notepad, 0, "Edit", vbNullString) '获取记事本的输入框句柄

PostMessage edit, WM_CHAR, Asc("m"), MakeKeyLparam(Asc("m"), WM_KEYDOWN) '输入m
PostMessage edit, WM_CHAR, Asc("z"), MakeKeyLparam(Asc("z"), WM_KEYDOWN) '输入z
PostMessage edit, WM_CHAR, Asc("w"), MakeKeyLparam(Asc("w"), WM_KEYDOWN) '输入w
PostMessage edit, WM_CHAR, Asc("u"), MakeKeyLparam(Asc("u"), WM_KEYDOWN) '输入u
PostMessage edit, WM_CHAR, Asc("."), MakeKeyLparam(Asc("."), WM_KEYDOWN) '输入.
PostMessage edit, WM_CHAR, Asc("c"), MakeKeyLparam(Asc("c"), WM_KEYDOWN) '输入c
PostMessage edit, WM_CHAR, Asc("o"), MakeKeyLparam(Asc("o"), WM_KEYDOWN) '输入o
PostMessage edit, WM_CHAR, Asc("m"), MakeKeyLparam(Asc("m"), WM_KEYDOWN) '输入m

notepad = FindWindowEx(0, notepad, "Notepad", vbNullString) '获取下一个记事本句柄
Loop
End Sub

+ 查看当前日志评论 (0)

登录

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 1.021 ms

返回

标题: 使用FindWindowEx获取多个窗体示例

你没有权限发表评论

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 1.021 ms

返回

暂无评论

返回

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 1.069 ms

返回