在VBA中已获得另一窗体的ListBox句柄后,如何才能继续得到item信息?
在VBA环境中我已获得某一窗体的句柄等, 需要继续获取ListBox的item信息, 该如何做?请见图及相关程序
[img=http://][/img]C:\Documents and Settings\Administrator\My Documents\My Pictures\如何利用句柄获得listbox中的item信息.jpg
Public Function GetListBoxHwnd() As Long
'Declare local variables
Dim lngDeskTopHandle As Long 'Used to hold the value of the parent handle.
Dim lngHand As Long 'Used to hold each windows handle as it loops.
Dim strName As String * 255
'Get the handle for the UserForm
lngDeskTopHandle = FindWindow(vbNullString, Me.Caption)
GW_CHILD = 5
'Get the first child of the UserForm window.
lngHand = GetWindow(lngDeskTopHandle, GW_CHILD)
Do While lngHand <> 0
'Get the title of the next window in the window list.
GetWindowText lngHand, strName, Len(strName)
'Get the down level of the current window.
lngHand = GetWindow(lngHand, GW_CHILD)
Debug.Print VBA.Left$(strName, InStr(1, strName, vbNullChar))
If lngHand = 0 Then Exit Function
GetListBoxHwnd = lngHand
Loop
End Function
Private Sub CommandButton1_Click()
Dim item() As String, i As Long
hWnd = FindWindow(vbNullString, "UserForm1")
lstID = GetCurrentProcessId
childWnd = GetListBoxHwnd ' FindWindowEx(hwnd, 0, "ListControl", vbNullString)
lstID2 = GetCurrentProcessId
UserForm1.TextBox1.Text = hWnd & "的ListBox号=" & childWnd
UserForm1.TextBox2.Text = "进程ID:" & lstID & ", " & lstID2
' 获得列表框中的内容
GetListItem childWnd, item()
For i = 0 To UBound(item)
Debug.Print item(i)
Next i
End Sub
Private Sub UserForm_Initialize()
With ListBox1
.BoundColumn = 1
.ColumnCount = 1
.Font.Size = 13
.AddItem "1" & vbTab & "1:12.31" & vbTab & "1:12.31" & vbTab & "DNF", 0
.AddItem "2" & vbTab & "1:12.37" & vbTab & "1:12.37" & vbTab & "DNF", 1
End With
End Sub
Sub GetListItem(ByVal childWnd As Long, ByRef item() As String)
Dim Count As Long, idx As Long, i As Long
Count = SendMessage(childWnd, LB_GETCOUNT, ByVal 0, ByVal 0)
For i = 0 To Count - 1
ReDim Preserve item(i)
item(i) = String(255, Chr(0))
SendMessage hWnd, LB_GETTEXT, ByVal i, ByVal item(i)
item(i) = Left(item(i), InStr(item(i), Chr(0)) - 1)
Next i
End Sub
' 见笑了, 我仅能给 30分
补充:VB , API