用sendmessage向指定外部控件发送文本消息的问题
DimsAsStringFT_hWnd=FindWindow(vbNullString,"Snow_Win32API中文帮助(未注册)")IfFT_hWnd=0ThenM...
Dim s As String
FT_hWnd = FindWindow(vbNullString, "Snow_Win32API 中文帮助(未注册)")
If FT_hWnd = 0 Then
MsgBox "False", vbInformation, "Title"
End
End If
ID_Control_hWnd = GetDlgItem(FT_hWnd, &H15)
SendMessage ID_Control_hWnd, WM_SETTEXT, LenB(StrConv(s, vbFromUnicode)), s
最后显示出来的却是乱码.该怎么解决? 展开
FT_hWnd = FindWindow(vbNullString, "Snow_Win32API 中文帮助(未注册)")
If FT_hWnd = 0 Then
MsgBox "False", vbInformation, "Title"
End
End If
ID_Control_hWnd = GetDlgItem(FT_hWnd, &H15)
SendMessage ID_Control_hWnd, WM_SETTEXT, LenB(StrConv(s, vbFromUnicode)), s
最后显示出来的却是乱码.该怎么解决? 展开
2个回答
展开全部
Option Explicit
Private Const WM_SETTEXT = &HC
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"就可以解决问题。
我这段代码是针对记事本程序的,你再改成你的对应窗体就可以了。
Private Const WM_SETTEXT = &HC
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"就可以解决问题。
我这段代码是针对记事本程序的,你再改成你的对应窗体就可以了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询