Private Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
strSend = StrConv(strSend, vbFromUnicode)
for i=1 to lenb(strSend)
PostMessage lngHwnd, WM_CHAR, AscB(MidB(strSend, iPos2, 1)), 0
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 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 Sub Command1_Click()
Dim s As String
Dim FT_hWnd As Long
Dim ID_Control_hWnd As Long
s = "test" & vbCrLf & "测试"
FT_hWnd = FindWindow("Notepad", vbNullString)
If FT_hWnd = 0 Then
MsgBox "False", vbInformation, "Title"
End
End If
ID_Control_hWnd = GetDlgItem(FT_hWnd, &HF)
SendMessage ID_Control_hWnd, WM_SETTEXT, 0, ByVal s
End Sub
当用SendMessage发送WM_SETTEXT消息时,wParam未使用,所以填0即可,没必要写成LenB(StrConv(s, vbFromUnicode))这么复杂,lParam参数才是需要设置的文本信息。
你一定知道VB中字符串是用Unicode形式存放的,所以你想把它转成ANSI字符串,wParam参数才写成LenB(StrConv(s, vbFromUnicode))这么复杂,但最重要的lParam参数确没对字符串s做任何处理。注意从VB的API Viewer中复制出来的SendMessage的lParam参数是"lParam As Any",所以你在这个参数上直接写s是把s字符串的地址传给了lParam,而且传入的这个地址还是VarPtr(s)而不是StrPtr(s),其实在VB中,对字符串只需要"ByVal 字符串名"就能将Unicode字符串转成ANSI字符串并将字符串指针传递过去,所以我在lParam参数上只用了"ByVal s"就可以解决问题。