vb6中 按鍵的問題!
新建壹個窗體,添加壹個command和壹個text框並粘貼以下代碼
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetDlgItem Lib "user32" (ByVal hDlg As Long, ByVal nIDDlgItem As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long
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 Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Const WM_SETTEXT = &HC
Private Const WM_KEYUP = &H101
Private Const WM_KEYDOWN = &H100
Private Const SW_SHOWNORMAL = 1
Private Sub Command1_Click()
Static i As Integer
Dim hwn As Long
Dim twn As Long
If i = 0 Then '運行計算器
Text1.Text = "運行計算器"
hwn = FindWindow(vbNullString, "計算器") '查找窗體名稱為'計算器'的句柄
If hwn = 0 Then
Shell "calc.exe"
Else
ShowWindow hwn, SW_SHOWNORMAL '激活窗口
SetForegroundWindow hwn
End If
End If
If i = 1 Then '向計算器文本框發送文本'5ZqweSC'
Text1.Text = "向計算器文本框發送文本'5ZqweSC'"
hwn = FindWindow(vbNullString, "計算器")
twn = GetDlgItem(hwn, 403)
SendMessage twn, WM_SETTEXT, 0, "5ZqweSC"
End If
If i = 2 Then '計算器模擬按鍵Esc
Text1.Text = "計算器模擬按鍵Esc"
hwn = FindWindow(vbNullString, "計算器")
PostMessage hwn, WM_KEYDOWN, vbKeyEscape, 0
End If
If i = 3 Then '計算器模擬按鍵5
Text1.Text = "計算器模擬按鍵5"
hwn = FindWindow(vbNullString, "計算器")
PostMessage hwn, WM_KEYDOWN, vbKey5, 0
End If
If i = 4 Then '計算器模擬按鍵+
Text1.Text = "計算器模擬按鍵+"
hwn = FindWindow(vbNullString, "計算器")
PostMessage hwn, WM_KEYDOWN, vbKeyAdd, 0
End If
If i = 5 Then '計算器模擬按鍵7
Text1.Text = "計算器模擬按鍵7"
hwn = FindWindow(vbNullString, "計算器")
PostMessage hwn, WM_KEYDOWN, vbKey7, 0
End If
If i = 6 Then '計算器模擬按鍵Enter
Text1.Text = "計算器模擬按鍵回車"
hwn = FindWindow(vbNullString, "計算器")
PostMessage hwn, WM_KEYDOWN, vbKeyReturn, 0
End If
i = i + 1
If i = 7 Then i = 0
End Sub