用sendkeys发送text不稳定 有时候显示乱码
text1.text="a17"
SendKeys frmMain.Text1.Text
怎么在其他窗口有的时候显示的是“a!&”,有时候显示"a17"
[解决办法]
本帖最后由 bcrun 于 2013-08-19 10:04:07 编辑
Option Explicit
Private Const WM_CHAR = &H102
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202
Private Const WM_KEYDOWN = &H100
Private Const WM_KEYUP = &H101
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
'将一个字符串通过PostMessage传送出去
Private Sub subPostString(ByVal lngHandle As Long, ByVal strP As String)
Dim intP As Integer
Dim intK As Integer
Dim lngAsc As Long
Dim lngP As Long
Dim strT As String
On Error GoTo errSub
For intP = 0 To Len(strP) - 1
strT = Mid(strP, intP + 1, 1)
If Asc(strT) <= 0 Then
lngAsc = AscW(StrConv(strT, 128))
If lngAsc < 0 Then
lngAsc = lngAsc + 65536 '2^16
End If
Else
lngAsc = Asc(strT)
End If
lngP = PostMessage(lngHandle, WM_CHAR, lngAsc, 0)
Next intP
Exit Sub
errSub:
End Sub
Private Sub Command1_Click()
Call subPostString(Text1.hwnd, "a17") '将字符串发送给输入文本框
End Sub