我写了一个自动写字的程序,如下 
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 可以一切换到别的记事本,原先那个记事本就不行了,而新的记事本又开始自动写了./ 有没有办法让这个程序只能在原先的记事本上写啊,不管怎么切换,缩小,都一直在写,帮帮忙啊!

解决方案 »

  1.   

    如果要实现这样的功能,你的方法是不行的。
    SendKeys是模拟键盘操作,所以对输入焦点敏感。如果要锁定输入,就要用Api取得当前要输入文本的控件句柄,然后用TextOut才行。
      

  2.   

    Option ExplicitPrivate Const GW_HWNDNEXT = 2Private Const GW_HWNDPREV = 3Private Const WM_SETTEXT = &HCPrivate Const WM_GETTEXT = &HDPrivate Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As LongPrivate Declare Function GetDesktopWindow Lib "user32" () As LongPrivate Declare Function GetTopWindow Lib "user32" (ByVal hwnd As Long) As LongPrivate Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wFlag As Long) As LongPrivate Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As LongPrivate Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As LongPrivate Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As LongPrivate Sub Command1_Click()
        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
      

  3.   

    其他版本窗体的树结构可能不一样,配合Spy++用同样的遍历方法也可以找出来
      

  4.   

    使用API
    先找到最先的记事本句柄(很简单吧不用说了吧)
    然后用SENDMESSAGE来发送消息就行了