VB用API读取LISTBOX中的每一项的内容和项的总数.

2025-02-25 14:54:06
推荐回答(5个)
回答1:

我正好有原码:

使用很简单:

运行后将鼠标移到你要的窗口,如果是一个Listbox,它的内容将被复制到程序的List2控件。

http://www.mjbox.com/r/ww/wwccwwccwc/GetFormAndList.rar

回答2:

VB API读取LISTBOX

Private Const LB_GETTEXT = &H189
Private Const LB_GETCOUNT = &H18B

Private Declare Function SendMessageBynum& Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long)
Private Declare Function SendMessageByString& Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String)

Private Sub Command1_Click()
Dim i As Long
Dim count As Long
Dim s As String
Dim sztext As String

count = SendMessageBynum&(List1.hwnd, LB_GETCOUNT, 0&, 0&)

Label1.Caption = count
For i = 0 To count - 1
s = String$(255, 0)
SendMessageByString& List1.hwnd, LB_GETTEXT, i, s
s = Left$(s, InStr(1, s, Chr$(0)) - 1)

sztext = sztext & s & vbCrLf
Next i

Text1.Text = sztext

End Sub

回答3:

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
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 Const LB_GETCOUNT = &H18B
Private Sub Command1_Click()
Dim WinWnd As Long
WinWnd = FindWindow(vbNullString, "Form1") '#Form1是对方应用程序的标题
'MsgBox WinWnd
If WinWnd <> 0 Then
hWnd2 = FindWindowEx(WinWnd, 0, "ThunderRT6ListBox", vbNullString)
If hWnd2 Then
ItemCount = SendMessage(hWnd2, LB_GETCOUNT, 0, 0)
MsgBox ItemCount
End If
End If
End Sub

回答4:

for i=0 to list1.listcount-1
list1.listindex=i
msgbox list1.text
next

没有必要API

回答5:

我的个神哪,,这么简单的问题还用API???
你这不是明摆着要把简单问题复杂化嘛 = =

自己看一下listbox控件的属性方法吧。