Страница: 1 |
|
Вопрос: LVM_GETITEMCOUNT помогите плиз
|
Добавлено: 12.03.09 00:26
|
|
Автор вопроса: dimon
|
Нужно получить список пунктов из чужого ListView"а и текст выделенного пункта.
Нашел несколько примеров, для диспетчера задач, раб. стола.
Скопировал, пришлось заменить
Public Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" _
(ByVal hwndParent As Long, _
ByVal hwndChildAfter As Long, _
ByVal lpszClass As Any, _ на stringByVal lpszWindow As Any) As Long на string
Any VB2005 не принимает.
Public Class Form1
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Public Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" _
(ByVal hwndParent As Long, _
ByVal hwndChildAfter As Long, _
ByVal lpszClass As String, _
ByVal lpszWindow As String) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim SLV As Long ' хэндл SysListView32
Dim col As Long
Dim IconCount As Integer
Const LVM_GETITEMCOUNT As Long = 4100 ' получаем количество иконнок
SLV = FindWindow("Progman", "Program Manager") 'ищем окно класса Progman
SLV = FindWindowEx(SLV, vbNull, "SHELLDLL_DefView", "") 'внутри него ищем SHELLDLL_DefView
SLV = FindWindowEx(SLV, vbNull, "SysListView32", "") 'И наконец получаем хэндл нашего SysListView32
IconCount = SendMessage(SLV, LVM_GETITEMCOUNT, 0, 0) ' получаем количество итемов... здесь прога стопорится, Integer не вмещает кол-во итемов, если сделать Dim IconCount As double или long, IconCount"у присваивается значение SLV- явно не то что надо.
Здесь неверно определяется хэндл SysListView32? или
SendMessage неправильно посылается?
End Sub
End Class
Ответить
|
Номер ответа: 6 Автор ответа: dimon
Вопросов: 6 Ответов: 23
|
Профиль | | #6
|
Добавлено: 12.03.09 15:57
|
Писал в VBA Ворд. Если надо всё ещё скопирую
Скопируй плиз, он с APIфункциями так же как .net работает?
Ответить
|
Номер ответа: 8 Автор ответа: GDK
Вопросов: 13 Ответов: 348
|
Профиль | | #8
|
Добавлено: 12.03.09 16:59
|
Ну смотри.
Public Function InitObjectGN(HWndGNp As Long) 'Перепиши "под себя" эту функцию
Её целью было получить хендл родительского окна, обнаружить хендл моего SysListView32 и занести это в глобальные переменные объекта.
Dim HWndGNm As Long - Туда надо будет внести хендл родительского окошка
Dim HWndObjm As Long - А сюда - хендл окна вашего SysListView32.
.нет - ваще не знаю разбирайся сам(сори за тупые ответы).
Будут вопросы - не поленюсь ответить. Если замучаешься ждать - пиши в почту, постараюсь поспешить. В .нет редко заглядываю.
Да, раз говоришь что не соображаешь - адресные пространства твоей проги и чужой разные. Это надо учитывать.
В "моём" классе смотри Public Property Get GetItemText(ItemIndex As Long, ColumnIndex As Long) As String
-
- Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
-
-
-
-
-
- Private Const WM_COMMAND = &H111
- Private Const LVM_FIRST As Long = &H1000
- Private Const LVM_GETHOTITEM As Long = (LVM_FIRST + 61)
- Private Const LVM_GETITEMCOUNT As Long = (LVM_FIRST + 4)
- Private Const LVM_GETITEMSTATE As Long = (LVM_FIRST + 44)
- Private Const LVM_GETSELECTEDCOUNT As Long = (LVM_FIRST + 50)
- Private Const LVM_GETSELECTIONMARK As Long = (LVM_FIRST + 66)
- Private Const LVM_GETTOPINDEX As Long = (LVM_FIRST + 39)
- Private Const LVM_GETITEMTEXTA As Long = (LVM_FIRST + 45)
- Private Const LVM_GETITEMTEXTW As Long = (LVM_FIRST + 115)
- Private Const LVM_GETITEMRECT As Long = (LVM_FIRST + 14)
-
- Private Const LVIS_SELECTED As Long = &H2
- Private Const LVIF_TEXT As Long = &H1
-
- Private Const WM_LBUTTONDOWN As Long = &H201
- Private Const WM_LBUTTONUP As Long = &H202
- Private Const WM_KEYDOWN As Long = &H100
- Private Const WM_KEYUP As Long = &H101
- Private Const WM_VSCROLL As Long = &H115
- Private Const WM_HSCROLL As Long = &H114
- Private Const WM_CHAR As Long = &H102
-
-
-
-
- Private Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hWnd As Long, ByRef lpdwProcessId As Long) As Long
-
- Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
- Private Declare Function VirtualAllocEx Lib "kernel32" (ByVal hProcess As Long, ByVal lpAddress As Long, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long
- Private Declare Function VirtualFreeEx Lib "kernel32" (ByVal hProcess As Long, ByVal lpAddress As Long, ByVal dwSize As Long, ByVal dwFreeType As Long) As Long
- Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Any, ByRef lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
- Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Any, ByRef lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
-
- Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
-
- Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
-
-
- Private Const WM_USER As Long = &H400
- Private Const STANDARD_RIGHTS_REQUIRED As Long = &HF0000
- Private Const SYNCHRONIZE As Long = &H100000
- Private Const PROCESS_ALL_ACCESS As Long = (STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF)
- Private Const PROCESS_DUP_HANDLE As Long = (&H40)
- Private Const MEM_COMMIT As Long = &H1000
- Private Const MEM_TOP_DOWN As Long = &H100000
- Private Const PAGE_READWRITE As Long = &H4
- Private Const TB_GETBUTTON As Long = (WM_USER + 23)
- Private Const MEM_RELEASE As Long = &H8000
-
- Private Const PROCESS_QUERY_INFORMATION = 1024
- Private Const PROCESS_VM_OPERATION = &H8
- Private Const PROCESS_VM_READ = &H10
- Private Const PROCESS_VM_WRITE = &H20
- Private Const MAX_LVMSTRING As Long = 255
-
-
- Private Declare Function GetWindowRect Lib "user32.dll" (ByVal hWnd As Long, ByRef lpRect As RECT) As Long
-
-
- Private Type LV_ITEM
- mask As Long
- iItem As Long
- iSubItem As Long
- state As Long
- stateMask As Long
- pszText As Long
- cchTextMax As Long
- iImage As Long
- lParam As Long
- iIndent As Long
- End Type
-
-
-
- Private Type RECT
- Left As Long
- Top As Long
- Right As Long
- Bottom As Long
- End Type
-
-
-
- Dim HWndGNm As Long
- Dim HWndObjm As Long
-
-
-
- Public Function InitObjectGN(HWndGNp As Long)
- Dim FObjf As New FGDK
- Dim HWndGNf As Long
- Dim GNWinArrf() As Long
-
- HWndGNf = HWndGNp
- HWndGNm = HWndGNf
-
- GNWinArrf() = FObjf.GetGNChWind(HWndGNf)
- HWndObjm = GNWinArrf(14)
- Set FObjf = Nothing
- End Function
-
-
-
- Public Property Get GetWinTop() As Long
- Dim GNRectf As RECT
- Call GetWindowRect(HWndObjm, GNRectf)
- GetWinTop = GNRectf.Top
- End Property
- Public Property Get GetWinBottom() As Long
- Dim GNRectf As RECT
- Call GetWindowRect(HWndObjm, GNRectf)
- GetWinBottom = GNRectf.Bottom
- End Property
- Public Property Get GetWinLeft() As Long
- Dim GNRectf As RECT
- Call GetWindowRect(HWndObjm, GNRectf)
- GetWinLeft = GNRectf.Left
- End Property
- Public Property Get GetWinRight() As Long
- Dim GNRectf As RECT
- Call GetWindowRect(HWndObjm, GNRectf)
- GetWinRight = GNRectf.Right
- End Property
-
- Public Property Get GetWinWidth() As Long
- Dim GNRectf As RECT
- Call GetWindowRect(HWndObjm, GNRectf)
- GetWinWidth = GNRectf.Right - GNRectf.Left
- End Property
- Public Property Get GetWinHeigt() As Long
- Dim GNRectf As RECT
- Call GetWindowRect(HWndObjm, GNRectf)
- GetWinHeigt = GNRectf.Bottom - GNRectf.Top
- End Property
-
- Public Property Get GetHWndTrNameList() As Long
- GetHWndTrNameList = HWndObjm
- End Property
-
- Public Property Get GetHWndGN() As Long
- GetHWndGN = HWndGNm
- End Property
-
-
- Public Property Get GetItemText(ItemIndex As Long, ColumnIndex As Long) As String
- Dim i As Long
- Dim BufStr As String
- Dim Columnf As Long
- Dim NumItemf As Long
- Dim BufStrArr() As Byte
- Dim PtrItmTxt As Long
- Dim PtrItem As Long
- Dim Itemf As LV_ITEM
-
- Dim procidf As Long
- Dim ProcHnd As Long
-
- ReDim BufStrArr(1023)
-
- Columnf = ColumnIndex
- NumItemf = ItemIndex
-
- Call GetWindowThreadProcessId(HWndObjm, procidf)
-
- ProcHnd = OpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_READ Or PROCESS_VM_WRITE, False, procidf)
-
- PtrItmTxt = VirtualAllocEx(ProcHnd, 0, 1023, MEM_COMMIT, PAGE_READWRITE)
-
- Itemf.mask = LVIF_TEXT
- Itemf.cchTextMax = 1023
- Itemf.iSubItem = Columnf
- Itemf.pszText = PtrItmTxt
-
- PtrItem = VirtualAllocEx(ProcHnd, 0, Len(Itemf), MEM_COMMIT, PAGE_READWRITE)
-
- Call WriteProcessMemory(ProcHnd, PtrItem, Itemf, Len(Itemf), 0)
-
- которая выделена для текста в чужом процессе итема и указатель которой находится в структуре, _
- которая находится в чужом процессе по указателю PtrItem
- Call SendMessage(HWndObjm, LVM_GETITEMTEXTA, NumItemf, ByVal PtrItem)
-
- Call ReadProcessMemory(ProcHnd, PtrItmTxt, BufStrArr(0), 1023, 0)
-
- Call VirtualFreeEx(ProcHnd, PtrItmTxt, 0, MEM_RELEASE)
- Call VirtualFreeEx(ProcHnd, PtrItem, 0, MEM_RELEASE)
- Call CloseHandle(ProcHnd)
-
- For i = 0 To 1023
- If Chr(BufStrArr(i)) = vbNullChar Then Exit For
- BufStr = BufStr & Chr(BufStrArr(i))
- Next i
- GetItemText = BufStr
- End Property
-
- Public Property Get GetItemRect(ByVal NumItem As Long) As String
- Dim i As Long
- Dim BufStr As String
- Dim NumItemf As Long
- Dim PtrItem As Long
- Dim ItRect As RECT
-
- Dim procidf As Long
- Dim ProcHnd As Long
-
-
- NumItemf = NumItem
-
- Call GetWindowThreadProcessId(HWndObjm, procidf)
-
- ProcHnd = OpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_READ Or PROCESS_VM_WRITE, False, procidf)
-
- PtrItem = VirtualAllocEx(ProcHnd, 0, Len(ItRect), MEM_COMMIT, PAGE_READWRITE)
-
- которая выделена для этого в чужом процессе итема и указатель которой находится в структуре, _
- которая находится в чужом процессе по указателю PtrItem
- Call SendMessage(HWndObjm, LVM_GETITEMRECT, NumItemf, ByVal PtrItem)
-
- Call ReadProcessMemory(ProcHnd, PtrItem, ItRect, Len(ItRect), 0)
-
- Call VirtualFreeEx(ProcHnd, PtrItem, 0, MEM_RELEASE)
- Call CloseHandle(ProcHnd)
-
- BufStr = ItRect.Bottom & "\" & ItRect.Left & "\" & ItRect.Right & "\" & ItRect.Top
- GetItemRect = BufStr
-
- End Property
-
-
- Public Function ItemMousClick(ByVal NumItem As Long) As Boolean
- Dim i As Long
- Dim BufStr As String
- Dim BufStrArr() As String
- Dim ItemRect As RECT
- Dim X As Long
- Dim Y As Long
- Dim HeightWin As Long
-
- BufStr = Me.GetItemRect(NumItem)
- BufStrArr() = VBA.Split(BufStr, "\")
- ItemRect.Bottom = CLng(VBA.Round(VBA.Val(BufStrArr(0)), 0))
- ItemRect.Left = CLng(VBA.Round(VBA.Val(BufStrArr(1)), 0))
- ItemRect.Right = CLng(VBA.Round(VBA.Val(BufStrArr(2)), 0))
- ItemRect.Top = CLng(VBA.Round(VBA.Val(BufStrArr(3)), 0))
-
- Y = ItemRect.Top + ((ItemRect.Bottom - ItemRect.Top) / 2)
- X = &H20
-
- If ItemRect.Left < 0 Then
- For i = 1 To Round(ItemRect.Left * -1 / 7, 0) + 5
- Call PostMessage(HWndObjm, WM_KEYDOWN, &H25, &H14B00001)
- Call PostMessage(HWndObjm, WM_KEYUP, &H25, &HC14B0001)
- Next i
- End If
-
- HeightWin = Me.GetWinHeigt
- If ItemRect.Bottom > HeightWin Then
- Call PostMessage(HWndObjm, WM_LBUTTONDOWN, 1, (HeightWin - 25) * 65536 + X)
- Call PostMessage(HWndObjm, WM_LBUTTONUP, 0, (HeightWin - 25) * 65536 + X)
-
- For i = 1 To Round((ItemRect.Bottom - HeightWin) / 25, 0) + 4
- Call PostMessage(HWndObjm, WM_KEYDOWN, &H28, &H15000001)
- Call PostMessage(HWndObjm, WM_KEYUP, &H28, &HC1500001)
- Next i
- Call Sleep(500)
- BufStr = Me.GetItemRect(NumItem)
- Erase BufStrArr()
- BufStrArr() = VBA.Split(BufStr, "\")
- ItemRect.Bottom = CLng(VBA.Round(VBA.Val(BufStrArr(0)), 0))
- ItemRect.Left = CLng(VBA.Round(VBA.Val(BufStrArr(1)), 0))
- ItemRect.Right = CLng(VBA.Round(VBA.Val(BufStrArr(2)), 0))
- ItemRect.Top = CLng(VBA.Round(VBA.Val(BufStrArr(3)), 0))
- Y = ItemRect.Top + ((ItemRect.Bottom - ItemRect.Top) / 2)
- End If
-
- If ItemRect.Top < 0 Then
- Call PostMessage(HWndObjm, WM_LBUTTONDOWN, 1, 25 * 65536 + X)
- Call PostMessage(HWndObjm, WM_LBUTTONUP, 0, 25 * 65536 + X)
-
- For i = 1 To Round((ItemRect.Top * -1) / 15, 0) + 3
- Call PostMessage(HWndObjm, WM_KEYDOWN, &H26, &H14800001)
- Call PostMessage(HWndObjm, WM_KEYUP, &H26, &HC1480001)
- Next i
- Call Sleep(500)
- BufStr = Me.GetItemRect(NumItem)
- Erase BufStrArr()
- BufStrArr() = VBA.Split(BufStr, "\")
- ItemRect.Bottom = CLng(VBA.Round(VBA.Val(BufStrArr(0)), 0))
- ItemRect.Left = CLng(VBA.Round(VBA.Val(BufStrArr(1)), 0))
- ItemRect.Right = CLng(VBA.Round(VBA.Val(BufStrArr(2)), 0))
- ItemRect.Top = CLng(VBA.Round(VBA.Val(BufStrArr(3)), 0))
- Y = ItemRect.Top + ((ItemRect.Bottom - ItemRect.Top) / 2)
- End If
-
-
- Call PostMessage(HWndObjm, WM_LBUTTONDOWN, 1, Y * 65536 + X)
- Call PostMessage(HWndObjm, WM_LBUTTONUP, 0, Y * 65536 + X)
-
- ItemMousClick = True
- End Function
-
- Public Function SendEnter()
-
- Call PostMessage(HWndObjm, WM_CHAR, &HD, &H1C0001)
-
- End Function
-
- Public Property Get ATest()
-
- i = SendMessage(HWndObjm, LVM_GETITEMSTATE, 1, LVIS_SELECTED)
- Debug.Print i & " - HHHHH"
-
- End Property
-
-
- Public Property Get SelectedItems() As Scripting.Dictionary
- Dim BufDict As New Scripting.Dictionary
- Dim i As Long
- Dim BufStr As String
- Dim j As Long
-
- j = Me.GetItemCount
- If j = 0 Then
- Set SelectedItems = Nothing
- Exit Property
- End If
-
- For i = 0 To j - 1
- j = SendMessage(HWndObjm, LVM_GETITEMSTATE, i, LVIS_SELECTED)
- BufStr = Me.GetItemText(i, 0)
- If j > 0 Then
- Call BufDict.Add(i, BufStr)
- End If
- Next i
- Set SelectedItems = BufDict
- End Property
-
- Public Property Get SelectedCount() As Long
- Dim i As Long
- Dim j As Long
- Dim Rez As Long
-
- j = Me.GetItemCount
-
- For i = 0 To j - 1
- j = SendMessage(HWndObjm, LVM_GETITEMSTATE, i, LVIS_SELECTED)
- Rez = Rez + j
- Next i
-
- SelectedCount = Rez
-
- End Property
-
- Public Property Get GetItemCount() As Long
- Dim i As Long
- i = SendMessage(HWndObjm, LVM_GETITEMCOUNT, 0, 0)
- GetItemCount = i
- End Property
-
-
- Public Property Get FirstSelItem() As Long
- Dim i As Long
- Dim j As Long
- Dim Rez As Long
-
- j = Me.GetItemCount
- Rez = -1
- For i = 0 To j - 1
- j = SendMessage(HWndObjm, LVM_GETITEMSTATE, i, LVIS_SELECTED)
- If j > 0 Then
- Rez = i
- Exit For
- End If
- Next i
-
- FirstSelItem = Rez
-
- End Property
-
-
Ответить
|
Страница: 1 |
Поиск по форуму