Select an item in a SysListView32 control

Posted on 2006-04-07
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

Question by:NewAgeNemesis
    1 Comment
    LVL 27

    Accepted Solution

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Introduction This article makes the case for using two modules in your VBA/VB6 applications to provide both case-sensitive and case-insensitive text comparison operations.  Recently, I solved an EE question using the LIKE function.  In order for th…
    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    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…

    758 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now