主题:刚刚找到一段代码,用SendMessage得到ListBox类里面的内容
在vb里面如何得到外部程序的ListBox内容
以下代码掩饰在得知两个ListBox句柄时,把其中一个里面的内容复制到另外一个里面
DuplicateListBox参数解释:
SourceHwnd=源ListBox句柄(HWnd)
TargetHwnd=目标ListBox句柄(HWnd)
AppendMode=是否对源ListBox的Content进行校验,True就可以了
========================================================================
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 LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) _
As Long
Const LB_RESETCONTENT = &H184
Const LB_GETCOUNT = &H18B
Const LB_GETTEXT = &H189
Const LB_ADDSTRING = &H180
Const LB_GETITEMDATA = &H199
Const LB_SETITEMDATA = &H19A
Sub DuplicateListBox(SourceHwnd As Long, TargetHwnd As Long, _
Optional AppendMode As Boolean)
Dim index As Long
Dim itmData As Long
Dim numItems As Long
Dim sItemText As String
' prepare the receiving buffer
sItemText = Space$(512)
' temporarily prevent updating
LockWindowUpdate TargetHwnd
' reset target contents, if not in append mode
If Not AppendMode Then
SendMessage TargetHwnd, LB_RESETCONTENT, 0, ByVal 0&
End If
' get the number of items in the source list
numItems = SendMessage(SourceHwnd, LB_GETCOUNT, 0&, ByVal 0&)
For index = 0 To numItems - 1
' get the item text
SendMessage SourceHwnd, LB_GETTEXT, index, ByVal sItemText
' get the item data
itmData = SendMessage(SourceHwnd, LB_GETITEMDATA, index, ByVal 0&)
' add the item text to the target list
SendMessage TargetHwnd, LB_ADDSTRING, 0&, ByVal sItemText
' add the item data to the target list
SendMessage TargetHwnd, LB_SETITEMDATA, index, ByVal itmData
Next
' allow redrawing
LockWindowUpdate 0
End Sub
Private Sub Form_Load()
DuplicateListBox List2.hWnd, List1.hWnd, True
End Sub
===========================================================================
以下代码掩饰在得知两个ListBox句柄时,把其中一个里面的内容复制到另外一个里面
DuplicateListBox参数解释:
SourceHwnd=源ListBox句柄(HWnd)
TargetHwnd=目标ListBox句柄(HWnd)
AppendMode=是否对源ListBox的Content进行校验,True就可以了
========================================================================
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 LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) _
As Long
Const LB_RESETCONTENT = &H184
Const LB_GETCOUNT = &H18B
Const LB_GETTEXT = &H189
Const LB_ADDSTRING = &H180
Const LB_GETITEMDATA = &H199
Const LB_SETITEMDATA = &H19A
Sub DuplicateListBox(SourceHwnd As Long, TargetHwnd As Long, _
Optional AppendMode As Boolean)
Dim index As Long
Dim itmData As Long
Dim numItems As Long
Dim sItemText As String
' prepare the receiving buffer
sItemText = Space$(512)
' temporarily prevent updating
LockWindowUpdate TargetHwnd
' reset target contents, if not in append mode
If Not AppendMode Then
SendMessage TargetHwnd, LB_RESETCONTENT, 0, ByVal 0&
End If
' get the number of items in the source list
numItems = SendMessage(SourceHwnd, LB_GETCOUNT, 0&, ByVal 0&)
For index = 0 To numItems - 1
' get the item text
SendMessage SourceHwnd, LB_GETTEXT, index, ByVal sItemText
' get the item data
itmData = SendMessage(SourceHwnd, LB_GETITEMDATA, index, ByVal 0&)
' add the item text to the target list
SendMessage TargetHwnd, LB_ADDSTRING, 0&, ByVal sItemText
' add the item data to the target list
SendMessage TargetHwnd, LB_SETITEMDATA, index, ByVal itmData
Next
' allow redrawing
LockWindowUpdate 0
End Sub
Private Sub Form_Load()
DuplicateListBox List2.hWnd, List1.hWnd, True
End Sub
===========================================================================