我写了一个自动写字的程序,如下
Private Sub Form_Load()
ReturnValue2 = Shell("C:\WINDOWS\NOTEPAD.exe", 1)
AppActivate ReturnValue2, True
End Sub Private Sub Timer1_Timer()
SendKeys "{A}", True
End Sub 可以一切换到别的记事本,原先那个记事本就不行了,而新的记事本又开始自动写了./ 有没有办法让这个程序只能在原先的记事本上写啊,不管怎么切换,缩小,都一直在写,帮帮忙啊!
Private Sub Form_Load()
ReturnValue2 = Shell("C:\WINDOWS\NOTEPAD.exe", 1)
AppActivate ReturnValue2, True
End Sub Private Sub Timer1_Timer()
SendKeys "{A}", True
End Sub 可以一切换到别的记事本,原先那个记事本就不行了,而新的记事本又开始自动写了./ 有没有办法让这个程序只能在原先的记事本上写啊,不管怎么切换,缩小,都一直在写,帮帮忙啊!
SendKeys是模拟键盘操作,所以对输入焦点敏感。如果要锁定输入,就要用Api取得当前要输入文本的控件句柄,然后用TextOut才行。
Dim nTargetProcID As Long
Command1.Enabled = False
nTargetProcID = Shell("C:\WINDOWS\NOTEPAD.exe", 1)
Timer1.Tag = nTargetProcID
Timer1.Enabled = True
End SubPrivate Sub Command2_Click()
Timer1.Enabled = False
Timer1.Tag = ""
Command1.Enabled = True
End SubPrivate Sub Form_Load()
Timer1.Enabled = False
Timer1.Interval = 2000
End SubPrivate Sub Timer1_Timer()
Dim hwnd As Long hwnd = GetWindowHandle(Val(Timer1.Tag))
If Not hwnd = 0 Then
SetForegroundWindow hwnd
SendKeys "{A}", True
End If
End SubPrivate Function GetWindowHandle(ByVal nTargetProcID As Long) As Long
Dim hWndDesktop As Long
Dim hwnd As Long
Dim hWndNext As Long
Dim nProcID As Long
Dim sTitle As String
GetWindowHandle = 0
hWndDesktop = GetDesktopWindow
hwnd = GetTopWindow(hWndDesktop)
Do
GetWindowThreadProcessId hwnd, nProcID
If nProcID = nTargetProcID Then
sTitle = String(512, vbNullChar)
GetWindowText hwnd, sTitle, 1024
sTitle = Left(sTitle, InStr(sTitle, vbNullChar) - 1)
If Right(sTitle, 3) = "记事本" Then
GetWindowHandle = GetTopWindow(hwnd)
Exit Function
End If
End If
hWndNext = GetNextWindow(hwnd, GW_HWNDNEXT)
If hWndNext = 0 Then Exit Do
hwnd = hWndNext
Loop
End Function比较丑陋,勉强可用,调试过了WinXP SP2
先找到最先的记事本句柄(很简单吧不用说了吧)
然后用SENDMESSAGE来发送消息就行了