?
Solved

Select an item in a SysListView32 control

Posted on 2006-04-07
4
Medium Priority
?
815 Views
Last Modified: 2007-12-19
i have the window handle of a SysListView32 control in an external application.  I want my program to select/highlight the item of index 3.  Any idea what the API is to do this?? Thanks

-Adam
0
Comment
Question by:NewAgeNemesis
1 Comment
 
LVL 28

Accepted Solution

by:
Ark earned 1000 total points
ID: 16433560
Option Explicit

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 Const LVM_FIRST = &H1000
Private Const LVM_SETITEMSTATE = (LVM_FIRST + 43)
Private Const LVM_GETITEMSTATE = (LVM_FIRST + 44)

Private Const LVIF_STATE = &H8
Private Const LVIS_FOCUSED = &H1
Private Const LVIS_SELECTED = &H2

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId 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, lpAddress As Any, ByVal dwSize As Long, ByVal dwFreeType As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Const MEM_COMMIT = &H1000
Private Const MEM_RESERVE = &H2000
Private Const MEM_RELEASE = &H8000

Private Const PAGE_READWRITE = &H4&
Private Const PROCESS_ALL_ACCESS = &H1F0FFF

Public Sub SelectItemRemote(ByVal hLV As Long, ByVal itmNum As Long)
   Dim tid As Long, pid As Long, hProcess As Long
   Dim liAddr As Long, lWritten As Long
   
   Dim li As LV_ITEM
   
   tid = GetWindowThreadProcessId(hLV, pid)
   If pid = 0 Then Exit Sub
   hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, pid)
   If hProcess = 0 Then Exit Sub
   With li
      .mask = LVIF_STATE
      .state = True
      .stateMask = LVIS_SELECTED Or LVIS_FOCUSED
   End With
   liAddr = VirtualAllocEx(hProcess, 0, Len(li), MEM_RESERVE Or MEM_COMMIT, PAGE_READWRITE)
   WriteProcessMemory hProcess, ByVal liAddr, li, Len(li), lWritten
   Call SendMessage(hLV, LVM_SETITEMSTATE, itmNum, ByVal liAddr)
   VirtualFreeEx hProcess, ByVal liAddr, 0, MEM_RELEASE
   If hProcess Then CloseHandle hProcess
End Sub

'==========
Private Sub Command1_Click()
   SelectItemRemote &H10092, 2 ' Note - lv items index zero based
End Sub
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question