(本文中部分使用繁体字,如果在简体系统下请将第三部实例程序记事本标题换成相对应的简体名称)
第一部分:
常用的与窗体相关的API函数
Public Declare Function ShowWindow Lib"user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long (根据句柄控制某一窗体显示状态)
Public Declare Function EnableWindow Lib"user32" (ByVal hwnd As Long, ByVal fEnable As Long) As Long(使能窗口事件)
Public Declare Function FindWindow Lib"user32" Alias "FindWindowA" (ByVal lpClassName As String,ByVal lpWindowName As String) As Long(根据标题找主窗口句柄)
Public Declare Function EnumWindows Lib"user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long(找到窗口组)
Public Declare Function FindWindowEx Lib"user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByValhWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long(找子窗口部件)
Public Declare Function GetParent Lib"user32" (ByVal hwnd As Long) As Long
Public Declare Function GetWindow Lib"user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Function GetWindowText Lib"user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVallpString As String, ByVal cch As Long) As Long(获取窗口标题)
Public Declare Function GetClassName Lib"user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVallpClassName As String, ByVal nMaxCount As Long) As Long(获取窗口类)
Public Declare Function SendMessage Lib"user32" Alias "SendMessageA" (ByVal hwnd As Long, ByValwMsg As Long, ByVal wParam As Long, lParam As Any) As Long(发送信息)
Private Declare Function PostMessage Lib"user32" Alias "PostMessageA" (ByVal hwnd As Long, ByValwMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long(发送信息)
Public Declare Sub Sleep Lib "kernel32"(ByVal dwMilliseconds As Long) (延时用)
Public Declare Sub keybd_event Lib"user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags AsLong, ByVal dwExtraInfo As Long)(键盘事件)
第二部分
按键编码
Public Const VK_LBUTTON = &H1
Public Const VK_RBUTTON = &H2
Public Const VK_CANCEL = &H3
Public Const VK_MBUTTON = &H4
Public Const VK_BACK = &H8
Public Const VK_TAB = &H9
Public Const VK_CLEAR = &HC
Public Const VK_RETURN = &HD
Public Const VK_SHIFT = &H10
Public Const VK_CONTROL = &H11
Public Const VK_MENU = &H12
Public Const VK_PAUSE = &H13
Public Const VK_CAPITAL = &H14
Public Const VK_ESCAPE = &H1B
Public Const VK_SPACE = &H20
Public Const VK_PRIOR = &H21
Public Const VK_NEXT = &H22
Public Const VK_END = &H23
Public Const VK_HOME = &H24
Public Const VK_LEFT = &H25
Public Const VK_UP = &H26
Public Const VK_RIGHT = &H27
Public Const VK_DOWN = &H28
Public Const VK_Select = &H29
Public Const VK_PRINT = &H2A
Public Const VK_EXECUTE = &H2B
Public Const VK_SNAPSHOT = &H2C
Public Const VK_Insert = &H2D
Public Const VK_Delete = &H2E
Public Const VK_HELP = &H2F
Public Const VK_0 = &H30
Public Const VK_1 = &H31
Public Const VK_2 = &H32
Public Const VK_3 = &H33
Public Const VK_4 = &H34
Public Const VK_5 = &H35
Public Const VK_6 = &H36
Public Const VK_7 = &H37
Public Const VK_8 = &H38
Public Const VK_9 = &H39
Public Const VK_A = &H41
Public Const VK_B = &H42
Public Const VK_C = &H43
Public Const VK_D = &H44
Public Const VK_E = &H45
Public Const VK_F = &H46
Public Const VK_G = &H47
Public Const VK_H = &H48
Public Const VK_I = &H49
Public Const VK_J = &H4A
Public Const VK_K = &H4B
Public Const VK_L = &H4C
Public Const VK_M = &H4D
Public Const VK_N = &H4E
Public Const VK_O = &H4F
Public Const VK_P = &H50
Public Const VK_Q = &H51
Public Const VK_R = &H52
Public Const VK_S = &H53
Public Const VK_T = &H54
Public Const VK_U = &H55
Public Const VK_V = &H56
Public Const VK_W = &H57
Public Const VK_X = &H58
Public Const VK_Y = &H59
Public Const VK_Z = &H5A
Public Const VK_STARTKEY = &H5B
Public Const VK_CONTEXTKEY = &H5D
Public Const VK_NUMPAD0 = &H60
Public Const VK_NUMPAD1 = &H61
Public Const VK_NUMPAD2 = &H62
Public Const VK_NUMPAD3 = &H63
Public Const VK_NUMPAD4 = &H64
Public Const VK_NUMPAD5 = &H65
Public Const VK_NUMPAD6 = &H66
Public Const VK_NUMPAD7 = &H67
Public Const VK_NUMPAD8 = &H68
Public Const VK_NUMPAD9 = &H69
Public Const VK_MULTIPLY = &H6A
Public Const VK_ADD = &H6B
Public Const VK_SEPARATOR = &H6C
Public Const VK_SUBTRACT = &H6D
Public Const VK_DECIMAL = &H6E
Public Const VK_DIVIDE = &H6F
Public Const VK_F1 = &H70
Public Const VK_F2 = &H71
Public Const VK_F3 = &H72
Public Const VK_F4 = &H73
Public Const VK_F5 = &H74
Public Const VK_F6 = &H75
Public Const VK_F7 = &H76
Public Const VK_F8 = &H77
Public Const VK_F9 = &H78
Public Const VK_F10 = &H79
Public Const VK_F11 = &H7A
Public Const VK_F12 = &H7B
Public Const VK_F13 = &H7C
Public Const VK_F14 = &H7D
Public Const VK_F15 = &H7E
Public Const VK_F16 = &H7F
Public Const VK_F17 = &H80
Public Const VK_F18 = &H81
Public Const VK_F19 = &H82
Public Const VK_F20 = &H83
Public Const VK_F21 = &H84
Public Const VK_F22 = &H85
Public Const VK_F23 = &H86
Public Const VK_F24 = &H87
Public Const VK_NUMLOCK = &H90
Public Const VK_OEM_SCROLL = &H91
Public Const VK_OEM_1 = &HBA
Public Const VK_OEM_PLUS = &HBB
Public Const VK_OEM_COMMA = &HBC
Public Const VK_OEM_MINUS = &HBD
Public Const VK_OEM_PERIOD = &HBE
Public Const VK_OEM_2 = &HBF
Public Const VK_OEM_3 = &HC0
Public Const VK_OEM_4 = &HDB
Public Const VK_OEM_5 = &HDC
Public Const VK_OEM_6 = &HDD
Public Const VK_OEM_7 = &HDE
Public Const VK_OEM_8 = &HDF
Public Const VK_ICO_F17 = &HE0
Public Const VK_ICO_F18 = &HE1
Public Const VK_OEM102 = &HE2
Public Const VK_ICO_HELP = &HE3
Public Const VK_ICO_00 = &HE4
Public Const VK_ICO_CLEAR = &HE6
Public Const VK_OEM_RESET = &HE9
Public Const VK_OEM_JUMP = &HEA
Public Const VK_OEM_PA1 = &HEB
Public Const VK_OEM_PA2 = &HEC
Public Const VK_OEM_PA3 = &HED
Public Const VK_OEM_WSCTRL = &HEE
Public Const VK_OEM_CUSEL = &HEF
Public Const VK_OEM_ATTN = &HF0
Public Const VK_OEM_FINNISH = &HF1
Public Const VK_OEM_COPY = &HF2
Public Const VK_OEM_AUTO = &HF3
Public Const VK_OEM_ENLW = &HF4
Public Const VK_OEM_BACKTAB = &HF5
Public Const VK_ATTN = &HF6
Public Const VK_CRSEL = &HF7
Public Const VK_EXSEL = &HF8
Public Const VK_EREOF = &HF9
Public Const VK_PLAY = &HFA
Public Const VK_ZOOM = &HFB
Public Const VK_NONAME = &HFC
Public Const VK_PA1 = &HFD
Public Const VK_OEM_CLEAR = &HFE
第三部分:
实例打开记事本---并发送信息
(上面的声明放在一个模块中即可)
Option Explicit
Private StartTime As Long
Private Declare Function SendMessage& Lib"user32" Alias "SendMessageA" (ByVal hwnd As Long, ByValwMsg As Long, ByVal wParam As Long, lParam As Any)
Private Declare Function PostMessage Lib"user32" Alias "PostMessageA" (ByVal hwnd As Long, ByValwMsg 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 Const WM_KEYDOWN = &H100
Private Const VK_F1 = &H70
Private Const WM_KEYUP = &H101
Public Sub Delay(mSec As Long)
DimTStart As Single
TStart =Timer
While(Timer - TStart) < (mSec / 1000)
DoEvents
Wend
End Sub
Private Sub Command1_Click()
DimReturnValue
ReturnValue = Shell("notepad.exe", vbNormalFocus) '打开记事本
AppActivate ReturnValue '激活窗口
Dim hwndAs Long
hwnd =FindWindow(vbNullString, "未命名-记事本") '窗口标题
SendKeys"= ", True
SendKeys"I am ok", True
SendKeys"{ENTER}", True
SendKeys "{TAB}", True
PostMessage hwnd, WM_KEYDOWN, VK_RETURN, 0
Delay(1000)
PostMessage hwnd, WM_KEYUP, VK_RETURN, 0
Delay(1000)
SendKeys "5", True
End Sub
Private Sub Command2_Click()
End
End Sub
第四部分
总结
获取句柄的方法
1主窗体(可参考上面的方法)
如果是多个窗体要查找某一个
'用 FindWindowEx 查找第一个,再用找到的窗口句柄作为参数再次调用 FindWindowEx,查找第二个,直到返回 0 为止 '得到句柄后,用 GetWindowText 得到标题,分析它提取出你需要的窗口 Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long 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 dl As Long, winHwnd As Long, nStr As String, nSize As Long nSize = 128 Do winHwnd = FindWindowEx(0, winHwnd, vbNullString, vbNullString) '得到一个窗口句柄 If winHwnd = 0 Then Exit Do nStr = String(nSize, vbNullChar) dl = GetWindowText(winHwnd, nStr, nSize) '得到窗口标题 nStr = Left(nStr, InStr(nStr, vbNullChar) - 1) '去掉后面的无用字符 If LCase(Right(nStr, 5)) = "1.exe" Then MsgBox winHwnd & vbCrLf & nStr '提取出符合条件的标题 Loop
2子窗体(部件),窗体组
`控件需要知道类名可以用spy++查看
private Sub Command1_Click()
Dim Phwnd As Long
Dim ChildHwnd As Long
Phwnd = FindWindow(vbNullString, "计算器")
If Phwnd <> 0 Then
ChildHwnd = FindWindowEx(Phwnd, 0, "Edit", vbNullString)
If ChildHwnd <> 0 Then
SendMessage ChildHwnd, WM_SETTEXT, 0, ByVal "我是计算器"
Else
MsgBox "找不到相关控件"
End If
Else
MsgBox "计算器程序没有运行"
End If
End Sub
发送信息的方法
1--------------
先获取窗口句柄(方法同上),然后用sendmessage发信息
Private Const WM_SETTEXT = &HC 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 GetDlgItem Lib "user32" (ByVal hDlg As Long, ByVal nIDDlgItem As Long) As Long
Dim s As String Dim FT_hWnd As Long Dim ID_Control_hWnd As Long s = "test" & vbCrLf & "找到你了"
ID_Control_hWnd = GetDlgItem(你得到的句柄值, &HF) SendMessage ID_Control_hWnd, WM_SETTEXT, 0, ByVal s
2------------------------------
用 SendKeys 语句
AppActivate "接收键盘事件的窗口标题" ' 激活指定窗口
SendKeys "A", True '发送字符 A
' SendKeys "I am ok", True
' SendKeys "{ENTER}", True
' PostMessage hwnd, WM_KEYDOWN, VK_RETURN, 0
' Delay (1000)
' PostMessage hwnd, WM_KEYUP, VK_RETURN, 0
' Delay (1000)